NettyNetworkManager implements SynchronousNetworkManager<Channel>, AsynchronousNetworkManager<Channel> { @Override public void close() throws IOException, IllegalStateException { if (this.open.compareAndSet(true, false)) { try { this.eventGroup.shutdownGracefully(0L, 5L, TimeUnit.SECONDS).sync(); } catch (Exception e) { throw new IOException("Could not be gracefully closed.", e); } } else { throw new IllegalStateException("Network Manager is already closed"); } } 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 testCloseSync() throws Exception { this.eventLoopGroup = new NioEventLoopGroup(1); final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class); PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup); final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap); networkManager.close(); assertTrue(eventLoopGroup.isShutdown()); }
@Test(expected = IOException.class) public void testCloseSyncFailure() throws Exception { final EventLoopGroup eventLoopGroup = mock(EventLoopGroup.class); final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class); PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup); final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap); when(eventLoopGroup.shutdownGracefully(any(Long.class), any(Long.class), any(TimeUnit.class))).thenThrow(new RuntimeException("Testing purposes!")); networkManager.close(); }
@SuppressWarnings("unchecked") @Test public void testCloseAsync() { this.eventLoopGroup = new NioEventLoopGroup(1); final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class); PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup); final FutureCallback<Void> callback = mock(FutureCallback.class); final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap); networkManager.close(callback); verify(callback, timeout((NettyNetworkManager.SHUTDOWN_TIMEOUT + 1) * 1000)).onSuccess(null); assertTrue(eventLoopGroup.isShutdown()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testCloseAsyncFailure() { final EventLoopGroup eventLoopGroup = mock(EventLoopGroup.class); final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class); PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup); final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap); final FutureCallback<Void> callback = mock(FutureCallback.class); final Exception exception = new RuntimeException("Testing purposes!"); this.eventExecutor = new DefaultEventExecutor(); final Future shutdownFuture = new FailedFuture<>(this.eventExecutor, exception); when(eventLoopGroup.shutdownGracefully(any(Long.class), any(Long.class), any(TimeUnit.class))).thenReturn(shutdownFuture); networkManager.close(callback); verify(callback, timeout(100)).onFailure(exception); }