AsyncNettyNetworkChannel implements AsynchronousNetworkChannel<M> { @Override public void receive() { if (isBound()) { this.context.getChannel().read(); } } AsyncNettyNetworkChannel(NettyNetworkManager networkManager);  AsyncNettyNetworkChannel(NettyNetworkChannelGlobalContext context); @Override boolean isOpen(); @Override boolean isBound(); @Override SocketAddress getLocalAddress(); @Override boolean isConnected(); @Override SocketAddress getRemoteAddress(); @Override void open(FutureCallback<Void> callback); @Override void close(FutureCallback<Void> callback); @Override void bind(SocketAddress localAddress, FutureCallback<Void> callback); @Override void connect(SocketAddress remoteAddress, FutureCallback<Void> callback); @Override void disconnect(FutureCallback<Void> callback); @Override void receive(); @Override void send(M message, FutureCallback<Void> callback); @Override void send(M message, SocketAddress remoteAddress, FutureCallback<Void> callback); static final int N_THREADS; }
@SuppressWarnings("unchecked") @Test public void testReceive() throws Exception { final String message = "hello"; final byte[] data = message.getBytes(); final ByteBuffer dataBuffer = ByteBuffer.allocate(data.length); final SocketAddress localAddress = new InetSocketAddress("127.0.0.1", 2427); final SocketAddress remoteAddress = new InetSocketAddress("127.0.0.1", 2727); final ObjectChannelHandler channelHandler = new ObjectChannelHandler(); this.eventGroup = new NioEventLoopGroup(); final Bootstrap bootstrap = new Bootstrap().group(eventGroup).handler(channelHandler).channel(NioDatagramChannel.class); final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap); final AsyncNettyNetworkChannel<String> networkChannel = new AsyncNettyNetworkChannel<>(networkManager); final FutureCallback<Void> openCallback = mock(FutureCallback.class); final FutureCallback<Void> bindCallback = mock(FutureCallback.class); final FutureCallback<Void> closeCallback = mock(FutureCallback.class); this.remotePeer = DatagramChannel.open(); this.remotePeer.configureBlocking(false); this.remotePeer.bind(remoteAddress); networkChannel.open(openCallback); verify(openCallback, timeout(100)).onSuccess(null); networkChannel.bind(localAddress, bindCallback); verify(bindCallback, timeout(100)).onSuccess(null); dataBuffer.put(data); dataBuffer.flip(); int sent = remotePeer.send(dataBuffer, localAddress); Thread.sleep(20); this.remotePeer.close(); networkChannel.close(closeCallback); assertEquals(sent, data.length); assertEquals(message, channelHandler.getMessage()); }