Search in sources :

Example 6 with AsyncWritableChannel

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

the class CopyForcingByteBuf method doContentAddAndReadTest.

/**
 * Does the content addition and read verification based on the arguments provided.
 * @param digestAlgorithm the digest algorithm to use. Can be empty or {@code null} if digest checking is not
 *                        required.
 * @param content the complete content.
 * @param httpContents {@code content} in parts and as {@link HttpContent}. Should contain all the data in
 * {@code content}.
 * @param numChunksToAddBeforeRead the number of {@link HttpContent} to add before making the
 * {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} call.
 * @param method Http method
 * @throws Exception
 */
private void doContentAddAndReadTest(String digestAlgorithm, ByteBuffer content, List<HttpContent> httpContents, int numChunksToAddBeforeRead, HttpMethod method) throws Exception {
    if (numChunksToAddBeforeRead < 0 || numChunksToAddBeforeRead > httpContents.size()) {
        throw new IllegalArgumentException("Illegal value of numChunksToAddBeforeRead");
    }
    Channel channel = new MockChannel();
    NettyRequest nettyRequest = createNettyRequest(method, "/", null, channel);
    byte[] wholeDigest = null;
    if (digestAlgorithm != null && !digestAlgorithm.isEmpty()) {
        MessageDigest digest = MessageDigest.getInstance(digestAlgorithm);
        digest.update(content);
        wholeDigest = digest.digest();
        content.rewind();
        nettyRequest.setDigestAlgorithm(digestAlgorithm);
    }
    int bytesToVerify = 0;
    int addedCount = 0;
    for (; addedCount < numChunksToAddBeforeRead; addedCount++) {
        HttpContent httpContent = httpContents.get(addedCount);
        bytesToVerify += httpContent.content().readableBytes();
        nettyRequest.addContent(httpContent);
        // ref count always 2 when added before calling readInto()
        assertEquals("Reference count is not as expected", 2, httpContent.refCnt());
    }
    ByteBufferAsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    Future<Long> future = nettyRequest.readInto(writeChannel, callback);
    readAndVerify(bytesToVerify, writeChannel, content);
    bytesToVerify = 0;
    for (; addedCount < httpContents.size(); addedCount++) {
        HttpContent httpContent = httpContents.get(addedCount);
        bytesToVerify += httpContent.content().readableBytes();
        nettyRequest.addContent(httpContent);
        assertEquals("Reference count is not as expected", 2, httpContent.refCnt());
    }
    readAndVerify(bytesToVerify, writeChannel, content);
    verifyRefCnts(httpContents);
    writeChannel.close();
    callback.awaitCallback();
    if (callback.exception != null) {
        throw callback.exception;
    }
    long futureBytesRead = future.get();
    assertEquals("Total bytes read does not match (callback)", content.limit(), callback.bytesRead);
    assertEquals("Total bytes read does not match (future)", content.limit(), futureBytesRead);
    assertEquals("nettyRequest.getBytesReceived() does not match expected", content.limit(), nettyRequest.getBytesReceived());
    // check twice to make sure the same digest is returned every time
    for (int i = 0; i < 2; i++) {
        assertArrayEquals("Part by part digest should match digest of whole", wholeDigest, nettyRequest.getDigest());
    }
    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) ByteBufferAsyncWritableChannel(com.github.ambry.commons.ByteBufferAsyncWritableChannel) MessageDigest(java.security.MessageDigest) 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 7 with AsyncWritableChannel

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

the class CopyForcingByteBuf method getDigestBeforeAllContentProcessedTest.

/**
 * Tests for failure when {@link NettyRequest#getDigest()} is called before
 * 1. All content is added.
 * 2. All content is processed (i.e. before a call to {@link NettyRequest#readInto(AsyncWritableChannel, Callback)}).
 * @throws NoSuchAlgorithmException
 * @throws RestServiceException
 */
private void getDigestBeforeAllContentProcessedTest() throws NoSuchAlgorithmException, RestServiceException {
    // all content not added test.
    List<HttpContent> httpContents = new ArrayList<HttpContent>();
    generateContent(httpContents);
    Channel channel = new MockChannel();
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    nettyRequest.setDigestAlgorithm("MD5");
    // add all except the LastHttpContent
    for (int i = 0; i < httpContents.size() - 1; i++) {
        nettyRequest.addContent(httpContents.get(i));
    }
    ByteBufferAsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    nettyRequest.readInto(writeChannel, callback);
    try {
        nettyRequest.getDigest();
        fail("Getting a digest should have failed because all the content has not been added");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    }
    closeRequestAndValidate(nettyRequest, channel);
    // content not processed test.
    httpContents.clear();
    generateContent(httpContents);
    nettyRequest = createNettyRequest(HttpMethod.POST, "/", null, channel);
    nettyRequest.setDigestAlgorithm("MD5");
    for (HttpContent httpContent : httpContents) {
        nettyRequest.addContent(httpContent);
    }
    try {
        nettyRequest.getDigest();
        fail("Getting a digest should have failed because the content has not been processed (readInto() not called)");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    }
    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)

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