@Test public void testValidInitRequest() throws Exception { // Given EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler()); InitRequest initRequest = new InitRequest( 42, InitMessage.DEFAULT_VERSION, new HashMap<String, String>() { { put(InitMessage.HOST_PORT_KEY, "0.0.0.0:0"); put(InitMessage.PROCESS_NAME_KEY, "test-process"); } }); channel.writeInbound(initRequest); channel.writeOutbound(channel.readInbound()); // Then InitResponse initResponse = channel.readOutbound(); // Assert assertNotNull(initResponse); assertEquals(initRequest.getId(), initResponse.getId()); assertEquals(initRequest.getVersion(), initResponse.getVersion()); assertEquals(initRequest.getHostPort(), initResponse.getHostPort()); }
@Test public void testInitHandlerRemovesItself() throws Exception { // Given EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler()); assertEquals(3, channel.pipeline().names().size()); InitRequest initRequest = new InitRequest( 42, InitMessage.DEFAULT_VERSION, new HashMap<String, String>() { { put(InitMessage.HOST_PORT_KEY, "0.0.0.0:0"); put(InitMessage.PROCESS_NAME_KEY, "test-process"); } }); channel.writeInbound(initRequest); channel.writeOutbound(channel.readInbound()); // Then InitResponse initResponse = channel.readOutbound(); // Assert assertNotNull(initResponse); assertEquals(initRequest.getId(), initResponse.getId()); assertEquals(initRequest.getVersion(), initResponse.getVersion()); assertEquals(initRequest.getHostPort(), initResponse.getHostPort()); // Assert Pipeline is empty assertEquals(2, channel.pipeline().names().size()); // Make sure Messages are still passed through channel.writeInbound(initRequest); channel.writeOutbound(channel.readInbound()); InitRequest sameInitRequest = channel.readOutbound(); assertEquals(initRequest.getId(), sameInitRequest.getId()); assertEquals(initRequest.getVersion(), sameInitRequest.getVersion()); assertEquals(initRequest.getHostPort(), sameInitRequest.getHostPort()); }