use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project ambry by linkedin.
the class MockChannelHandlerContext method fillWriteBufferTest.
/**
* Tests handling of content that is larger than write buffer size. In this test case, the write buffer low and high
* watermarks are requested to be set to 1 and 2 respectively so the content will be written byte by byte into the
* {@link NettyResponseChannel}. This does <b><i>not</i></b> test for the same situation in a async scenario since
* {@link EmbeddedChannel} only provides blocking semantics.
* @throws IOException
*/
@Test
public void fillWriteBufferTest() throws IOException {
String content = "@@randomContent@@@";
String lastContent = "@@randomLastContent@@@";
EmbeddedChannel channel = createEmbeddedChannel();
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.GET, TestingUri.FillWriteBuffer.toString(), null);
HttpUtil.setKeepAlive(httpRequest, false);
channel.writeInbound(httpRequest);
channel.writeInbound(createContent(content, false));
channel.writeInbound(createContent(lastContent, true));
// first outbound has to be response.
HttpResponse response = channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
// content echoed back.
StringBuilder returnedContent = new StringBuilder();
while (returnedContent.length() < content.length()) {
HttpContent responseContent = channel.readOutbound();
returnedContent.append(RestTestUtils.getContentString(responseContent));
responseContent.release();
}
assertEquals("Content does not match with expected content", content, returnedContent.toString());
// last content echoed back.
StringBuilder returnedLastContent = new StringBuilder();
while (returnedLastContent.length() < lastContent.length()) {
HttpContent responseContent = channel.readOutbound();
returnedLastContent.append(RestTestUtils.getContentString(responseContent));
responseContent.release();
}
assertEquals("Content does not match with expected content", lastContent, returnedLastContent.toString());
assertFalse("Channel not closed on the server", channel.isActive());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project ambry by linkedin.
the class MockChannelHandlerContext method responsesWithTransferEncodingChunkedTest.
/**
* Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via
* {@link NettyResponseChannel#write(ByteBuf, Callback)} and then complete the response.
* <p/>
* These responses have the header Transfer-Encoding set to chunked.
* @throws Exception
*/
@Test
public void responsesWithTransferEncodingChunkedTest() throws Exception {
String content = "@@randomContent@@@";
String lastContent = "@@randomLastContent@@@";
EmbeddedChannel channel = createEmbeddedChannel();
MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
AtomicLong contentIdGenerator = new AtomicLong(0);
final int ITERATIONS = 10;
for (int i = 0; i < ITERATIONS; i++) {
boolean isKeepAlive = i != (ITERATIONS - 1);
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
channel.writeInbound(httpRequest);
ArrayList<String> contents = new ArrayList<>();
for (int j = 0; j <= i; j++) {
String contentToSend = content + contentIdGenerator.getAndIncrement();
channel.writeInbound(createContent(contentToSend, false));
contents.add(contentToSend);
}
channel.writeInbound(createContent(lastContent, true));
verifyCallbacks(processor);
// first outbound has to be response.
HttpResponse response = channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
assertTrue("Response must say 'Transfer-Encoding : chunked'", HttpUtil.isTransferEncodingChunked(response));
// content echoed back.
for (String srcOfTruth : contents) {
HttpContent responseContent = channel.readOutbound();
String returnedContent = RestTestUtils.getContentString(responseContent);
responseContent.release();
assertEquals("Content does not match with expected content", srcOfTruth, returnedContent);
}
HttpContent responseContent = channel.readOutbound();
// last content echoed back.
String returnedContent = RestTestUtils.getContentString(responseContent);
responseContent.release();
assertEquals("Content does not match with expected content", lastContent, returnedContent);
// When the Transfer-Encoding is Chunked, response channel would send the last http content in the request as
// regular http content and then send an empty last http content after that.
assertTrue("Did not receive end marker", channel.readOutbound() instanceof LastHttpContent);
assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive());
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project ambry by linkedin.
the class MockChannelHandlerContext method sendRequestAndEvaluateResponsePerformance.
/**
* Send the curated request and verify that the response and satisfaction are expected.
* @param httpRequest the request to be handled by {@link MockNettyMessageProcessor}
* @param requestContent the content needed for POST request
* @param expectedStatus the expected {@link HttpResponseStatus}
* @param shouldBeSatisfied whether the request should be satisfied or not
* @throws IOException
*/
private void sendRequestAndEvaluateResponsePerformance(HttpRequest httpRequest, String requestContent, HttpResponseStatus expectedStatus, boolean shouldBeSatisfied) throws IOException {
EmbeddedChannel channel = createEmbeddedChannel();
channel.pipeline().get(MockNettyMessageProcessor.class);
channel.writeInbound(httpRequest);
if (requestContent != null) {
channel.writeInbound(createContent(requestContent, true));
}
HttpResponse response = channel.readOutbound();
assertEquals("Unexpected response status", expectedStatus, response.status());
if (requestContent != null) {
HttpContent responseContent = channel.readOutbound();
String returnedContent = RestTestUtils.getContentString(responseContent);
responseContent.release();
assertEquals("Content does not match with expected content", requestContent, returnedContent);
}
if (shouldBeSatisfied) {
assertTrue(httpRequest.method() + " request should be satisfied", MockNettyRequest.mockTracker.isSatisfied());
} else {
assertFalse(httpRequest.method() + " request should be unsatisfied", MockNettyRequest.mockTracker.isSatisfied());
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project ambry by linkedin.
the class NettyRequest method readInto.
@Override
public Future<Long> readInto(AsyncWritableChannel asyncWritableChannel, Callback<Long> callback) {
ReadIntoCallbackWrapper tempWrapper = new ReadIntoCallbackWrapper(callback);
contentLock.lock();
try {
if (!isOpen()) {
nettyMetrics.requestAlreadyClosedError.inc();
tempWrapper.invokeCallback(new ClosedChannelException());
} else if (writeChannel != null) {
throw new IllegalStateException("ReadableStreamChannel cannot be read more than once");
}
HttpContent content = requestContents.poll();
while (content != null) {
try {
writeContent(asyncWritableChannel, tempWrapper, content);
} finally {
ReferenceCountUtil.release(content);
}
content = requestContents.poll();
}
callbackWrapper = tempWrapper;
writeChannel = asyncWritableChannel;
} finally {
contentLock.unlock();
}
return tempWrapper.futureResult;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project ambry by linkedin.
the class NettyMultipartRequest method readInto.
/**
* {@inheritDoc}
* @param asyncWritableChannel the {@link AsyncWritableChannel} to read the data into.
* @param callback the {@link Callback} that will be invoked either when all the data in the channel has been emptied
* into the {@code asyncWritableChannel} or if there is an exception in doing so. This can be null.
* @return the {@link Future} that will eventually contain the result of the operation.
* @throws IllegalStateException if an attempt is made to read the channel before calling {@link #prepare()} or if
* this function is called more than once.
*/
@Override
public Future<Long> readInto(AsyncWritableChannel asyncWritableChannel, Callback<Long> callback) {
if (callbackWrapper != null) {
throw new IllegalStateException("ReadableStreamChannel cannot be read more than once");
} else if (!readyForRead) {
throw new IllegalStateException("The channel cannot be read yet");
}
callbackWrapper = new ReadIntoCallbackWrapper(callback);
if (!isOpen()) {
nettyMetrics.multipartRequestAlreadyClosedError.inc();
callbackWrapper.invokeCallback(new ClosedChannelException());
}
HttpContent content = requestContents.poll();
while (content != null) {
try {
writeContent(asyncWritableChannel, callbackWrapper, content);
} finally {
ReferenceCountUtil.release(content);
}
content = requestContents.poll();
}
return callbackWrapper.futureResult;
}
Aggregations