@Test public void test() throws Exception { final List<Integer> source = Lists.newArrayList(); for (int i = 0; i < 5; i++) { source.add(i); } final NioEventloop eventloop = new NioEventloop(); final StreamConsumers.ToList<Integer> consumerToList = StreamConsumers.toList(eventloop); SimpleNioServer server = new SimpleNioServer(eventloop) { @Override protected SocketConnection createConnection(SocketChannel socketChannel) { return new TcpStreamSocketConnection(eventloop, socketChannel) { @Override protected void wire( StreamProducer<ByteBuf> socketReader, StreamConsumer<ByteBuf> socketWriter) { StreamBinaryDeserializer<Integer> streamDeserializer = new StreamBinaryDeserializer<>(eventloop, intSerializer(), 10); streamDeserializer.streamTo(consumerToList); socketReader.streamTo(streamDeserializer); } }; } }; server.setListenAddress(address).acceptOnce(); server.listen(); final StreamBinarySerializer<Integer> streamSerializer = new StreamBinarySerializer<>(eventloop, intSerializer(), 1, 10, 0, false); reconnect( eventloop, address, defaultSocketSettings(), 3, 100L, new ConnectCallback() { @Override public void onConnect(SocketChannel socketChannel) { SocketConnection connection = new TcpStreamSocketConnection(eventloop, socketChannel) { @Override protected void wire( StreamProducer<ByteBuf> socketReader, StreamConsumer<ByteBuf> socketWriter) { streamSerializer.streamTo(socketWriter); StreamProducers.ofIterable(eventloop, source).streamTo(streamSerializer); } }; connection.register(); } @Override public void onException(Exception exception) { fail(); } }); eventloop.run(); assertEquals(source, consumerToList.getList()); assertEquals(getPoolItemsString(), ByteBufPool.getCreatedItems(), ByteBufPool.getPoolItems()); }
@Test public void testGsonMessagingUpload() throws Exception { final List<Long> source = Lists.newArrayList(); for (long i = 0; i < 100; i++) { source.add(i); } final NioEventloop eventloop = new NioEventloop(); final StreamConsumers.ToList<Long> consumerToList = StreamConsumers.toList(eventloop); SimpleNioServer server = new SimpleNioServer(eventloop) { @Override protected SocketConnection createConnection(SocketChannel socketChannel) { return new StreamMessagingConnection<>( eventloop, socketChannel, new StreamGsonDeserializer<>(eventloop, new Gson(), String.class, 10), new StreamGsonSerializer<>(eventloop, new Gson(), String.class, 1, 50, 0)) .addHandler( String.class, new MessagingHandler<String, String>() { @Override public void onMessage(String item, Messaging<String> messaging) { assertEquals("start", item); StreamBinaryDeserializer<Long> streamDeserializer = new StreamBinaryDeserializer<>(eventloop, longSerializer(), 10); messaging.binarySocketReader().streamTo(streamDeserializer); streamDeserializer.streamTo(consumerToList); messaging.shutdownWriter(); } }); } }; server.setListenAddress(address).acceptOnce(); server.listen(); eventloop.connect( address, new SocketSettings(), new ConnectCallback() { @Override public void onConnect(SocketChannel socketChannel) { SocketConnection connection = new StreamMessagingConnection<>( eventloop, socketChannel, new StreamGsonDeserializer<>(eventloop, new Gson(), String.class, 10), new StreamGsonSerializer<>(eventloop, new Gson(), String.class, 1, 50, 0)) .addStarter( new MessagingStarter<String>() { @Override public void onStart(Messaging<String> messaging) { messaging.sendMessage("start"); StreamBinarySerializer<Long> streamSerializer = new StreamBinarySerializer<>( eventloop, longSerializer(), 1, 10, 0, false); StreamProducers.ofIterable(eventloop, source) .streamTo(streamSerializer); streamSerializer.streamTo(messaging.binarySocketWriter()); messaging.shutdownReader(); } }); connection.register(); } @Override public void onException(Exception e) { fail("Test Exception: " + e); } }); eventloop.run(); assertEquals(source, consumerToList.getList()); // FIXME: 1 ByteBuf Leak (ByteBuf(16) produced by StreamBinaryDeserializer) // assertEquals(getPoolItemsString(), ByteBufPool.getCreatedItems(), // ByteBufPool.getPoolItems()); }
@Test public void testPing() throws Exception { final NioEventloop eventloop = new NioEventloop(); SimpleNioServer server = new SimpleNioServer(eventloop) { @Override protected SocketConnection createConnection(SocketChannel socketChannel) { return new StreamMessagingConnection<>( eventloop, socketChannel, new StreamBinaryDeserializer<>(eventloop, intSerializer(), 10), new StreamBinarySerializer<>(eventloop, intSerializer(), 2, 10, 0, false)) .addHandler( Integer.class, new MessagingHandler<Integer, Integer>() { @Override public void onMessage(Integer item, Messaging<Integer> messaging) { System.out.println(item); messaging.sendMessage(item); } }); } }; server.setListenAddress(address).acceptOnce(); server.listen(); eventloop.connect( address, new SocketSettings(), new ConnectCallback() { @Override public void onConnect(SocketChannel socketChannel) { SocketConnection connection = new StreamMessagingConnection<>( eventloop, socketChannel, new StreamBinaryDeserializer<>(eventloop, intSerializer(), 10), new StreamBinarySerializer<>(eventloop, intSerializer(), 2, 10, 0, false)) .addStarter( new MessagingStarter<Integer>() { @Override public void onStart(Messaging<Integer> messaging) { messaging.sendMessage(3); } }) .addHandler( Integer.class, new MessagingHandler<Integer, Integer>() { @Override public void onMessage(Integer item, Messaging<Integer> messaging) { if (item > 0) { messaging.sendMessage(item - 1); } else { messaging.shutdown(); } } }); connection.register(); } @Override public void onException(Exception exception) { fail("Test Exception: " + exception); } }); eventloop.run(); assertEquals(getPoolItemsString(), ByteBufPool.getCreatedItems(), ByteBufPool.getPoolItems()); }
@Test public void testBinaryMessagingUploadAck() throws Exception { final List<Long> source = Lists.newArrayList(); for (long i = 0; i < 100; i++) { source.add(i); } final AtomicBoolean ack = new AtomicBoolean(false); final NioEventloop eventloop = new NioEventloop(); final StreamConsumers.ToList<Long> consumerToList = StreamConsumers.toList(eventloop); SimpleNioServer server = new SimpleNioServer(eventloop) { @Override protected SocketConnection createConnection(SocketChannel socketChannel) { return new StreamMessagingConnection<>( eventloop, socketChannel, new StreamBinaryDeserializer<>(eventloop, stringSerializer(), 10), new StreamBinarySerializer<>(eventloop, stringSerializer(), 2, 10, 0, false)) .addHandler( String.class, new MessagingHandler<String, String>() { @Override public void onMessage(String item, final Messaging<String> messaging) { assertEquals("start", item); System.out.println("receive start"); StreamBinaryDeserializer<Long> streamDeserializer = new StreamBinaryDeserializer<>(eventloop, longSerializer(), 10); messaging.binarySocketReader().streamTo(streamDeserializer); streamDeserializer.streamTo(consumerToList); consumerToList.addCompletionCallback( new CompletionCallback() { @Override public void onComplete() { System.out.println("send ack"); messaging.sendMessage("ack"); messaging.shutdown(); } @Override public void onException(Exception exception) { messaging.shutdown(); } }); } }); } }; server.setListenAddress(address).acceptOnce(); server.listen(); eventloop.connect( address, new SocketSettings(), new ConnectCallback() { @Override public void onConnect(SocketChannel socketChannel) { SocketConnection connection = new StreamMessagingConnection<>( eventloop, socketChannel, new StreamBinaryDeserializer<>(eventloop, stringSerializer(), 10), new StreamBinarySerializer<>( eventloop, stringSerializer(), 2, 10, 0, false)) .addStarter( new MessagingStarter<String>() { @Override public void onStart(Messaging<String> messaging) { System.out.println("send start"); messaging.sendMessage("start"); StreamBinarySerializer<Long> streamSerializer = new StreamBinarySerializer<>( eventloop, longSerializer(), 1, 10, 0, false); StreamProducers.ofIterable(eventloop, source) .streamTo(streamSerializer); streamSerializer.streamTo(messaging.binarySocketWriter()); } }) .addHandler( String.class, new MessagingHandler<String, String>() { @Override public void onMessage(String item, Messaging<String> messaging) { ack.set(true); assertEquals("ack", item); System.out.println("receive ack"); } }); connection.register(); } @Override public void onException(Exception e) { fail("Test Exception: " + e); } }); eventloop.run(); assertEquals(source, consumerToList.getList()); assertTrue(ack.get()); assertEquals(getPoolItemsString(), ByteBufPool.getCreatedItems(), ByteBufPool.getPoolItems()); }