use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.
the class ChannelWriteCallback method behaviourUnderWriteFailuresTest.
/**
* Tests behaviour of various functions of {@link NettyResponseChannel} under write failures.
* @throws Exception
*/
@Test
public void behaviourUnderWriteFailuresTest() throws Exception {
onResponseCompleteUnderWriteFailureTest(TestingUri.ImmediateResponseComplete);
onResponseCompleteUnderWriteFailureTest(TestingUri.OnResponseCompleteWithNonRestException);
// writing to channel with a outbound handler that generates an Exception
String message = UtilsTest.getRandomString(10);
try {
String content = "@@randomContent@@@";
MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler(new Exception(message));
EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
// channel has been closed because of write failure
channel.writeInbound(createContent(content, true));
verifyCallbacks(processor);
fail("Callback for write would have thrown an Exception");
} catch (Exception e) {
assertEquals("Exception not as expected", message, e.getMessage());
}
// writing to channel with a outbound handler that encounters a ClosedChannelException
try {
String content = "@@randomContent@@@";
MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler(new ClosedChannelException());
EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
// channel has been closed because of write failure
channel.writeInbound(createContent(content, true));
verifyCallbacks(processor);
fail("Callback for write would have thrown an Exception");
} catch (IOException e) {
assertTrue("Should be recognized as a client termination", Utils.isPossibleClientTermination(e));
}
// writing to channel with a outbound handler that generates an Error
MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
EmbeddedChannel channel = new EmbeddedChannel(new ErrorOutboundHandler(), processor);
try {
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, TestingUri.WriteFailureWithThrowable.toString(), null));
verifyCallbacks(processor);
} catch (Error e) {
assertEquals("Unexpected error", ErrorOutboundHandler.ERROR_MESSAGE, e.getMessage());
}
channel = createEmbeddedChannel();
processor = channel.pipeline().get(MockNettyMessageProcessor.class);
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ResponseFailureMidway.toString(), null));
verifyCallbacks(processor);
assertFalse("Channel is not closed at the remote end", channel.isActive());
}
use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.
the class ChannelWriteCallback method headersPresenceTest.
/**
* Sends a request with certain headers that will copied into the response. Checks the response for those headers to
* see that values match.
* @throws ParseException
*/
@Test
public void headersPresenceTest() throws ParseException {
HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.CopyHeaders.toString());
HttpUtil.setKeepAlive(request, false);
EmbeddedChannel channel = createEmbeddedChannel();
channel.writeInbound(request);
HttpResponse response = (HttpResponse) channel.readOutbound();
assertFalse("Channel not closed on the server", channel.isActive());
checkHeaders(request, response);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.
the class ChannelWriteCallback method errorResponseTest.
/**
* Tests that error responses are correctly formed.
*/
@Test
public void errorResponseTest() {
EmbeddedChannel channel = createEmbeddedChannel();
for (RestServiceErrorCode errorCode : RestServiceErrorCode.values()) {
HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, errorCode);
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.HEAD, TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders));
HttpResponse response = channel.readOutbound();
HttpResponseStatus expectedStatus = getExpectedHttpResponseStatus(errorCode);
assertEquals("Unexpected response status", expectedStatus, response.status());
boolean containsFailureReasonHeader = response.headers().contains(NettyResponseChannel.FAILURE_REASON_HEADER);
if (expectedStatus == HttpResponseStatus.BAD_REQUEST) {
assertTrue("Could not find failure reason header.", containsFailureReasonHeader);
} else {
assertFalse("Should not have found failure reason header.", containsFailureReasonHeader);
}
if (HttpStatusClass.CLIENT_ERROR.contains(response.status().code())) {
assertEquals("Wrong error code", errorCode, RestServiceErrorCode.valueOf(response.headers().get(NettyResponseChannel.ERROR_CODE_HEADER)));
} else {
assertFalse("Should not have found error code header", response.headers().contains(NettyResponseChannel.ERROR_CODE_HEADER));
}
if (response instanceof FullHttpResponse) {
// assert that there is no content
assertEquals("The response should not contain content", 0, ((FullHttpResponse) response).content().readableBytes());
} else {
HttpContent content = channel.readOutbound();
assertTrue("End marker should be received", content instanceof LastHttpContent);
}
assertNull("There should be no more data in the channel", channel.readOutbound());
boolean shouldBeAlive = !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(expectedStatus);
assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
assertEquals("Connection header should be consistent with channel state", shouldBeAlive, HttpUtil.isKeepAlive(response));
if (!shouldBeAlive) {
channel = createEmbeddedChannel();
}
}
channel.close();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.
the class ChannelWriteCallback method createEmbeddedChannel.
/**
* Creates a new {@link EmbeddedChannel} with a {@link ChunkedWriteHandler} and {@link MockNettyMessageProcessor} in
* the pipeline.
* @return the created {@link EmbeddedChannel}.
*/
private EmbeddedChannel createEmbeddedChannel() {
ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
return new EmbeddedChannel(chunkedWriteHandler, processor);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project ambry by linkedin.
the class ChannelWriteCallback method setStatusTest.
/**
* Tests setting of different available {@link ResponseStatus} codes and sees that they are recognized and converted
* in {@link NettyResponseChannel}.
* <p/>
* If this test fails, a case for conversion probably needs to be added in {@link NettyResponseChannel}.
*/
@Test
public void setStatusTest() {
// ask for every status to be set
for (ResponseStatus expectedResponseStatus : ResponseStatus.values()) {
HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.SetStatus.toString());
request.headers().set(MockNettyMessageProcessor.STATUS_HEADER_NAME, expectedResponseStatus);
HttpUtil.setKeepAlive(request, false);
EmbeddedChannel channel = createEmbeddedChannel();
channel.writeInbound(request);
// pull but discard response
channel.readOutbound();
assertFalse("Channel not closed on the server", channel.isActive());
}
// check if all the ResponseStatus codes were recognized.
String metricName = MetricRegistry.name(NettyResponseChannel.class, "UnknownResponseStatusCount");
long metricCount = MockNettyMessageProcessor.METRIC_REGISTRY.getCounters().get(metricName).getCount();
assertEquals("Some of the ResponseStatus codes were not recognized", 0, metricCount);
}
Aggregations