Search in sources :

Example 1 with BatchGetRequestBuilder

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));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) BatchResponse(com.linkedin.restli.common.BatchResponse) RestliRequestOptionsBuilder(com.linkedin.restli.client.RestliRequestOptionsBuilder) BatchGetRequestBuilder(com.linkedin.restli.client.BatchGetRequestBuilder) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Aggregations

CompressionConfig (com.linkedin.r2.filter.CompressionConfig)1 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)1 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)1 NamedThreadFactory (com.linkedin.r2.util.NamedThreadFactory)1 BatchGetRequestBuilder (com.linkedin.restli.client.BatchGetRequestBuilder)1 RestClient (com.linkedin.restli.client.RestClient)1 RestliRequestOptionsBuilder (com.linkedin.restli.client.RestliRequestOptionsBuilder)1 BatchResponse (com.linkedin.restli.common.BatchResponse)1 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)1 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 HashMap (java.util.HashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Test (org.testng.annotations.Test)1