List of usage examples for io.netty.handler.codec.http HttpContent refCnt
int refCnt();
From source file:com.github.ambry.rest.NettyMultipartRequestTest.java
License:Open Source License
/** * Tests that reference counts are correct when a {@link NettyMultipartRequest} is closed without being read. * @throws Exception//from w w w. j ava2 s .co m */ @Test public void refCountsAfterCloseTest() throws Exception { NettyMultipartRequest requestCloseBeforePrepare = createRequest(null, null); NettyMultipartRequest requestCloseAfterPrepare = createRequest(null, null); List<HttpContent> httpContents = new ArrayList<HttpContent>(5); for (int i = 0; i < 5; i++) { HttpContent httpContent = new DefaultHttpContent( Unpooled.wrappedBuffer(RestTestUtils.getRandomBytes(10))); requestCloseBeforePrepare.addContent(httpContent); requestCloseAfterPrepare.addContent(httpContent); assertEquals("Reference count is not as expected", 3, httpContent.refCnt()); httpContents.add(httpContent); } closeRequestAndValidate(requestCloseBeforePrepare); requestCloseAfterPrepare.prepare(); closeRequestAndValidate(requestCloseAfterPrepare); for (HttpContent httpContent : httpContents) { assertEquals("Reference count is not as expected", 1, httpContent.refCnt()); } }
From source file:com.github.ambry.rest.NettyMultipartRequestTest.java
License:Open Source License
/** * Tests different scenarios with {@link NettyMultipartRequest#prepare()}. * Currently tests://from w ww. j ava2 s . co m * 1. Idempotency of {@link NettyMultipartRequest#prepare()}. * 2. Exception scenarios of {@link NettyMultipartRequest#prepare()}. * @throws Exception */ @Test public void prepareTest() throws Exception { // prepare half baked data HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); HttpPostRequestEncoder encoder = createEncoder(httpRequest, null); NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), nettyMetrics); assertTrue("Request channel is not open", request.isOpen()); // insert random data HttpContent httpContent = new DefaultHttpContent(Unpooled.wrappedBuffer(RestTestUtils.getRandomBytes(10))); request.addContent(httpContent); // prepare should fail try { request.prepare(); fail("Preparing request should have failed"); } catch (HttpPostRequestDecoder.NotEnoughDataDecoderException e) { assertEquals("Reference count is not as expected", 1, httpContent.refCnt()); } finally { closeRequestAndValidate(request); } // more than one blob part HttpHeaders httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(RestUtils.Headers.BLOB_SIZE, 256); InMemoryFile[] files = new InMemoryFile[2]; files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); files[1] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); request = createRequest(httpHeaders, files); assertEquals("Request size does not match", 256, request.getSize()); try { request.prepare(); fail("Prepare should have failed because there was more than one " + RestUtils.MultipartPost.BLOB_PART); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MalformedRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } // more than one part named "part-1" files = new InMemoryFile[2]; files[0] = new InMemoryFile("Part-1", ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); files[1] = new InMemoryFile("Part-1", ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); request = createRequest(null, files); try { request.prepare(); fail("Prepare should have failed because there was more than one part named Part-1"); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MalformedRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } // size of blob does not match the advertized size httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(RestUtils.Headers.BLOB_SIZE, 256); files = new InMemoryFile[1]; files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(128))); request = createRequest(httpHeaders, files); try { request.prepare(); fail("Prepare should have failed because there was more than one " + RestUtils.MultipartPost.BLOB_PART); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } // non fileupload (file attribute present) httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); HttpHeaders.setHeader(httpRequest, RestUtils.Headers.BLOB_SIZE, 256); files = new InMemoryFile[1]; files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); encoder = createEncoder(httpRequest, files); encoder.addBodyAttribute("dummyKey", "dummyValue"); request = new NettyMultipartRequest(encoder.finalizeRequest(), nettyMetrics); assertTrue("Request channel is not open", request.isOpen()); while (!encoder.isEndOfInput()) { // Sending null for ctx because the encoder is OK with that. request.addContent(encoder.readChunk(null)); } try { request.prepare(); fail("Prepare should have failed because there was non fileupload"); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } }
From source file:com.github.ambry.rest.NettyRequestTest.java
License:Open Source License
/** * Tests {@link NettyRequest#addContent(HttpContent)} and * {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} by creating a {@link NettyRequest}, adding a few * pieces of content to it and then reading from it to match the stream with the added content. * <p/>//from w w w . j a v a2s. c om * The read happens at different points of time w.r.t content addition (before, during, after). * @throws Exception */ @Test public void contentAddAndReadTest() throws Exception { // start reading before content added NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); List<HttpContent> httpContents = new ArrayList<HttpContent>(); ByteBuffer content = generateContent(httpContents); ByteBufferAsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel(); ReadIntoCallback callback = new ReadIntoCallback(); Future<Long> future = nettyRequest.readInto(writeChannel, callback); for (HttpContent httpContent : httpContents) { nettyRequest.addContent(httpContent); assertEquals("Reference count is not as expected", 2, httpContent.refCnt()); } readAndVerify(content.limit(), writeChannel, content); verifyRefCnts(httpContents); writeChannel.close(); 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); closeRequestAndValidate(nettyRequest); // start reading in the middle of content add nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); httpContents = new ArrayList<HttpContent>(); content = generateContent(httpContents); writeChannel = new ByteBufferAsyncWritableChannel(); callback = new ReadIntoCallback(); // add content initially int bytesToVerify = 0; int addedCount = 0; for (; addedCount < httpContents.size() / 2; addedCount++) { HttpContent httpContent = httpContents.get(addedCount); bytesToVerify += httpContent.content().readableBytes(); nettyRequest.addContent(httpContent); assertEquals("Reference count is not as expected", 2, httpContent.refCnt()); } future = nettyRequest.readInto(writeChannel, callback); readAndVerify(bytesToVerify, writeChannel, content); // add some more 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(); if (callback.exception != null) { throw callback.exception; } 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); closeRequestAndValidate(nettyRequest); // start reading after all content added nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); httpContents = new ArrayList<HttpContent>(); content = generateContent(httpContents); writeChannel = new ByteBufferAsyncWritableChannel(); callback = new ReadIntoCallback(); for (HttpContent httpContent : httpContents) { nettyRequest.addContent(httpContent); assertEquals("Reference count is not as expected", 2, httpContent.refCnt()); } future = nettyRequest.readInto(writeChannel, callback); readAndVerify(content.limit(), writeChannel, content); verifyRefCnts(httpContents); writeChannel.close(); if (callback.exception != null) { throw callback.exception; } 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); closeRequestAndValidate(nettyRequest); }
From source file:com.github.ambry.rest.NettyRequestTest.java
License:Open Source License
/** * Tests exception scenarios of {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} and behavior of * {@link NettyRequest} when {@link AsyncWritableChannel} instances fail. * @throws InterruptedException//from w w w . jav a 2 s . c o m * @throws IOException * @throws RestServiceException */ @Test public void readIntoExceptionsTest() throws InterruptedException, IOException, RestServiceException { // try to call readInto twice. NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); 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. } // write into a channel that throws exceptions // non RuntimeException nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); 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); 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); // RuntimeException // during readInto nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); 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); verifyRefCnts(httpContents); // after readInto nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); 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); verifyRefCnts(httpContents); }
From source file:com.github.ambry.rest.NettyRequestTest.java
License:Open Source License
/** * Tests for POST request that has no content. * @throws Exception//from w w w . j ava 2 s. co m */ @Test public void zeroSizeContentTest() throws Exception { NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", null); HttpContent httpContent = new DefaultLastHttpContent(); nettyRequest.addContent(httpContent); 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); assertEquals("There should be no content", 0, writeChannel.getNextChunk().remaining()); writeChannel.resolveOldestChunk(null); closeRequestAndValidate(nettyRequest); writeChannel.close(); assertEquals("Reference count of http content has changed", 1, httpContent.refCnt()); if (callback.exception != null) { throw callback.exception; } long futureBytesRead = future.get(); assertEquals("Total bytes read does not match (callback)", 0, callback.bytesRead); assertEquals("Total bytes read does not match (future)", 0, futureBytesRead); }
From source file:com.github.ambry.rest.NettyRequestTest.java
License:Open Source License
/** * Verifies that the reference counts of {@code httpContents} is undisturbed after all operations. * @param httpContents the {@link List<HttpContent>} of contents whose reference counts need to checked. *///from w w w . jav a 2 s . c o m private void verifyRefCnts(List<HttpContent> httpContents) { for (HttpContent httpContent : httpContents) { assertEquals("Reference count of http content has changed", 1, httpContent.refCnt()); } }
From source file:com.github.ambry.rest.NettyRequestTest.java
License:Open Source License
/** * Tests reaction of NettyRequest when content size is different from the size specified in the headers. * @param nettyRequest the {@link NettyRequest} to which content will be added. * @param httpContents the {@link List<HttpContent>} that needs to be added to {@code nettyRequest}. * @throws InterruptedException//from w ww. j a va 2 s . c om * @throws IOException * @throws RestServiceException */ private void doHeaderAndContentSizeMismatchTest(NettyRequest nettyRequest, List<HttpContent> httpContents) throws InterruptedException, IOException, RestServiceException { 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); writeChannel.close(); verifyRefCnts(httpContents); 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()); } }
From source file:org.ballerinalang.net.grpc.MessageDeframer.java
License:Open Source License
public void deframe(HttpContent data) { if (data == null) { throw new RuntimeException("Data buffer is null"); }//ww w . j a v a 2s. c om boolean needToCloseData = true; try { if (!isClosedOrScheduledToClose()) { unprocessed.addBuffer(data.content()); needToCloseData = false; deliver(); } } finally { if (needToCloseData && data.refCnt() != 0) { data.release(); } } }