Search in sources :

Example 6 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestClientStreamCompressionFilter method provideRequestData.

@DataProvider(name = "requestData")
private Object[][] provideRequestData() {
    CompressionConfig smallThresholdConfig = new CompressionConfig(1);
    CompressionConfig largeThresholdConfig = new CompressionConfig(10000);
    return new Object[][] { { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_OFF, false, "" }, { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_ON, true, "" }, { new CompressionConfig(Integer.MAX_VALUE), null, false, "" }, { new CompressionConfig(0), CompressionOption.FORCE_OFF, false, "" }, { new CompressionConfig(0), CompressionOption.FORCE_ON, true, "" }, { new CompressionConfig(0), null, true, "" }, { smallThresholdConfig, CompressionOption.FORCE_OFF, false, "" }, { smallThresholdConfig, CompressionOption.FORCE_ON, true, "" }, { smallThresholdConfig, null, true, "" }, { largeThresholdConfig, CompressionOption.FORCE_OFF, false, "" }, { largeThresholdConfig, CompressionOption.FORCE_ON, true, "" }, { largeThresholdConfig, null, false, "" }, // The same tests, but with null instead of an empty string
    { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_OFF, false, null }, { new CompressionConfig(Integer.MAX_VALUE), CompressionOption.FORCE_ON, true, null }, { new CompressionConfig(Integer.MAX_VALUE), null, false, null }, { new CompressionConfig(0), CompressionOption.FORCE_OFF, false, null }, { new CompressionConfig(0), CompressionOption.FORCE_ON, true, null }, { new CompressionConfig(0), null, true, null }, { smallThresholdConfig, CompressionOption.FORCE_OFF, false, null }, { smallThresholdConfig, CompressionOption.FORCE_ON, true, null }, { smallThresholdConfig, null, true, null }, { largeThresholdConfig, CompressionOption.FORCE_OFF, false, null }, { largeThresholdConfig, CompressionOption.FORCE_ON, true, null }, { largeThresholdConfig, null, false, null } };
}
Also used : CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 7 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestClientCompressionFilter method testResponseCompressionRules.

@Test(dataProvider = "responseCompressionData")
public void testResponseCompressionRules(CompressionConfig responseCompressionConfig, CompressionOption responseCompressionOverride, String expectedAcceptEncoding, String expectedCompressionThreshold, String operation) throws CompressionException, URISyntaxException {
    ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), new CompressionConfig(Integer.MAX_VALUE), ACCEPT_COMPRESSIONS, responseCompressionConfig, Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR));
    RestRequest restRequest = new RestRequestBuilder(new URI(URI)).build();
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, responseCompressionOverride);
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.ACCEPT_ENCODING, expectedAcceptEncoding));
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, expectedCompressionThreshold));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 8 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestClientCompressionFilter method testRequestCompressionRules.

@Test(dataProvider = "requestCompressionData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent) throws CompressionException, URISyntaxException {
    ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Collections.<String>emptyList());
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    RestRequest restRequest = new RestRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).setEntity(entity).build();
    int compressed = EncodingType.SNAPPY.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    String expectedContentEncoding = headerShouldBePresent ? EncodingType.SNAPPY.getHttpName() : null;
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncoding, entityLength));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 9 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class RestLiIntegrationTest method init.

public void init(List<? extends Filter> filters) throws IOException {
    final FilterChain fc = FilterChains.empty().addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression, new CompressionConfig(0))).addLastRest(new SimpleLoggingFilter());
    init(filters, fc, false);
}
Also used : ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig)

Example 10 with CompressionConfig

use of com.linkedin.r2.filter.CompressionConfig in project rest.li by linkedin.

the class TestRequestCompression method requestData.

@DataProvider(name = "requestData")
private Object[][] requestData() {
    int tiny = 10;
    int small = 100;
    int large = 1000;
    int huge = 10000;
    CompressionConfig tinyThresholdConfig = new CompressionConfig(tiny);
    CompressionConfig hugeThresholdConfig = new CompressionConfig(huge);
    String encodings = "unsupportedEncoding, x-snappy-framed, snappy, gzip";
    RestliRequestOptions forceOnOption = new RestliRequestOptionsBuilder().setProtocolVersionOption(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE).setRequestCompressionOverride(CompressionOption.FORCE_ON).build();
    RestliRequestOptions forceOffOption = new RestliRequestOptionsBuilder().setProtocolVersionOption(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE).setRequestCompressionOverride(CompressionOption.FORCE_OFF).build();
    return new Object[][] { // Compression depending on request size
    { null, encodings, RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_COMPRESSION }, { null, encodings, RestliRequestOptions.DEFAULT_OPTIONS, small, EXPECT_NO_COMPRESSION }, // Override the default threshold and cause even small requests to be compressed
    { tinyThresholdConfig, encodings, RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_COMPRESSION }, { tinyThresholdConfig, encodings, RestliRequestOptions.DEFAULT_OPTIONS, small, EXPECT_COMPRESSION }, // Override the default threshold and causes even large requests to be NOT compressed.
    { hugeThresholdConfig, encodings, RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_NO_COMPRESSION }, { hugeThresholdConfig, encodings, RestliRequestOptions.DEFAULT_OPTIONS, small, EXPECT_NO_COMPRESSION }, // Force on/off using RestliRequestOptions
    { null, encodings, forceOnOption, large, EXPECT_COMPRESSION }, { null, encodings, forceOnOption, small, EXPECT_COMPRESSION }, { hugeThresholdConfig, encodings, forceOnOption, small, EXPECT_COMPRESSION }, { null, encodings, forceOffOption, large, EXPECT_NO_COMPRESSION }, { null, encodings, forceOffOption, small, EXPECT_NO_COMPRESSION }, { tinyThresholdConfig, encodings, forceOffOption, large, EXPECT_NO_COMPRESSION }, // Force on/off using RequestCompressionConfig
    { new CompressionConfig(0), encodings, RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_COMPRESSION }, { new CompressionConfig(0), encodings, RestliRequestOptions.DEFAULT_OPTIONS, small, EXPECT_COMPRESSION }, { new CompressionConfig(Integer.MAX_VALUE), encodings, RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_NO_COMPRESSION }, { new CompressionConfig(Integer.MAX_VALUE), encodings, RestliRequestOptions.DEFAULT_OPTIONS, small, EXPECT_NO_COMPRESSION }, // RestliRequestOptions takes precedence over RequestCompressionConfig
    { new CompressionConfig(0), encodings, forceOffOption, large, EXPECT_NO_COMPRESSION }, { new CompressionConfig(Integer.MAX_VALUE), encodings, forceOnOption, small, EXPECT_COMPRESSION }, // Can't compress if no encodings are available
    { null, "unsupportedEncoding", RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_NO_COMPRESSION }, { null, "", RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_NO_COMPRESSION }, { new CompressionConfig(0), "unsupportedEncoding", RestliRequestOptions.DEFAULT_OPTIONS, large, EXPECT_NO_COMPRESSION }, { null, "", forceOnOption, large, EXPECT_NO_COMPRESSION } };
}
Also used : RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) RestliRequestOptionsBuilder(com.linkedin.restli.client.RestliRequestOptionsBuilder) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Aggregations

CompressionConfig (com.linkedin.r2.filter.CompressionConfig)20 DataProvider (org.testng.annotations.DataProvider)9 Test (org.testng.annotations.Test)8 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)6 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)6 RequestContext (com.linkedin.r2.message.RequestContext)5 URI (java.net.URI)5 HashMap (java.util.HashMap)5 ByteString (com.linkedin.data.ByteString)4 ClientStreamCompressionFilter (com.linkedin.r2.filter.compression.ClientStreamCompressionFilter)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4 Client (com.linkedin.r2.transport.common.Client)4 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)4 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)4 FilterChain (com.linkedin.r2.filter.FilterChain)3 StreamFilter (com.linkedin.r2.filter.message.stream.StreamFilter)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FutureCallback (com.linkedin.common.callback.FutureCallback)2