ITCHSession implements Closeable { public int receive() throws IOException { int bytes = channel.read(rxBuffer); if (bytes <= 0) return bytes; rxBuffer.flip(); while (parse()); rxBuffer.compact(); if (rxBuffer.position() == rxBuffer.capacity()) throw new ITCHException("Packet length exceeds buffer capacity"); receivedData(); return bytes; } protected  ITCHSession(Clock clock, SocketChannel channel, int rxBufferCapacity,
            byte heartbeatMessageType); SocketChannel getChannel(); int receive(); void keepAlive(); @Override void close();  }
@Test public void loginAccepted() throws Exception { ASCII.putLongRight(loginAccepted.sequenceNumber, 1); server.accept(loginAccepted); while (clientEvents.collect().size() != 1) client.receive(); assertEquals(asList(new LoginAccepted(1)), clientEvents.collect()); }
@Test public void instrumentDirectoryRequest() throws Exception { client.requestInstrumentDirectory(); while (serverEvents.collect().size() != 1) server.receive(); assertEquals(asList(new InstrumentDirectoryRequest()), serverEvents.collect()); }
@Test public void loginRejected() throws Exception { ASCII.putLeft(loginRejected.reason, "foo"); server.reject(loginRejected); while (clientEvents.collect().size() != 1) client.receive(); assertEquals(asList(new LoginRejected("foo ")), clientEvents.collect()); }
@Test public void endOfSession() throws Exception { server.endSession(); while (clientEvents.collect().size() != 1) client.receive(); assertEquals(asList(new EndOfSession()), clientEvents.collect()); }
@Test public void errorNotification() throws Exception { ASCII.putLeft(errorNotification.errorExplanation, "foo"); server.notifyError(errorNotification); while (clientEvents.collect().size() != 1) client.receive(); assertEquals(asList(new ErrorNotification( "foo " + " ")), clientEvents.collect()); }
@Test public void instrumentDirectory() throws Exception { ASCII.putLongRight(instrumentDirectory.numberOfCurrencyPairs, 3); ASCII.putLeft(instrumentDirectory.currencyPair[0], "FOO/BAR"); ASCII.putLeft(instrumentDirectory.currencyPair[1], "BAR/BAZ"); ASCII.putLeft(instrumentDirectory.currencyPair[2], "BAZ/FOO"); server.instrumentDirectory(instrumentDirectory); while (clientEvents.collect().size() != 1) client.receive(); assertEquals(asList(new InstrumentDirectory(asList("FOO/BAR", "BAR/BAZ", "BAZ/FOO"))), clientEvents.collect()); }
@Test public void loginRequest() throws Exception { ASCII.putLeft(loginRequest.loginName, "foo"); ASCII.putLeft(loginRequest.password, "bar"); loginRequest.marketDataUnsubscribe = ITCH.TRUE; ASCII.putLongRight(loginRequest.reserved, 0); client.login(loginRequest); while (serverEvents.collect().size() != 1) server.receive(); assertEquals(asList(new LoginRequest( "foo ", "bar ", ITCH.TRUE, 0)), serverEvents.collect()); }
@Test public void logoutRequest() throws Exception { client.logout(); while (serverEvents.collect().size() != 1) server.receive(); assertEquals(asList(new LogoutRequest()), serverEvents.collect()); }
@Test public void marketSnapshotRequest() throws Exception { ASCII.putLeft(marketSnapshotRequest.currencyPair, "FOO/BAR"); client.request(marketSnapshotRequest); while (serverEvents.collect().size() != 1) server.receive(); assertEquals(asList(new MarketSnapshotRequest("FOO/BAR")), serverEvents.collect()); }
@Test public void tickerSubscription() throws Exception { ASCII.putLeft(tickerSubscribeRequest.currencyPair, "FOO/BAR"); ASCII.putLeft(tickerUnsubscribeRequest.currencyPair, "FOO/BAR"); client.request(tickerSubscribeRequest); client.request(tickerUnsubscribeRequest); while (serverEvents.collect().size() != 2) server.receive(); assertEquals(asList(new TickerSubscribeRequest("FOO/BAR"), new TickerUnsubscribeRequest("FOO/BAR")), serverEvents.collect()); }
@Test public void marketDataSubscription() throws Exception { ASCII.putLeft(marketDataSubscribeRequest.currencyPair, "FOO/BAR"); ASCII.putLeft(marketDataUnsubscribeRequest.currencyPair, "FOO/BAR"); client.request(marketDataSubscribeRequest); client.request(marketDataUnsubscribeRequest); while (serverEvents.collect().size() != 2) server.receive(); assertEquals(asList(new MarketDataSubscribeRequest("FOO/BAR"), new MarketDataUnsubscribeRequest("FOO/BAR")), serverEvents.collect()); }