QuoteEngine implements IQuoteEngine { @Override public void fireMarketDepthQuote(ILevel2Quote quote) { synchronized (level2ListenerMap) { List<Level2QuoteListener> listeners = level2ListenerMap.get(quote.getTicker()); if (listeners == null) { return; } for (Level2QuoteListener listener : listeners) { try { synchronized (listeners) { Thread thread = new Thread(() -> { listener.level2QuoteReceived(quote); }); thread.start(); } } catch (Exception ex) { } } } } QuoteEngine(); void addErrorListener(ErrorListener listener); void removeErrorListener(ErrorListener listener); void fireErrorEvent(QuoteError error); void subscribeLevel1(Ticker ticker, Level1QuoteListener listener); void unsubscribeLevel1(Ticker ticker, Level1QuoteListener listener); @Override void fireLevel1Quote(final ILevel1Quote quote); @Override void fireMarketDepthQuote(ILevel2Quote quote); void subscribeMarketDepth(Ticker ticker, Level2QuoteListener listener); void unsubscribeMarketDepth(Ticker ticker, Level2QuoteListener listener);  }
@Test public void testFireMarketDepthQuote() throws Exception { QuoteEngine quoteEngine = createNewQuoteEngine(); final ILevel2Quote mockQuote = mockery.mock( ILevel2Quote.class ); final Map mockLevel2ListenerMap = mockery.mock( Map.class ); final Ticker ticker = new StockTicker( "ABC" ); final Level2QuoteListener mockListener = mockery.mock( Level2QuoteListener.class ); final List<Level2QuoteListener> mockListenerList = new ArrayList<Level2QuoteListener>(); mockListenerList.add( mockListener ); quoteEngine.level2ListenerMap = mockLevel2ListenerMap; mockery.checking( new Expectations() {{ one(mockQuote).getTicker(); will(returnValue(ticker)); one(mockLevel2ListenerMap).get(ticker); will(returnValue(mockListenerList)); one(mockListener).level2QuoteReceived(mockQuote); }}); try { quoteEngine.fireMarketDepthQuote(mockQuote); } catch( Exception ex ) { fail(); } Thread.sleep(1000); mockery.assertIsSatisfied(); }
@Test public void testFireLevelMarketDepthQuote_NoListeners() { QuoteEngine quoteEngine = createNewQuoteEngine(); final ILevel2Quote mockQuote = mockery.mock( ILevel2Quote.class ); final Map mockLevel2ListenerMap = mockery.mock( Map.class ); final Ticker ticker = new StockTicker( "ABC" ); quoteEngine.level2ListenerMap = mockLevel2ListenerMap; mockery.checking( new Expectations() {{ one(mockQuote).getTicker(); will(returnValue(ticker)); one(mockLevel2ListenerMap).get(ticker); will(returnValue(null)); }}); quoteEngine.fireMarketDepthQuote(mockQuote); mockery.assertIsSatisfied(); }