use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestDisruptor method testStreamTimeoutDisrupt.
@Test
public void testStreamTimeoutDisrupt() throws Exception {
final Map<String, Object> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
final Client client = _clientProvider.createClient(FilterChains.empty(), properties);
final RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.timeout());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
client.streamRequest(new StreamRequestBuilder(getHttpURI()).build(EntityStreams.emptyStream()), requestContext, new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
}
@Override
public void onError(Throwable e) {
success.set(e instanceof TimeoutException);
latch.countDown();
}
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestDisruptor method testRestTimeoutDisrupt.
@Test
public void testRestTimeoutDisrupt() throws Exception {
final Map<String, Object> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
final Client client = _clientProvider.createClient(FilterChains.empty(), properties);
final RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.timeout());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
client.restRequest(new RestRequestBuilder(getHttpURI()).build(), requestContext, new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse result) {
latch.countDown();
}
@Override
public void onError(Throwable e) {
success.set(e instanceof TimeoutException);
latch.countDown();
}
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TransportHealthCheck method checkHealth.
@Override
public void checkHealth(Callback<None> callback) {
final long startTime = _clock.currentTimeMillis();
TransportCallback<RestResponse> transportCallback = response -> {
long delay = _clock.currentTimeMillis() - startTime;
if (response.hasError()) {
// Currently treat all errors as failure
_log.debug("checkHealth: error response for request ({}): {}", _restRequest.getURI(), response.getError());
callback.onError(new Exception("Error from " + _restRequest.getURI() + " : " + response.getError()));
} else if (delay > _responseTimeThreshold) {
_log.debug("checkHealth: return delay ({}ms) longer than threshold for request {}", delay, _restRequest.getURI());
callback.onError(new TimeoutException("HealthCheck Timeout: " + delay + "ms for " + _restRequest.getURI()));
} else if (!_healthCheckResponseValidator.validateResponse(response.getResponse())) {
_log.error("checkHealth: response validating error for request ({}): {}", _restRequest.getURI(), response);
callback.onError(new Throwable("HealthCheck Response Error"));
} else {
_log.debug("checkHealth successful for client {}", _clientToCheck);
callback.onSuccess(None.none());
}
};
_clientToCheck.restRequest(_restRequest, _requestContextSupplier.get(), _wireAttrsSupplier.get(), transportCallback);
}
use of com.linkedin.r2.util.Timeout 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<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
StreamResponse response = cb.get(30, TimeUnit.SECONDS);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<>();
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.r2.util.Timeout 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