use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class Http2StreamResponseHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ResponseWithCallback) {
@SuppressWarnings("unchecked") ResponseWithCallback<StreamResponse, TransportCallback<StreamResponse>> responseWithCallback = (ResponseWithCallback<StreamResponse, TransportCallback<StreamResponse>>) msg;
StreamResponse response = responseWithCallback.response();
TransportCallback<StreamResponse> callback = responseWithCallback.callback();
Map<String, String> headers = new HashMap<>(response.getHeaders());
Map<String, String> wireAttrs = new HashMap<>(WireAttributeHelper.removeWireAttributes(headers));
StreamResponse newResponse = new StreamResponseBuilder(response).unsafeSetHeaders(headers).build(response.getEntityStream());
LOG.debug("{}: handling a response", ctx.channel().remoteAddress());
callback.onResponse(TransportResponseImpl.success(newResponse, wireAttrs));
}
ctx.fireChannelRead(msg);
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testCancelStreamRequests.
@Test(dataProvider = "requestResponseParameters", groups = TestGroupNames.TESTNG_GROUP_KNOWN_ISSUE)
public void testCancelStreamRequests(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() {
@Override
public void onDataAvailable(ByteString data) {
}
@Override
public void onDone() {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onInit(ReadHandle rh) {
rh.cancel();
succeeded.incrementAndGet();
latch.countDown();
}
});
}
@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();
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testRequestContextAttributes.
@Test(dataProvider = "remoteClientAddressClients")
public void testRequestContextAttributes(AbstractNettyStreamClient client) throws InterruptedException, IOException, TimeoutException {
RestRequest r = new RestRequestBuilder(URI.create("http://localhost")).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
RequestContext requestContext = new RequestContext();
client.streamRequest(Messages.toStreamRequest(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" + actualRemoteAddress);
if (client instanceof HttpNettyStreamClient) {
Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_1_1);
} else if (client instanceof Http2NettyStreamClient) {
Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_2);
} else {
Assert.fail("Unexpected client instance type");
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testShutdown.
@Test(dataProvider = "shutdownClients")
public void testShutdown(AbstractNettyStreamClient client) throws Exception {
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
// Now verify a new request will also fail
RestRequest r = new RestRequestBuilder(URI.create("http://no.such.host.linkedin.com")).build();
FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), new TransportCallbackAdapter<StreamResponse>(callback));
try {
callback.get(30, TimeUnit.SECONDS);
} catch (ExecutionException e) {
// Expected
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testHeaderSize.
public void testHeaderSize(AbstractNettyStreamClient client, int headerSize, int expectedResult) throws Exception {
Server server = new HttpServerBuilder().headerSize(headerSize).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);
cb.get(300, TimeUnit.SECONDS);
if (expectedResult == TOO_LARGE) {
Assert.fail("Max header size exceeded, expected exception. ");
}
} catch (ExecutionException e) {
if (expectedResult == RESPONSE_OK) {
Assert.fail("Unexpected ExecutionException, header was <= max header size.", e);
}
if (client instanceof HttpNettyStreamClient) {
verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
} else if (client instanceof Http2NettyStreamClient) {
verifyCauseChain(e, RemoteInvocationException.class, Http2Exception.class);
} else {
Assert.fail("Unrecognized client");
}
} finally {
server.stop();
}
}
Aggregations