use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownIOThread.
@Test(dataProvider = "configs")
public void testShutdownIOThread(boolean restOverStream, String protocolVersion) throws Exception {
EventLoopGroup eventLoop = new NioEventLoopGroup();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
ExecutorService callbackExecutor = Executors.newFixedThreadPool(1);
HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true, callbackExecutor, false);
CountDownLatch responseLatch = new CountDownLatch(1);
Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
try {
server.start();
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
Client client = new TransportClientAdapter(factory.getClient(properties), restOverStream);
URI uri = new URI(URI);
Future<RestResponse> responseFuture = client.restRequest(new RestRequestBuilder(uri).build());
FutureCallback<None> factoryShutdown = new FutureCallback<>();
factory.shutdown(factoryShutdown);
FutureCallback<None> clientShutdown = new FutureCallback<>();
client.shutdown(clientShutdown);
// Client and factory shutdowns are now pending. When we release the latch, the response will
// be returned, which causes the shutdowns to complete on the Netty IO thread that received the
// response.
responseLatch.countDown();
clientShutdown.get(60, TimeUnit.SECONDS);
factoryShutdown.get(60, TimeUnit.SECONDS);
} finally {
server.stop();
}
Assert.assertTrue(eventLoop.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down event-loop");
Assert.assertTrue(scheduler.awaitTermination(60, TimeUnit.SECONDS), "Failed to shut down scheduler");
callbackExecutor.shutdown();
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestHttpClientFactory method testSuccessfulRequest.
@Test(dataProvider = "configsExpectedRequestCount")
public void testSuccessfulRequest(boolean restOverStream, String protocolVersion, int expectedRequests) throws Exception {
EventLoopGroup eventLoop = new NioEventLoopGroup();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true);
HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = new HttpServerBuilder.HttpServerStatsProvider();
Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
try {
server.start();
List<Client> clients = new ArrayList<>();
int savedTimingKeyCount = TimingKey.getCount();
for (int i = 0; i < 100; i++) {
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
}
int addedTimingKeyCount = TimingKey.getCount() - savedTimingKeyCount;
// In current implementation, one client can have around 30 TimingKeys by default.
Assert.assertTrue(addedTimingKeyCount >= 30 * clients.size());
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
c.restRequest(r).get(30, TimeUnit.SECONDS);
}
Assert.assertEquals(httpServerStatsProvider.requestCount(), expectedRequests);
savedTimingKeyCount = TimingKey.getCount();
for (Client c : clients) {
FutureCallback<None> callback = new FutureCallback<>();
c.shutdown(callback);
callback.get(30, TimeUnit.SECONDS);
}
FutureCallback<None> factoryShutdown = new FutureCallback<>();
factory.shutdown(factoryShutdown);
factoryShutdown.get(30, TimeUnit.SECONDS);
int removedTimingKeyCount = savedTimingKeyCount - TimingKey.getCount();
Assert.assertEquals(addedTimingKeyCount, removedTimingKeyCount);
} finally {
server.stop();
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestHttpNettyClientCommon method testPerRequestTimeout.
/**
* Testing making request with custom-perRequest timeout, higher and lower than request timeout,
* d2 or http requests and check it is working
*/
@SuppressWarnings("unchecked")
@Test(dataProvider = "isStreamAndHigher")
public void testPerRequestTimeout(boolean isStream, boolean isHigherThanDefault) throws InterruptedException, IOException {
TestServer testServer = new TestServer();
int defaultRequestTimeout = 300;
int requestTimeoutPerRequest = isHigherThanDefault ? defaultRequestTimeout + 200 : defaultRequestTimeout - 200;
HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(defaultRequestTimeout);
AbstractNettyClient<?, ?> client = isStream ? clientBuilder.buildStreamClient() : clientBuilder.buildRestClient();
RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.REQUEST_TIMEOUT, requestTimeoutPerRequest);
long startTime = System.currentTimeMillis();
FutureCallback<?> cb = new FutureCallback<>();
if (isStream) {
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>((FutureCallback<StreamResponse>) cb);
client.streamRequest(Messages.toStreamRequest(r), requestContext, new HashMap<>(), callback);
} else {
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>((FutureCallback<RestResponse>) cb);
client.restRequest(r, 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(10, 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);
long endTime = System.currentTimeMillis();
Assert.assertEquals((endTime - startTime) > defaultRequestTimeout, isHigherThanDefault, "The request timed out after " + (endTime - startTime) + "ms but it was supposed to be about " + (isHigherThanDefault ? "higher" : "lower") + " than " + defaultRequestTimeout + "ms");
// 150 ms of accuracy
Assert.assertTrue(// 150 ms of accuracy
(endTime - startTime) - requestTimeoutPerRequest < 150, "The request timed out after " + (endTime - startTime) + "ms but it was supposed to be about " + requestTimeoutPerRequest + "ms");
}
testServer.shutdown();
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testMakingOutboundHttpsRequest.
@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
SSLContext context = SSLContext.getDefault();
SSLParameters sslParameters = context.getDefaultSSLParameters();
HttpNettyStreamClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildStreamClient();
RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
cb.get(30, TimeUnit.SECONDS);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestNettyRequestAdapter method testRestToNettyRequestWithMultipleCookies.
@Test
public void testRestToNettyRequestWithMultipleCookies() throws Exception {
RestRequestBuilder restRequestBuilder = new RestRequestBuilder(new URI(ANY_URI));
restRequestBuilder.setCookies(ANY_COOKIES);
RestRequest restRequest = restRequestBuilder.build();
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(restRequest);
Assert.assertEquals(nettyRequest.headers().get("Cookie"), ENCODED_COOKIES_HEADER_VALUE);
}
Aggregations