@Override public final ChannelFuture close() { return close(newPromise()); }
@Override public final ChannelFuture close() { return close(newPromise()); }
@Override public final ChannelFuture close() { return close(newPromise()); }
/** * Mark this {@link Channel} as finished. Any further try to write data to it will fail. * * @param releaseAll if {@code true} all pending message in the inbound and outbound buffer are released. * @return bufferReadable returns {@code true} if any of the used buffers has something left to read */ private boolean finish(boolean releaseAll) { close(); try { checkException(); return isNotEmpty(inboundMessages) || isNotEmpty(outboundMessages); } finally { if (releaseAll) { releaseAll(inboundMessages); releaseAll(outboundMessages); } } }
@Test void shouldManageChannelsInChannelInitializer() { NetworkConnectionTracker connectionTracker = mock( NetworkConnectionTracker.class ); SocketTransport socketTransport = newSocketTransport( connectionTracker, NO_THROTTLE ); EmbeddedChannel channel = new EmbeddedChannel( socketTransport.channelInitializer() ); ArgumentCaptor<TrackedNetworkConnection> captor = ArgumentCaptor.forClass( TrackedNetworkConnection.class ); verify( connectionTracker ).add( captor.capture() ); verify( connectionTracker, never() ).remove( any() ); channel.close(); verify( connectionTracker ).remove( captor.getValue() ); }
/** * Mark this {@link Channel} as finished. Any further try to write data to it will fail. * * @param releaseAll if {@code true} all pending message in the inbound and outbound buffer are released. * @return bufferReadable returns {@code true} if any of the used buffers has something left to read */ private boolean finish(boolean releaseAll) { close(); try { checkException(); return isNotEmpty(inboundMessages) || isNotEmpty(outboundMessages); } finally { if (releaseAll) { releaseAll(inboundMessages); releaseAll(outboundMessages); } } }
@Test(timeout = 60000) public void testDiscardReadWhenStopped() throws Exception { assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); handlerRule.channel.close(); // Should remove handler. handlerRule.channel.runPendingTasks(); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Stopped)); final ByteBuf msg = Unpooled.buffer().writeBytes("Hello".getBytes()); handlerRule.handler.channelRead(Mockito.mock(ChannelHandlerContext.class), msg); assertThat("Message not released when stopped.", msg.refCnt(), is(0)); }
@Test void shouldManageThrottlersInChannelInitializer() { TransportThrottleGroup throttleGroup = mock( TransportThrottleGroup.class ); SocketTransport socketTransport = newSocketTransport( NetworkConnectionTracker.NO_OP, throttleGroup ); EmbeddedChannel channel = new EmbeddedChannel( socketTransport.channelInitializer() ); verify( throttleGroup ).install( channel ); verify( throttleGroup, never() ).uninstall( channel ); channel.close(); verify( throttleGroup ).uninstall( channel ); }
@Test(timeout = 60000) public void testWriteFailed() throws Exception { subscriberRule.start(); String msg1 = "msg1"; subscriberRule.writeMessages(msg1); assertThat("Unexpected promise completion state.", subscriberRule.channelPromise.isDone(), is(false)); subscriberRule.channel.close(); assertThat("Unexpected promise completion state.", subscriberRule.channelPromise.isDone(), is(true)); assertThat("Unexpected promise result.", subscriberRule.channelPromise.isSuccess(), is(false)); }
/** * Mark this {@link Channel} as finished. Any further try to write data to it will fail. * * @param releaseAll if {@code true} all pending message in the inbound and outbound buffer are released. * @return bufferReadable returns {@code true} if any of the used buffers has something left to read */ private boolean finish(boolean releaseAll) { close(); try { checkException(); return isNotEmpty(inboundMessages) || isNotEmpty(outboundMessages); } finally { if (releaseAll) { releaseAll(inboundMessages); releaseAll(outboundMessages); } } }
@Test(timeout = 60000) public void testBufferDrainOnRemove() throws Exception { assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); final ByteBuf msg1 = Unpooled.buffer().writeBytes("hello1".getBytes()); final ByteBuf msg2 = Unpooled.buffer().writeBytes("hello2".getBytes()); handlerRule.feedMessagesForRead(msg1, msg2); /*More supply then demand*/ handlerRule.setMaxMessagesPerRead(2); /*Send all msgs in one iteration and cause buffer*/ handlerRule.requestMessages(1); /*Less demand*/ assertThat("Unexpected read requested count.", handlerRule.getReadRequestedCount(), is(1)); handlerRule.assertMessagesReceived(msg1); /*Since, the demand is met (requested 1 and got 1) , we move to buffering.*/ assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); assertThat("Unexpected buffer size.", handlerRule.handler.getBuffer(), hasSize(1)); assertThat("Unexpected buffer contents.", handlerRule.handler.getBuffer(), contains((Object) msg2)); assertThat("Unexpected buffer read index.", handlerRule.handler.getCurrentBufferIndex(), is(0)); handlerRule.channel.close(); // Should remove handler. handlerRule.channel.runPendingTasks(); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Stopped)); assertThat("Unexpected buffer size.", handlerRule.handler.getBuffer(), is(nullValue())); assertThat("Unexpected buffer read index.", handlerRule.handler.getCurrentBufferIndex(), is(0)); assertThat("Buffered item not released.", msg2.refCnt(), is(0)); }
channel.close(); verify( connection ).stop();
@Test(timeout = 60000) public void testCloseListener() throws Exception { Observable<Void> closeListener = channelOpRule.channelOperations.closeListener(); TestSubscriber<Void> subscriber = new TestSubscriber<>(); closeListener.subscribe(subscriber); subscriber.assertNoTerminalEvent(); subscriber.unsubscribe(); subscriber.assertNoTerminalEvent(); channelOpRule.channel.close().sync(); subscriber.assertNoTerminalEvent(); }
@Override public final ChannelFuture close() { return close(newPromise()); }
@Override public final ChannelFuture close() { return close(newPromise()); }
private void verifyFailure(String bad) throws Exception { // Send the bad vote EmbeddedChannel channel = createChannel(); byte[] encrypted = RSA.encrypt(bad.getBytes(StandardCharsets.UTF_8), TestVotifierPlugin.getI().getProtocolV1Key().getPublic()); ByteBuf encryptedByteBuf = Unpooled.wrappedBuffer(encrypted); try { channel.writeInbound(encryptedByteBuf); } finally { channel.close(); } }
@Test public void TestIsCallingNewMessageOnEveryMessage() { EmbeddedChannel embeddedChannel = new EmbeddedChannel(new BeatsHandler(spyListener)); embeddedChannel.writeInbound(batch); assertEquals(messageCount, spyListener.getLastMessages().size()); embeddedChannel.close(); }
@Test public void TestItCalledOnNewConnectionOnListenerWhenHandlerIsAdded() { EmbeddedChannel embeddedChannel = new EmbeddedChannel(new BeatsHandler(spyListener)); embeddedChannel.writeInbound(batch); assertTrue(spyListener.isOnNewConnectionCalled()); embeddedChannel.close(); }
@Test public void TestItCalledOnConnectionCloseOnListenerWhenChannelIsRemoved() { EmbeddedChannel embeddedChannel = new EmbeddedChannel(new BeatsHandler(spyListener)); embeddedChannel.writeInbound(batch); embeddedChannel.close(); assertTrue(spyListener.isOnConnectionCloseCalled()); }
@Test public void testAcksLastMessageInBatch() { EmbeddedChannel embeddedChannel = new EmbeddedChannel(new BeatsHandler(spyListener)); embeddedChannel.writeInbound(batch); assertEquals(messageCount, spyListener.getLastMessages().size()); Ack ack = embeddedChannel.readOutbound(); assertEquals(ack.getProtocol(), Protocol.VERSION_1); assertEquals(ack.getSequence(), startSequenceNumber + messageCount - 1); embeddedChannel.close(); } }