use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testReceiveBadHeader.
@Test
public void testReceiveBadHeader() throws InterruptedException, IOException {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(10000).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
RestRequest r = new RestRequestBuilder(testServer.getBadHeaderURI()).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
try {
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to fail");
} catch (TimeoutException e) {
Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, IllegalArgumentException.class);
}
testServer.shutdown();
}
use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient 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.transport.http.client.rest.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testNoResponseTimeout.
@Test
public void testNoResponseTimeout() throws InterruptedException, IOException {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
try {
// 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(30, 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("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
}
testServer.shutdown();
}
use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testBadAddress.
@Test
public void testBadAddress() throws InterruptedException, IOException, TimeoutException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(30000).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
try {
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to fail");
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, UnknownHostException.class);
}
}
use of com.linkedin.r2.transport.http.client.rest.HttpNettyClient in project rest.li by linkedin.
the class TestHttpNettyClient method testFailBackoff.
@Test
public void testFailBackoff() throws Exception {
final int WARM_UP = 10;
final int N = 5;
final int MAX_RATE_LIMITING_PERIOD = 500;
final CountDownLatch warmUpLatch = new CountDownLatch(WARM_UP);
final CountDownLatch latch = new CountDownLatch(N);
final AtomicReference<Boolean> isShutdown = new AtomicReference<>(false);
AsyncPool<Channel> testPool = new AsyncPoolImpl<>("test pool", new AsyncPool.Lifecycle<Channel>() {
@Override
public void create(Callback<Channel> callback) {
if (warmUpLatch.getCount() > 0) {
warmUpLatch.countDown();
} else {
latch.countDown();
}
callback.onError(new Throwable("Oops..."));
}
@Override
public boolean validateGet(Channel obj) {
return false;
}
@Override
public boolean validatePut(Channel obj) {
return false;
}
@Override
public void destroy(Channel obj, boolean error, Callback<Channel> callback) {
}
@Override
public PoolStats.LifecycleStats getStats() {
return null;
}
}, 200, 30000, _scheduler, Integer.MAX_VALUE, AsyncPoolImpl.Strategy.MRU, 0, new ExponentialBackOffRateLimiter(0, MAX_RATE_LIMITING_PERIOD, Math.max(10, MAX_RATE_LIMITING_PERIOD / 32), _scheduler), new SettableClock(), new LongTracking());
HttpNettyClient client = new HttpNettyClient(address -> testPool, _scheduler, MAX_RATE_LIMITING_PERIOD * 2, 500);
final RestRequest r = new RestRequestBuilder(URI.create("http://localhost:8080/")).setMethod("GET").build();
final ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
while (!isShutdown.get()) {
try {
FutureCallback<RestResponse> callback = new FutureCallback<>();
client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<>(callback));
callback.get();
} catch (Exception e) {
// ignore
}
}
});
// First ensure a bunch fail to get the rate limiting going
warmUpLatch.await(120, TimeUnit.SECONDS);
// Now we should be rate limited
long start = System.currentTimeMillis();
System.err.println("Starting at " + start);
long lowTolerance = N * MAX_RATE_LIMITING_PERIOD * 4 / 5;
long highTolerance = N * MAX_RATE_LIMITING_PERIOD * 5 / 4;
Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
long elapsed = System.currentTimeMillis() - start;
Assert.assertTrue(elapsed > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + elapsed + ")");
// shutdown everything
isShutdown.set(true);
executor.shutdown();
}
Aggregations