IBRealTimeBarEngine implements IRealtimeBarEngine { @Override public void unsubscribeRealtimeBars(RealtimeBarRequest request, RealtimeBarListener listener) { Ticker ticker = request.getTicker(); IBarBuilder builder = barMap.get(request); if( builder != null ) { builder.removeBarListener(listener); if( builder.getListenerCount() == 0 ) { quoteEngine.unsubscribeLevel1(ticker, builder); barMap.remove(request); } } } IBRealTimeBarEngine(IQuoteEngine quoteEngine, IHistoricalDataProvider historicalDataProvider); @Override boolean isConnected(); @Override void subscribeRealtimeBars(RealtimeBarRequest request, RealtimeBarListener listener); @Override void unsubscribeRealtimeBars(RealtimeBarRequest request, RealtimeBarListener listener);  }
@Test public void testUnsubscribeRealtimeBars() { StockTicker ticker = new StockTicker("qqq"); int timeInterval = 1; IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.MINUTE); final RealtimeBarListener listener = mockery.mock(RealtimeBarListener.class); final IBarBuilder mockBuilder = mockery.mock( IBarBuilder.class ); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); engine.barMap.put(request, mockBuilder ); mockery.checking( new Expectations() {{ one(mockBuilder).removeBarListener(listener); one(mockBuilder).getListenerCount(); will(returnValue(1)); }}); engine.unsubscribeRealtimeBars(request, listener); mockery.assertIsSatisfied(); }
@Test public void testUnsubscribeRealtimeBars_NoMoreListeners() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 1; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.MINUTE); final RealtimeBarListener listener = mockery.mock(RealtimeBarListener.class); final IBarBuilder mockBuilder = mockery.mock( IBarBuilder.class ); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); engine.barMap.put(request, mockBuilder ); mockery.checking( new Expectations() {{ one(mockBuilder).removeBarListener(listener); one(mockBuilder).getListenerCount(); will(returnValue(0)); one(mockQuoteEngine).unsubscribeLevel1(ticker, mockBuilder); }}); engine.unsubscribeRealtimeBars(request, listener); assertNull( engine.barMap.get(request)); mockery.assertIsSatisfied(); }