use of com.linkedin.restli.client.BatchGetRequestBuilder in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
// Often fails in CI without a retry
@Test(dataProvider = "requestData", retryAnalyzer = SingleRetry.class)
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<>();
if (responseCompressionConfig != null) {
responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
}
HttpClientFactory httpClientFactory = new HttpClientFactory.Builder().setEventLoopGroup(new NioEventLoopGroup(0, /* use default settings */
new NamedThreadFactory("R2 Nio Event Loop"))).setShutDownFactory(true).setScheduleExecutorService(executor).setShutdownScheduledExecutorService(true).setCallbackExecutor(executor).setShutdownCallbackExecutor(false).setJmxManager(AbstractJmxManager.NULL_JMX_MANAGER).setRequestCompressionThresholdDefault(Integer.MAX_VALUE).setRequestCompressionConfigs(Collections.<String, CompressionConfig>emptyMap()).setResponseCompressionConfigs(responseCompressionConfigs).setUseClientCompression(true).build();
Map<String, Object> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
if (useResponseCompression != null) {
properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
}
TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
Long[] ids = new Long[idCount];
for (int i = 0; i < ids.length; i++) {
ids[i] = (long) i;
}
BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
if (expectedCompressionThreshold != null) {
builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
}
Request<BatchResponse<Greeting>> request = builder.build();
Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
if (responseShouldBeCompressed) {
Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
} else {
Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
}
}
Aggregations