use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testResponseSize.
public void testResponseSize(AbstractNettyStreamClient client, int responseSize, int expectedResult) throws Exception {
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
RestRequest r = new RestRequestBuilder(new URI(URL)).build();
FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
StreamResponse response = cb.get(30, TimeUnit.SECONDS);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
response.getEntityStream().setReader(new Reader() {
@Override
public void onInit(ReadHandle rh) {
rh.request(Integer.MAX_VALUE);
}
@Override
public void onDataAvailable(ByteString data) {
}
@Override
public void onDone() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
error.set(e);
latch.countDown();
}
});
if (!latch.await(30, TimeUnit.SECONDS)) {
Assert.fail("Timeout waiting for response");
}
if (expectedResult == TOO_LARGE) {
Assert.assertNotNull(error.get(), "Max response size exceeded, expected exception. ");
verifyCauseChain(error.get(), TooLongFrameException.class);
}
if (expectedResult == RESPONSE_OK) {
Assert.assertNull(error.get(), "Unexpected Exception: response size <= max size");
}
} catch (ExecutionException e) {
if (expectedResult == RESPONSE_OK) {
Assert.fail("Unexpected ExecutionException, response was <= max response size.", e);
}
verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
} finally {
server.stop();
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testShutdownRequestOutstanding.
private void testShutdownRequestOutstanding(AbstractNettyStreamClient client, Class<?>... causeChain) throws Exception {
CountDownLatch responseLatch = new CountDownLatch(1);
Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
try {
server.start();
RestRequest r = new RestRequestBuilder(new URI(URL)).build();
FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
// This timeout needs to be significantly larger than the getTimeout of the netty client;
// we're testing that the client will generate its own timeout
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to time out");
} catch (TimeoutException e) {
// TimeoutException means the timeout for Future.get() elapsed and nothing happened.
// Instead, we are expecting our callback to be invoked before the future timeout
// with a timeout generated by the HttpNettyClient.
Assert.fail("Get timed out, should have thrown ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, causeChain);
} finally {
responseLatch.countDown();
server.stop();
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testBadAddress.
@Test(dataProvider = "badAddressClients")
public void testBadAddress(AbstractNettyStreamClient client) throws InterruptedException, IOException, TimeoutException {
RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).build();
FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
try {
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to fail");
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, UnknownHostException.class);
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testShutdownStuckInPool.
@Test
public void testShutdownStuckInPool() throws InterruptedException, ExecutionException, TimeoutException {
// Test that shutdown works when the outstanding request is stuck in the pool waiting for a channel
HttpNettyStreamClient client = new HttpNettyStreamClient(new NoCreations(_scheduler), _scheduler, 60000, 1, 1024 * 1024 * 2);
RestRequest r = new RestRequestBuilder(URI.create("http://some.host/")).build();
FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), new TransportCallbackAdapter<StreamResponse>(futureCallback));
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
try {
futureCallback.get(30, TimeUnit.SECONDS);
Assert.fail("get should have thrown exception");
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testStreamRequests.
/**
* Tests implementations of {@link AbstractNettyStreamClient} with different request dimensions.
*
* @param client Client implementation of {@link AbstractNettyStreamClient}
* @param method HTTP request method
* @param requestSize Request content size
* @param responseSize Response content size
* @param isFullRequest Whether to buffer a full request before stream
* @throws Exception
*/
@Test(dataProvider = "requestResponseParameters")
public void testStreamRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
AtomicInteger succeeded = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
CountDownLatch latch = new CountDownLatch(REQUEST_COUNT);
for (int i = 0; i < REQUEST_COUNT; i++) {
StreamRequest request = new StreamRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[requestSize]))));
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.streamRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse response) {
response.getEntityStream().setReader(new Reader() {
ReadHandle _rh;
int _consumed = 0;
@Override
public void onDataAvailable(ByteString data) {
_consumed += data.length();
_rh.request(1);
}
@Override
public void onDone() {
succeeded.incrementAndGet();
latch.countDown();
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(1);
}
});
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
}));
}
if (!latch.await(30, TimeUnit.SECONDS)) {
Assert.fail("Timeout waiting for responses. " + succeeded + " requests succeeded and " + failed + " requests failed out of total " + REQUEST_COUNT + " requests");
}
Assert.assertEquals(latch.getCount(), 0);
Assert.assertEquals(failed.get(), 0);
Assert.assertEquals(succeeded.get(), REQUEST_COUNT);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
} finally {
server.stop();
}
}
Aggregations