Search in sources :

Example 46 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel 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)

Example 47 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.

the class ChannelWriteCallback method headersPresenceTest.

/**
 * Sends a request with certain headers that will copied into the response. Checks the response for those headers to
 * see that values match.
 * @throws ParseException
 */
@Test
public void headersPresenceTest() throws ParseException {
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.CopyHeaders.toString());
    HttpUtil.setKeepAlive(request, false);
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertFalse("Channel not closed on the server", channel.isActive());
    checkHeaders(request, response);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 48 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.

the class ChannelWriteCallback method errorResponseTest.

/**
 * Tests that error responses are correctly formed.
 */
@Test
public void errorResponseTest() {
    EmbeddedChannel channel = createEmbeddedChannel();
    for (RestServiceErrorCode errorCode : RestServiceErrorCode.values()) {
        HttpHeaders httpHeaders = new DefaultHttpHeaders();
        httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, errorCode);
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.HEAD, TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders));
        HttpResponse response = channel.readOutbound();
        HttpResponseStatus expectedStatus = getExpectedHttpResponseStatus(errorCode);
        assertEquals("Unexpected response status", expectedStatus, response.status());
        boolean containsFailureReasonHeader = response.headers().contains(NettyResponseChannel.FAILURE_REASON_HEADER);
        if (expectedStatus == HttpResponseStatus.BAD_REQUEST) {
            assertTrue("Could not find failure reason header.", containsFailureReasonHeader);
        } else {
            assertFalse("Should not have found failure reason header.", containsFailureReasonHeader);
        }
        if (HttpStatusClass.CLIENT_ERROR.contains(response.status().code())) {
            assertEquals("Wrong error code", errorCode, RestServiceErrorCode.valueOf(response.headers().get(NettyResponseChannel.ERROR_CODE_HEADER)));
        } else {
            assertFalse("Should not have found error code header", response.headers().contains(NettyResponseChannel.ERROR_CODE_HEADER));
        }
        if (response instanceof FullHttpResponse) {
            // assert that there is no content
            assertEquals("The response should not contain content", 0, ((FullHttpResponse) response).content().readableBytes());
        } else {
            HttpContent content = channel.readOutbound();
            assertTrue("End marker should be received", content instanceof LastHttpContent);
        }
        assertNull("There should be no more data in the channel", channel.readOutbound());
        boolean shouldBeAlive = !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(expectedStatus);
        assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
        assertEquals("Connection header should be consistent with channel state", shouldBeAlive, HttpUtil.isKeepAlive(response));
        if (!shouldBeAlive) {
            channel = createEmbeddedChannel();
        }
    }
    channel.close();
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 49 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.

the class ChannelWriteCallback method createEmbeddedChannel.

/**
 * Creates a new {@link EmbeddedChannel} with a {@link ChunkedWriteHandler} and {@link MockNettyMessageProcessor} in
 * the pipeline.
 * @return the created {@link EmbeddedChannel}.
 */
private EmbeddedChannel createEmbeddedChannel() {
    ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    return new EmbeddedChannel(chunkedWriteHandler, processor);
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 50 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.

the class ChannelWriteCallback method setStatusTest.

/**
 * Tests setting of different available {@link ResponseStatus} codes and sees that they are recognized and converted
 * in {@link NettyResponseChannel}.
 * <p/>
 * If this test fails, a case for conversion probably needs to be added in {@link NettyResponseChannel}.
 */
@Test
public void setStatusTest() {
    // ask for every status to be set
    for (ResponseStatus expectedResponseStatus : ResponseStatus.values()) {
        HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.SetStatus.toString());
        request.headers().set(MockNettyMessageProcessor.STATUS_HEADER_NAME, expectedResponseStatus);
        HttpUtil.setKeepAlive(request, false);
        EmbeddedChannel channel = createEmbeddedChannel();
        channel.writeInbound(request);
        // pull but discard response
        channel.readOutbound();
        assertFalse("Channel not closed on the server", channel.isActive());
    }
    // check if all the ResponseStatus codes were recognized.
    String metricName = MetricRegistry.name(NettyResponseChannel.class, "UnknownResponseStatusCount");
    long metricCount = MockNettyMessageProcessor.METRIC_REGISTRY.getCounters().get(metricName).getCount();
    assertEquals("Some of the ResponseStatus codes were not recognized", 0, metricCount);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1027 Test (org.junit.jupiter.api.Test)515 ByteBuf (io.netty.buffer.ByteBuf)356 Test (org.junit.Test)342 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)85 HttpResponse (io.netty.handler.codec.http.HttpResponse)73 HttpRequest (io.netty.handler.codec.http.HttpRequest)69 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)64 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)60 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)55 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)50 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)49 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)46 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)42 IOException (java.io.IOException)38 InetSocketAddress (java.net.InetSocketAddress)38 Executable (org.junit.jupiter.api.function.Executable)36 ArrayList (java.util.ArrayList)35 Before (org.junit.Before)32 ChannelHandler (io.netty.channel.ChannelHandler)27