use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestRestCompressionEcho method tearDown.
@AfterClass
public void tearDown() throws Exception {
for (Client client : _clients) {
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
client.shutdown(clientShutdownCallback);
clientShutdownCallback.get();
}
for (TransportClientFactory factory : _clientFactories) {
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
factory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
}
if (_server != null) {
_server.stop();
_server.waitForStop();
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
private void testResponseCompression(URI uri, long bytes, String acceptEncoding, final StreamingCompressor compressor) throws InterruptedException, TimeoutException, ExecutionException {
for (Client client : clients()) {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, uri)));
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
StreamRequest request = builder.build(EntityStreams.emptyStream());
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
final FutureCallback<None> readerCallback = new FutureCallback<None>();
final BytesReader reader = new BytesReader(BYTE, readerCallback);
final EntityStream decompressedStream = compressor.inflate(response.getEntityStream());
decompressedStream.setReader(reader);
readerCallback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(reader.getTotalBytes(), bytes);
Assert.assertTrue(reader.allBytesCorrect());
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestResponseCompression method testEncodingNotAcceptable.
public void testEncodingNotAcceptable(String acceptEncoding) throws TimeoutException, InterruptedException {
for (Client client : clients()) {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, SMALL_URI)));
if (acceptEncoding != null) {
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
}
StreamRequest request = builder.build(EntityStreams.emptyStream());
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
try {
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.fail("Should have thrown exception when encoding is not acceptable");
} catch (ExecutionException e) {
Throwable t = e.getCause();
Assert.assertTrue(t instanceof StreamException);
StreamResponse response = ((StreamException) t).getResponse();
Assert.assertEquals(response.getStatus(), HttpConstants.NOT_ACCEPTABLE);
}
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestRestCompressionEcho method testResponseCompression.
@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
RestRequestBuilder builder = new RestRequestBuilder((Bootstrap.createHttpURI(PORT, ECHO_URI)));
byte[] content = new byte[(int) bytes];
for (int i = 0; i < bytes; i++) {
content[i] = (byte) (i % 256);
}
RestRequest request = builder.setEntity(content).build();
final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
RequestContext requestContext = new RequestContext();
// OPERATION is required to enabled response compression
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
client.restRequest(request, requestContext, callback);
final RestResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
Assert.assertEquals(response.getEntity().copyBytes(), content);
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestClientStreamCompressionFilter method testRequestCompressionRules.
@Test(dataProvider = "requestData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
Executor executor = Executors.newCachedThreadPool();
ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
// The entity should be compressible for this test.
int original = 100;
byte[] entity = new byte[original];
Arrays.fill(entity, (byte) 'A');
StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
RequestContext context = new RequestContext();
if (operation != null) {
context.putLocalAttr(R2Constants.OPERATION, operation);
}
context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
int entityLength = headerShouldBePresent ? compressed : original;
HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength);
clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
FutureCallback<ByteString> callback = new FutureCallback<ByteString>();
FullEntityReader reader = new FullEntityReader(callback);
captureFilter.getEntityStream().setReader(reader);
ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
Assert.assertEquals(entityRead.length(), entityLength);
}
Aggregations