Search in sources :

Example 1 with ChannelOutboundHandler

use of io.netty.channel.ChannelOutboundHandler in project ambry by linkedin.

the class ChannelWriteCallback method behaviourUnderWriteFailuresTest.

/**
 * Tests behaviour of various functions of {@link NettyResponseChannel} under write failures.
 * @throws Exception
 */
@Test
public void behaviourUnderWriteFailuresTest() throws Exception {
    onResponseCompleteUnderWriteFailureTest(TestingUri.ImmediateResponseComplete);
    onResponseCompleteUnderWriteFailureTest(TestingUri.OnResponseCompleteWithNonRestException);
    // writing to channel with a outbound handler that generates an Exception
    String message = UtilsTest.getRandomString(10);
    try {
        String content = "@@randomContent@@@";
        MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
        ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler(new Exception(message));
        EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
        // channel has been closed because of write failure
        channel.writeInbound(createContent(content, true));
        verifyCallbacks(processor);
        fail("Callback for write would have thrown an Exception");
    } catch (Exception e) {
        assertEquals("Exception not as expected", message, e.getMessage());
    }
    // writing to channel with a outbound handler that encounters a ClosedChannelException
    try {
        String content = "@@randomContent@@@";
        MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
        ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler(new ClosedChannelException());
        EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
        // channel has been closed because of write failure
        channel.writeInbound(createContent(content, true));
        verifyCallbacks(processor);
        fail("Callback for write would have thrown an Exception");
    } catch (IOException e) {
        assertTrue("Should be recognized as a client termination", Utils.isPossibleClientTermination(e));
    }
    // writing to channel with a outbound handler that generates an Error
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    EmbeddedChannel channel = new EmbeddedChannel(new ErrorOutboundHandler(), processor);
    try {
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, TestingUri.WriteFailureWithThrowable.toString(), null));
        verifyCallbacks(processor);
    } catch (Error e) {
        assertEquals("Unexpected error", ErrorOutboundHandler.ERROR_MESSAGE, e.getMessage());
    }
    channel = createEmbeddedChannel();
    processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ResponseFailureMidway.toString(), null));
    verifyCallbacks(processor);
    assertFalse("Channel is not closed at the remote end", channel.isActive());
}
Also used : ChannelOutboundHandler(io.netty.channel.ChannelOutboundHandler) ClosedChannelException(java.nio.channels.ClosedChannelException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) ParseException(java.text.ParseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Aggregations

UtilsTest (com.github.ambry.utils.UtilsTest)1 ChannelOutboundHandler (io.netty.channel.ChannelOutboundHandler)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 IOException (java.io.IOException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ParseException (java.text.ParseException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1