use of com.linkedin.r2.transport.common.bridge.common.TransportCallback 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()) {
_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, _requestContext, _wireAttrs, transportCallback);
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestHttpBridge method testStreamToHttpErrorMessage.
@Test
public void testStreamToHttpErrorMessage() throws TimeoutException, InterruptedException {
URI uri = URI.create("http://some.host/thisShouldAppearInTheErrorMessage");
RestRequest r = new RestRequestBuilder(uri).build();
FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(futureCallback);
TransportCallback<StreamResponse> bridgeCallback = HttpBridge.streamToHttpCallback(callback, Messages.toStreamRequest(r));
bridgeCallback.onResponse(TransportResponseImpl.<StreamResponse>error(new Exception()));
try {
futureCallback.get(30, TimeUnit.SECONDS);
Assert.fail("get should have thrown exception");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause().getMessage().contains(uri.toString()));
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestHttpBridge method testRestToHttpErrorMessage.
@Test
public void testRestToHttpErrorMessage() throws TimeoutException, InterruptedException {
URI uri = URI.create("http://some.host/thisShouldAppearInTheErrorMessage");
RestRequest r = new RestRequestBuilder(uri).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
TransportCallback<RestResponse> bridgeCallback = HttpBridge.restToHttpCallback(callback, r);
bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new Exception()));
try {
futureCallback.get(30, TimeUnit.SECONDS);
Assert.fail("get should have thrown exception");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause().getMessage().contains(uri.toString()));
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestMessages method testToRestTransportCallbackSuccess.
@Test
public void testToRestTransportCallbackSuccess() {
TransportCallback<StreamResponse> streamCallback = response -> {
Assert.assertFalse(response.hasError());
Assert.assertNotNull(response.getResponse());
response.getResponse().getEntityStream().setReader(ENTITY_VERIFIER);
Assert.assertNotNull(response.getWireAttributes());
Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
};
TransportCallback<RestResponse> restCallback = Messages.toRestTransportCallback(streamCallback);
RestResponseBuilder builder = new RestResponseBuilder();
builder.setEntity(DATA);
RestResponse restResponse = builder.build();
restCallback.onResponse(TransportResponseImpl.success(restResponse, WIRE_ATTR));
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestMessages method testToRestTransportCallbackRestException.
@Test
public void testToRestTransportCallbackRestException() {
TransportCallback<StreamResponse> streamCallback = response -> {
Assert.assertTrue(response.hasError());
Assert.assertNotNull(response.getError());
Assert.assertTrue(response.getError() instanceof StreamException);
Assert.assertNotNull(response.getWireAttributes());
Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
};
TransportCallback<RestResponse> restCallback = Messages.toRestTransportCallback(streamCallback);
RestResponseBuilder builder = new RestResponseBuilder();
builder.setEntity(DATA);
RestResponse restResponse = builder.build();
restCallback.onResponse(TransportResponseImpl.error(new RestException(restResponse, new IllegalStateException()), WIRE_ATTR));
}
Aggregations