Search in sources :

Example 1 with AsyncWritableChannel

use of com.github.ambry.router.AsyncWritableChannel in project ambry by linkedin.

the class CopyForcingByteBuf method doHeaderAndContentSizeMismatchTest.

/**
 * Tests reaction of NettyRequest when content size is different from the size specified in the headers.
 * @param httpHeaders {@link HttpHeaders} that need to be a part of the request.
 * @param httpContents the {@link List<HttpContent>} that needs to be added to {@code nettyRequest}.
 * @throws Exception
 */
private void doHeaderAndContentSizeMismatchTest(HttpHeaders httpHeaders, List<HttpContent> httpContents) throws Exception {
    Channel channel = new MockChannel();
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", httpHeaders, channel);
    AsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    Future<Long> future = nettyRequest.readInto(writeChannel, callback);
    int bytesAdded = 0;
    HttpContent httpContentToAdd = null;
    for (HttpContent httpContent : httpContents) {
        httpContentToAdd = httpContent;
        int contentBytes = httpContentToAdd.content().readableBytes();
        if (!(httpContentToAdd instanceof LastHttpContent) && (bytesAdded + contentBytes <= nettyRequest.getSize())) {
            nettyRequest.addContent(httpContentToAdd);
            assertEquals("Reference count is not as expected", 2, httpContentToAdd.refCnt());
            bytesAdded += contentBytes;
        } else {
            break;
        }
    }
    // the addition of the next content should throw an exception.
    try {
        nettyRequest.addContent(httpContentToAdd);
        fail("Adding content should have failed because there was a mismatch in size");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    closeRequestAndValidate(nettyRequest, channel);
    writeChannel.close();
    verifyRefCnts(httpContents);
    callback.awaitCallback();
    assertNotNull("There should be a RestServiceException in the callback", callback.exception);
    assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, ((RestServiceException) callback.exception).getErrorCode());
    try {
        future.get();
        fail("Should have thrown exception because the future is expected to have been given one");
    } catch (ExecutionException e) {
        RestServiceException restServiceException = (RestServiceException) Utils.getRootCause(e);
        assertNotNull("There should be a RestServiceException in the future", restServiceException);
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, restServiceException.getErrorCode());
    }
}
Also used : AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) ExecutionException(java.util.concurrent.ExecutionException) 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)

Example 2 with AsyncWritableChannel

use of com.github.ambry.router.AsyncWritableChannel in project ambry by linkedin.

the class CopyForcingByteBuf method readIntoExceptionsTest.

/**
 * Tests exception scenarios of {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} and behavior of
 * {@link NettyRequest} when {@link AsyncWritableChannel} instances fail.
 * @throws Exception
 */
@Test
public void readIntoExceptionsTest() throws Exception {
    Channel channel = new MockChannel();
    // try to call readInto twice.
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    AsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    nettyRequest.readInto(writeChannel, null);
    try {
        nettyRequest.readInto(writeChannel, null);
        fail("Calling readInto twice should have failed");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    }
    closeRequestAndValidate(nettyRequest, channel);
    // write into a channel that throws exceptions
    // non RuntimeException
    nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    List<HttpContent> httpContents = new ArrayList<HttpContent>();
    generateContent(httpContents);
    assertTrue("Not enough content has been generated", httpContents.size() > 2);
    String expectedMsg = "@@expectedMsg@@";
    Exception exception = new Exception(expectedMsg);
    writeChannel = new BadAsyncWritableChannel(exception);
    ReadIntoCallback callback = new ReadIntoCallback();
    // add content initially
    int addedCount = 0;
    for (; addedCount < httpContents.size() / 2; addedCount++) {
        HttpContent httpContent = httpContents.get(addedCount);
        nettyRequest.addContent(httpContent);
        assertEquals("Reference count is not as expected", 2, httpContent.refCnt());
    }
    Future<Long> future = nettyRequest.readInto(writeChannel, callback);
    // add some more content
    for (; addedCount < httpContents.size(); addedCount++) {
        HttpContent httpContent = httpContents.get(addedCount);
        nettyRequest.addContent(httpContent);
    }
    writeChannel.close();
    verifyRefCnts(httpContents);
    callback.awaitCallback();
    assertNotNull("Exception was not piped correctly", callback.exception);
    assertEquals("Exception message mismatch (callback)", expectedMsg, callback.exception.getMessage());
    try {
        future.get();
        fail("Future should have thrown exception");
    } catch (ExecutionException e) {
        assertEquals("Exception message mismatch (future)", expectedMsg, Utils.getRootCause(e).getMessage());
    }
    closeRequestAndValidate(nettyRequest, channel);
    // RuntimeException
    // during readInto
    nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    httpContents = new ArrayList<HttpContent>();
    generateContent(httpContents);
    exception = new IllegalStateException(expectedMsg);
    writeChannel = new BadAsyncWritableChannel(exception);
    callback = new ReadIntoCallback();
    for (HttpContent httpContent : httpContents) {
        nettyRequest.addContent(httpContent);
        assertEquals("Reference count is not as expected", 2, httpContent.refCnt());
    }
    try {
        nettyRequest.readInto(writeChannel, callback);
        fail("readInto did not throw expected exception");
    } catch (Exception e) {
        assertEquals("Exception caught does not match expected exception", expectedMsg, e.getMessage());
    }
    writeChannel.close();
    closeRequestAndValidate(nettyRequest, channel);
    verifyRefCnts(httpContents);
    // after readInto
    nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    httpContents = new ArrayList<HttpContent>();
    generateContent(httpContents);
    exception = new IllegalStateException(expectedMsg);
    writeChannel = new BadAsyncWritableChannel(exception);
    callback = new ReadIntoCallback();
    nettyRequest.readInto(writeChannel, callback);
    // add content
    HttpContent httpContent = httpContents.get(1);
    try {
        nettyRequest.addContent(httpContent);
        fail("addContent did not throw expected exception");
    } catch (Exception e) {
        assertEquals("Exception caught does not match expected exception", expectedMsg, e.getMessage());
    }
    writeChannel.close();
    closeRequestAndValidate(nettyRequest, channel);
    verifyRefCnts(httpContents);
}
Also used : AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) TimeoutException(java.util.concurrent.TimeoutException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) 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) Test(org.junit.Test)

Example 3 with AsyncWritableChannel

use of com.github.ambry.router.AsyncWritableChannel in project ambry by linkedin.

the class CopyForcingByteBuf method setDigestAfterReadTest.

// digestIncorrectUsageTest() helpers.
/**
 * Tests for failure when {@link NettyRequest#setDigestAlgorithm(String)} after
 * {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} is called.
 * @throws NoSuchAlgorithmException
 * @throws RestServiceException
 */
private void setDigestAfterReadTest() throws NoSuchAlgorithmException, RestServiceException {
    List<HttpContent> httpContents = new ArrayList<HttpContent>();
    generateContent(httpContents);
    Channel channel = new MockChannel();
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    ByteBufferAsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    nettyRequest.readInto(writeChannel, callback);
    try {
        nettyRequest.setDigestAlgorithm("MD5");
        fail("Setting a digest algorithm should have failed because readInto() has already been called");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    }
    writeChannel.close();
    closeRequestAndValidate(nettyRequest, channel);
}
Also used : AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) 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)

Example 4 with AsyncWritableChannel

use of com.github.ambry.router.AsyncWritableChannel in project ambry by linkedin.

the class NettyMultipartRequestTest method readIntoExceptionsTest.

/**
 * Tests exception scenarios of {@link NettyMultipartRequest#readInto(AsyncWritableChannel, Callback)}.
 * @throws Exception
 */
@Test
public void readIntoExceptionsTest() throws Exception {
    // most tests are in NettyRequest. Adding tests for differing code in NettyMultipartRequest
    // try to call readInto twice.
    NettyMultipartRequest request = createRequest(null, null);
    AsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    request.prepare();
    request.readInto(writeChannel, null);
    try {
        request.readInto(writeChannel, null);
        fail("Calling readInto twice should have failed");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    } finally {
        closeRequestAndValidate(request);
    }
    // call readInto when not ready for read.
    request = createRequest(null, null);
    writeChannel = new ByteBufferAsyncWritableChannel();
    try {
        request.readInto(writeChannel, null);
        fail("Calling readInto without calling prepare() should have failed");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    } finally {
        closeRequestAndValidate(request);
    }
}
Also used : RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) Test(org.junit.Test)

Example 5 with AsyncWritableChannel

use of com.github.ambry.router.AsyncWritableChannel in project ambry by linkedin.

the class CopyForcingByteBuf method operationsAfterCloseTest.

/**
 * Tests for behavior of multiple operations after {@link NettyRequest#close()} has been called. Some should be ok to
 * do and some should throw exceptions.
 * @throws Exception
 */
@Test
public void operationsAfterCloseTest() throws Exception {
    Channel channel = new MockChannel();
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    closeRequestAndValidate(nettyRequest, channel);
    // operations that should be ok to do (does not include all operations).
    nettyRequest.close();
    // operations that will throw exceptions.
    AsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    try {
        nettyRequest.readInto(writeChannel, callback).get();
        fail("Request channel has been closed, so read should have thrown ClosedChannelException");
    } catch (ExecutionException e) {
        Exception exception = (Exception) Utils.getRootCause(e);
        assertTrue("Exception is not ClosedChannelException", exception instanceof ClosedChannelException);
        callback.awaitCallback();
        assertEquals("Exceptions of callback and future differ", exception.getMessage(), callback.exception.getMessage());
    }
    try {
        byte[] content = TestUtils.getRandomBytes(1024);
        nettyRequest.addContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content)));
        fail("Request channel has been closed, so addContent() should have thrown ClosedChannelException");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed, e.getErrorCode());
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) AsyncWritableChannel(com.github.ambry.router.AsyncWritableChannel) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) ExecutionException(java.util.concurrent.ExecutionException) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) TimeoutException(java.util.concurrent.TimeoutException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ByteBufferAsyncWritableChannel (com.github.ambry.commons.ByteBufferAsyncWritableChannel)7 AsyncWritableChannel (com.github.ambry.router.AsyncWritableChannel)7 Channel (io.netty.channel.Channel)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)6 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)5 HttpContent (io.netty.handler.codec.http.HttpContent)5 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)5 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CertificateException (java.security.cert.CertificateException)2 TimeoutException (java.util.concurrent.TimeoutException)2 SSLException (javax.net.ssl.SSLException)2 RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)1 MessageDigest (java.security.MessageDigest)1