NettyNetworkManager implements SynchronousNetworkManager<Channel>, AsynchronousNetworkManager<Channel> { @Override public Channel openChannel() throws IOException, IllegalStateException { assertOpen(); try { return this.bootstrap.clone().register().sync().channel(); } catch (Exception e) { throw new IOException("Could not open channel.", e); } } NettyNetworkManager();  NettyNetworkManager(int threadCount);  NettyNetworkManager(Bootstrap bootstrap); @Override Channel openChannel(); @Override void openChannel(FutureCallback<Channel> callback); @Override void close(); @Override void close(FutureCallback<Void> callback);  }
@Test public void testOpenChannelSync() throws Exception { this.eventLoopGroup = new NioEventLoopGroup(1); final ChannelHandler channelHandler = mock(ChannelHandler.class); final Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioDatagramChannel.class).handler(channelHandler); try (final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap)) { final Channel channel = networkManager.openChannel(); assertTrue(channel.isOpen()); assertTrue(channel.isRegistered()); assertFalse(channel.isActive()); } }
@Test(expected = IOException.class) public void testOpenChannelSyncFailure() throws Exception { this.eventLoopGroup = new NioEventLoopGroup(1); final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class); PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup); final Exception exception = new RuntimeException("Testing purposes!"); try (final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap)) { when(bootstrap.clone()).thenReturn(bootstrap); when(bootstrap.register()).thenThrow(exception); networkManager.openChannel(); } }
@SuppressWarnings({ "unchecked" }) @Test public void testOpenChannelAsync() throws Exception { this.eventLoopGroup = new NioEventLoopGroup(1); final ChannelHandler channelHandler = mock(ChannelHandler.class); final Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioDatagramChannel.class).handler(channelHandler); this.eventExecutor = new DefaultEventExecutor(); final FutureCallback<Channel> callback = mock(FutureCallback.class); final ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class); try (final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap)) { networkManager.openChannel(callback); verify(callback, timeout(100)).onSuccess(captor.capture()); assertTrue(captor.getValue().isOpen()); assertTrue(captor.getValue().isRegistered()); assertFalse(captor.getValue().isActive()); } }
@SuppressWarnings("unchecked") @Test public void testOpenChannelAsyncFailure() throws Exception { this.eventLoopGroup = new NioEventLoopGroup(1); final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class); PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup); this.eventExecutor = new DefaultEventExecutor(); final FutureCallback<Channel> callback = mock(FutureCallback.class); final Channel channel = mock(Channel.class); final ChannelPromise promise = new DefaultChannelProgressivePromise(channel, eventExecutor); final Exception exception = new RuntimeException("Testing purposes!"); try (final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap)) { when(bootstrap.clone()).thenReturn(bootstrap); when(bootstrap.register()).thenReturn(promise); promise.setFailure(exception); networkManager.openChannel(callback); verify(callback, timeout(100)).onFailure(exception); } }