use of com.github.ambry.commons.ByteBufferAsyncWritableChannel in project ambry by linkedin.
the class NettyMultipartRequestTest method operationsAfterCloseTest.
/**
* Tests the expected behavior of operations after {@link NettyMultipartRequest#close()} has been called.
* @throws Exception
*/
@Test
public void operationsAfterCloseTest() throws Exception {
NettyMultipartRequest request = createRequest(null, null);
request.prepare();
closeRequestAndValidate(request);
// close should be idempotent.
request.close();
try {
request.readInto(new ByteBufferAsyncWritableChannel(), null).get();
fail("Reading should have failed because request is closed");
} catch (ExecutionException e) {
assertEquals("Unexpected exception", ClosedChannelException.class, Utils.getRootCause(e).getClass());
}
try {
request.prepare();
fail("Preparing should have failed because request is closed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed, e.getErrorCode());
}
try {
request.addContent(new DefaultHttpContent(Unpooled.wrappedBuffer(TestUtils.getRandomBytes(10))));
fail("Content addition should have failed because request is closed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestChannelClosed, e.getErrorCode());
}
}
use of com.github.ambry.commons.ByteBufferAsyncWritableChannel 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());
}
}
use of com.github.ambry.commons.ByteBufferAsyncWritableChannel 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);
}
use of com.github.ambry.commons.ByteBufferAsyncWritableChannel 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);
}
use of com.github.ambry.commons.ByteBufferAsyncWritableChannel in project ambry by linkedin.
the class CopyForcingByteBuf method doBackPressureTest.
/**
* Does the backpressure test by ensuring that {@link Channel#read()} isn't called when the number of bytes buffered
* is above the {@link NettyRequest#bufferWatermark}. Also ensures that {@link Channel#read()} is called correctly
* when the number of buffered bytes falls below the {@link NettyRequest#bufferWatermark}.
* @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 doBackPressureTest(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");
}
MockChannel channel = new MockChannel();
final 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);
}
final AtomicInteger queuedReads = new AtomicInteger(0);
ByteBufferAsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
ReadIntoCallback callback = new ReadIntoCallback();
channel.setChannelReadCallback(new MockChannel.ChannelReadCallback() {
@Override
public void onRead() {
queuedReads.incrementAndGet();
}
});
int addedCount = 0;
Future<Long> future = null;
boolean suspended = false;
int bytesToVerify = 0;
while (addedCount < httpContents.size()) {
if (suspended) {
assertEquals("There should have been no reads queued when over buffer watermark", 0, queuedReads.get());
if (future == null) {
future = nettyRequest.readInto(writeChannel, callback);
}
int chunksRead = readAndVerify(bytesToVerify, writeChannel, content);
assertEquals("Number of reads triggered is not as expected", chunksRead, queuedReads.get());
// collapse many reads into one
queuedReads.set(1);
bytesToVerify = 0;
suspended = false;
} else {
assertEquals("There should have been only one read queued", 1, queuedReads.get());
queuedReads.set(0);
if (future == null && addedCount == numChunksToAddBeforeRead) {
future = nettyRequest.readInto(writeChannel, callback);
}
final HttpContent httpContent = httpContents.get(addedCount);
bytesToVerify += (httpContent.content().readableBytes());
suspended = bytesToVerify >= NettyRequest.bufferWatermark;
addedCount++;
nettyRequest.addContent(httpContent);
assertEquals("Reference count is not as expected", 2, httpContent.refCnt());
}
}
if (future == null) {
future = nettyRequest.readInto(writeChannel, callback);
}
readAndVerify(bytesToVerify, writeChannel, content);
verifyRefCnts(httpContents);
writeChannel.close();
callback.awaitCallback();
if (callback.exception != null) {
throw callback.exception;
}
long futureBytesRead = future.get(1, TimeUnit.SECONDS);
assertEquals("Total bytes read does not match (callback)", content.limit(), callback.bytesRead);
assertEquals("Total bytes read does not match (future)", content.limit(), futureBytesRead);
// 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);
}
Aggregations