use of com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpBridge method testHttpToStreamErrorMessage.
@Test
public void testHttpToStreamErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(futureCallback);
TransportCallback<StreamResponse> bridgeCallback = HttpBridge.httpToStreamCallback(callback);
StreamResponse streamResponse = new StreamResponseBuilder().build(EntityStreams.emptyStream());
// Note: FutureCallback will fail if called twice. An exception would be raised on the current
// thread because we begin the callback sequence here in onResponse.
// (test originally added due to bug with double callback invocation)
bridgeCallback.onResponse(TransportResponseImpl.<StreamResponse>error(new StreamException(streamResponse)));
StreamResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
// should have unpacked restResponse from the RestException that we passed in without
// propagating the actual exception
Assert.assertSame(resp, streamResponse);
}
use of com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpBridge method testHttpToRestErrorMessage.
@Test
public void testHttpToRestErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
TransportCallback<RestResponse> bridgeCallback = HttpBridge.httpToRestCallback(callback);
RestResponse restResponse = new RestResponseBuilder().build();
// Note: FutureCallback will fail if called twice. An exception would be raised on the current
// thread because we begin the callback sequence here in onResponse.
// (test originally added due to bug with double callback invocation)
bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new RestException(restResponse)));
RestResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
// should have unpacked restResponse from the RestException that we passed in without
// propagating the actual exception
Assert.assertSame(resp, restResponse);
}
use of com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpNettyClient method testRequestContextAttributes.
@Test
public void testRequestContextAttributes() throws InterruptedException, IOException, TimeoutException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildRest();
RestRequest r = new RestRequestBuilder(URI.create("http://localhost")).build();
FutureCallback<RestResponse> cb = new FutureCallback<>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
RequestContext requestContext = new RequestContext();
client.restRequest(r, requestContext, new HashMap<>(), callback);
final String actualRemoteAddress = (String) requestContext.getLocalAttr(R2Constants.REMOTE_SERVER_ADDR);
final HttpProtocolVersion actualProtocolVersion = (HttpProtocolVersion) requestContext.getLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION);
Assert.assertTrue("127.0.0.1".equals(actualRemoteAddress) || "0:0:0:0:0:0:0:1".equals(actualRemoteAddress), "Actual remote client address is not expected. " + "The local attribute field must be IP address in string type");
Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_1_1);
}
use of com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpNettyClient method testMakingOutboundHttpsRequest.
@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
SSLContext context = SSLContext.getDefault();
SSLParameters sslParameters = context.getDefaultSSLParameters();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildRest();
RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
client.restRequest(r, new RequestContext(), new HashMap<String, String>(), callback);
cb.get(30, TimeUnit.SECONDS);
}
use of com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testNoChannelTimeout.
@Test
public void testNoChannelTimeout() throws InterruptedException {
HttpNettyStreamClient client = new HttpNettyStreamClient(new NoCreations(_scheduler), _scheduler, 500, 500, 1024 * 1024 * 2);
RestRequest r = new RestRequestBuilder(URI.create("http://localhost/")).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 {
// 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("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
}
}
Aggregations