use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttp2NettyStreamClient method testRequestTimeout.
/**
* Tests the condition that when a client request times out before the request is processed
* by the server, the servlet implementation throws when attempting to read the request entity.
*/
@Test(enabled = false)
public void testRequestTimeout() throws Exception {
final AtomicInteger serverIOExceptions = new AtomicInteger(0);
final CountDownLatch exceptionLatch = new CountDownLatch(1);
final CountDownLatch responseLatch = new CountDownLatch(1);
final CountDownLatch serverLatch = new CountDownLatch(1);
final HttpServerBuilder serverBuilder = new HttpServerBuilder();
final Server server = serverBuilder.exceptionListener(throwable -> {
if (throwable instanceof IOException) {
serverIOExceptions.incrementAndGet();
exceptionLatch.countDown();
}
}).responseLatch(serverLatch).build();
final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(500).buildHttp2StreamClient();
try {
server.start();
// Sends the stream request
final StreamRequestBuilder builder = new StreamRequestBuilder(new URI(URL));
final StreamRequest request = builder.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> responseLatch.countDown());
// Waits for request to timeout
Thread.sleep(1000);
// Allows server to process request
serverLatch.countDown();
} finally {
if (!responseLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
Assert.fail("Timeout waiting for response latch");
}
if (!exceptionLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
Assert.fail("Timeout waiting for exception latch");
}
server.stop();
}
// Expects two IOExceptions thrown by the server. One for the initial OPTIONS upgrade request and one for
// the actual GET request.
Assert.assertEquals(serverIOExceptions.get(), 2);
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testShutdownRequestOutstanding.
private void testShutdownRequestOutstanding(AbstractNettyStreamClient client, Class<?>... causeChain) throws Exception {
CountDownLatch responseLatch = new CountDownLatch(1);
Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
try {
server.start();
RestRequest r = new RestRequestBuilder(new URI(URL)).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
// This timeout needs to be significantly larger than the getTimeout of the netty client;
// we're testing that the client will generate its own timeout
cb.get(60, TimeUnit.SECONDS);
Assert.fail("Get was supposed to time out");
} catch (TimeoutException e) {
// TimeoutException means the timeout for Future.get() elapsed and nothing happened.
// Instead, we are expecting our callback to be invoked before the future timeout
// with a timeout generated by the HttpNettyClient.
Assert.fail("Get timed out, should have thrown ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, causeChain);
} finally {
responseLatch.countDown();
server.stop();
}
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testCancelStreamRequests.
@Test(dataProvider = "requestResponseParameters", enabled = false)
public void testCancelStreamRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
AtomicInteger succeeded = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
CountDownLatch latch = new CountDownLatch(REQUEST_COUNT);
for (int i = 0; i < REQUEST_COUNT; i++) {
StreamRequest request = new StreamRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[requestSize]))));
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.streamRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse response) {
response.getEntityStream().setReader(new Reader() {
@Override
public void onDataAvailable(ByteString data) {
}
@Override
public void onDone() {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onInit(ReadHandle rh) {
rh.cancel();
succeeded.incrementAndGet();
latch.countDown();
}
});
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
}));
}
if (!latch.await(30, TimeUnit.SECONDS)) {
Assert.fail("Timeout waiting for responses. " + succeeded + " requests succeeded and " + failed + " requests failed out of total " + REQUEST_COUNT + " requests");
}
Assert.assertEquals(latch.getCount(), 0);
Assert.assertEquals(failed.get(), 0);
Assert.assertEquals(succeeded.get(), REQUEST_COUNT);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
} finally {
server.stop();
}
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testHeaderSize.
public void testHeaderSize(AbstractNettyStreamClient client, int headerSize, int expectedResult) throws Exception {
Server server = new HttpServerBuilder().headerSize(headerSize).build();
try {
server.start();
RestRequest r = new RestRequestBuilder(new URI(URL)).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
cb.get(300, TimeUnit.SECONDS);
if (expectedResult == TOO_LARGE) {
Assert.fail("Max header size exceeded, expected exception. ");
}
} catch (ExecutionException e) {
if (expectedResult == RESPONSE_OK) {
Assert.fail("Unexpected ExecutionException, header was <= max header size.", e);
}
if (client instanceof HttpNettyStreamClient) {
verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
} else if (client instanceof Http2NettyStreamClient) {
verifyCauseChain(e, RemoteInvocationException.class, Http2Exception.class);
} else {
Assert.fail("Unrecognized client");
}
} finally {
server.stop();
}
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testRestRequests.
@Test(dataProvider = "requestResponseParameters", expectedExceptions = UnsupportedOperationException.class)
public void testRestRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
for (int i = 0; i < REQUEST_COUNT; i++) {
RestRequest request = new RestRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).setEntity(ByteString.copy(new byte[requestSize])).build();
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.restRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse response) {
}
@Override
public void onError(Throwable e) {
}
}));
}
} finally {
server.stop();
}
}
Aggregations