Search in sources :

Example 51 with EmbeddedChannel

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

the class ChannelWriteCallback method closeTest.

/**
 * Tests that the underlying network channel is closed when {@link NettyResponseChannel#close()} is called.
 */
@Test
public void closeTest() {
    // request is keep-alive by default.
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.Close.toString());
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.INTERNAL_SERVER_ERROR, response.status());
    assertFalse("Inconsistent value for Connection header", HttpUtil.isKeepAlive(response));
    // drain the channel of content.
    while (channel.readOutbound() != null) {
    }
    assertFalse("Channel should be closed", channel.isOpen());
}
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 52 with EmbeddedChannel

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

the class ChannelWriteCallback method doBadStateTransitionTest.

// badStateTransitionsTest() helpers
/**
 * Creates a channel and sends the request to the {@link EmbeddedChannel}. Checks for an exception and verifies the
 * exception class matches. If {@code exceptionClass} is {@link RestServiceException}, then checks the provided
 * {@link RestServiceErrorCode}.
 * @param uri the uri to hit.
 * @param exceptionClass the class of the exception expected.
 * @throws Exception
 */
private void doBadStateTransitionTest(TestingUri uri, Class exceptionClass) throws Exception {
    EmbeddedChannel channel = createEmbeddedChannel();
    MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    try {
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, uri.toString(), null));
        verifyCallbacks(processor);
        fail("This test was expecting the handler in the channel to throw an exception");
    } catch (Exception e) {
        if (!exceptionClass.isInstance(e)) {
            throw e;
        }
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TimeoutException(java.util.concurrent.TimeoutException) ParseException(java.text.ParseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 53 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jackrabbit-oak by apache.

the class GetBlobResponseEncoderTest method shouldEncodeOneChunkResponse.

@Test
public void shouldEncodeOneChunkResponse() throws Exception {
    byte[] blobData = new byte[] { 1, 2, 3 };
    String blobId = "blobId";
    byte mask = createMask(1, 1);
    EmbeddedChannel channel = new EmbeddedChannel(new ChunkedWriteHandler(), new GetBlobResponseEncoder(3));
    channel.writeOutbound(new GetBlobResponse("clientId", blobId, new ByteArrayInputStream(blobData), blobData.length));
    ByteBuf buffer = (ByteBuf) channel.readOutbound();
    ByteBuf expected = createBlobChunkBuffer(Messages.HEADER_BLOB, 3L, blobId, blobData, mask);
    assertEquals(expected, buffer);
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 54 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jackrabbit-oak by apache.

the class ResponseDecoderTest method shouldDecodeValidOneChunkGetBlobResponses.

@Test
public void shouldDecodeValidOneChunkGetBlobResponses() throws Exception {
    byte[] blobData = new byte[] { 1, 2, 3 };
    String blobId = "blobId";
    byte mask = createMask(1, 1);
    ByteBuf buf = createBlobChunkBuffer(Messages.HEADER_BLOB, 3L, blobId, blobData, mask);
    EmbeddedChannel channel = new EmbeddedChannel(new ResponseDecoder(folder.newFolder()));
    channel.writeInbound(buf);
    GetBlobResponse response = (GetBlobResponse) channel.readInbound();
    assertEquals("blobId", response.getBlobId());
    assertEquals(blobData.length, response.getLength());
    try (InputStream is = response.getInputStream()) {
        byte[] receivedData = IOUtils.toByteArray(is);
        assertArrayEquals(blobData, receivedData);
    }
}
Also used : InputStream(java.io.InputStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 55 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jackrabbit-oak by apache.

the class ResponseDecoderTest method shouldDropInvalidGetBlobResponses.

@Test
public void shouldDropInvalidGetBlobResponses() throws Exception {
    byte[] blobData = new byte[] { 1, 2, 3 };
    String blobId = "blobId";
    byte[] blobIdBytes = blobId.getBytes(Charsets.UTF_8);
    byte mask = createMask(1, 1);
    ByteBuf buf = Unpooled.buffer();
    buf.writeInt(1 + 1 + 8 + 4 + blobIdBytes.length + 8 + blobData.length);
    buf.writeByte(Messages.HEADER_BLOB);
    buf.writeByte(mask);
    buf.writeLong(3L);
    buf.writeInt(blobIdBytes.length);
    buf.writeBytes(blobIdBytes);
    buf.writeLong(hash(mask, 3L, blobData) + 1);
    buf.writeBytes(blobData);
    EmbeddedChannel channel = new EmbeddedChannel(new ResponseDecoder(folder.newFolder()));
    channel.writeInbound(buf);
    assertNull(channel.readInbound());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) 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