use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.
the class TestGreetingsClientProtocolVersionHeader method testNoProtocolVersionHeaderSuccess.
@Test
public void testNoProtocolVersionHeaderSuccess() throws InterruptedException, ExecutionException {
final TransportClientAdapter client = new TransportClientAdapter(CLIENT_FACTORY.getClient(Collections.<String, String>emptyMap()));
final RestRequestBuilder requestBuilder = new RestRequestBuilder(URI.create(URI_PREFIX + "greetings/1"));
final RestRequest request = requestBuilder.build();
Assert.assertTrue(request.getHeaders().isEmpty());
final RestResponse response = client.restRequest(request).get();
Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion().toString());
}
use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.
the class TestGreetingsClientProtocolVersionHeader method testNoProtocolVersionHeaderFail.
@Test
public void testNoProtocolVersionHeaderFail() throws InterruptedException {
final TransportClientAdapter client = new TransportClientAdapter(CLIENT_FACTORY.getClient(Collections.<String, String>emptyMap()));
final RestRequestBuilder requestBuilder = new RestRequestBuilder(URI.create(URI_PREFIX));
final RestRequest request = requestBuilder.build();
Assert.assertTrue(request.getHeaders().isEmpty());
try {
client.restRequest(request).get();
} catch (ExecutionException e) {
final RestException exception = (RestException) e.getCause();
final RestResponse response = exception.getResponse();
Assert.assertEquals(response.getStatus(), HttpStatus.S_404_NOT_FOUND.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion().toString());
final DataMap exceptionDetail = DataMapUtils.readMap(response.getEntity().asInputStream());
Assert.assertEquals(exceptionDetail.getString("exceptionClass"), RestLiServiceException.class.getName());
}
}
use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.
the class TestRequestCompression method testUpdate.
@Test(dataProvider = "requestData")
public void testUpdate(CompressionConfig requestCompressionConfig, String supportedEncodings, RestliRequestOptions restliRequestOptions, int messageLength, String testHelpHeader) throws RemoteInvocationException, CloneNotSupportedException, InterruptedException, ExecutionException, TimeoutException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
Map<String, CompressionConfig> requestCompressionConfigs = new HashMap<String, CompressionConfig>();
if (requestCompressionConfig != null) {
requestCompressionConfigs.put(SERVICE_NAME, requestCompressionConfig);
}
HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(), true, executor, true, null, false, AbstractJmxManager.NULL_JMX_MANAGER, // The default compression threshold is between small and large.
500, requestCompressionConfigs);
Map<String, String> properties = new HashMap<String, String>();
properties.put(HttpClientFactory.HTTP_REQUEST_CONTENT_ENCODINGS, supportedEncodings);
properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
RootBuilderWrapper<Long, Greeting> builders = new RootBuilderWrapper<Long, Greeting>(new GreetingsRequestBuilders(restliRequestOptions));
// GET
Request<Greeting> request = builders.get().id(1L).build();
ResponseFuture<Greeting> future = client.sendRequest(request);
Response<Greeting> greetingResponse = future.getResponse();
String response1 = greetingResponse.getEntity().getMessage();
Assert.assertNotNull(response1);
// POST
Greeting greeting = new Greeting(greetingResponse.getEntity().data().copy());
char[] As = new char[messageLength];
Arrays.fill(As, 'A');
String message = new String(As);
greeting.setMessage(message);
Request<EmptyRecord> writeRequest = builders.update().id(1L).input(greeting).setHeader(TEST_HELP_HEADER, testHelpHeader).build();
client.sendRequest(writeRequest).getResponse();
// GET again, to verify that our POST worked.
Request<Greeting> request2 = builders.get().id(1L).build();
ResponseFuture<Greeting> future2 = client.sendRequest(request2);
String response2 = future2.getResponse().getEntity().getMessage();
Assert.assertEquals(response2, message);
FutureCallback<None> callback1 = new FutureCallback<None>();
client.shutdown(callback1);
callback1.get(30, TimeUnit.SECONDS);
FutureCallback<None> callback2 = new FutureCallback<None>();
httpClientFactory.shutdown(callback2);
callback2.get(30, TimeUnit.SECONDS);
}
use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownAfterClients.
@Test
public void testShutdownAfterClients() throws Exception {
NioEventLoopGroup eventLoop = new NioEventLoopGroup();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true);
Server server = new HttpServerBuilder().build();
try {
server.start();
List<Client> clients = new ArrayList<Client>();
for (int i = 0; i < 1; i++) {
clients.add(new TransportClientAdapter(factory.getClient(Collections.<String, String>emptyMap()), true));
}
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
c.restRequest(r, futureCallback);
futureCallback.get(30, TimeUnit.SECONDS);
}
for (Client c : clients) {
FutureCallback<None> callback = new FutureCallback<None>();
c.shutdown(callback);
callback.get(30, TimeUnit.SECONDS);
}
FutureCallback<None> factoryShutdown = new FutureCallback<None>();
factory.shutdown(factoryShutdown);
factoryShutdown.get(30, TimeUnit.SECONDS);
Assert.assertTrue(eventLoop.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down event-loop");
Assert.assertTrue(scheduler.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down scheduler");
} finally {
server.stop();
}
}
use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownIOThread.
@Test(dataProvider = "configs")
public void testShutdownIOThread(boolean restOverStream, String protocolVersion) throws Exception {
NioEventLoopGroup 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<None>();
factory.shutdown(factoryShutdown);
FutureCallback<None> clientShutdown = new FutureCallback<None>();
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();
}
Aggregations