use of com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter in project rest.li by linkedin.
the class TestQueryTunnel method setUp.
@BeforeClass
protected void setUp() throws Exception {
Map<String, String> clientProperties = new HashMap<String, String>();
clientProperties.put(HttpClientFactory.HTTP_QUERY_POST_THRESHOLD, String.valueOf(QUERY_TUNNEL_THRESHOLD));
clientProperties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, _httpProtocolVersion);
_clientFactory = new HttpClientFactory();
final TransportClient transportClient = _clientFactory.getClient(clientProperties);
_client = new TransportClientAdapter(transportClient, _clientROS);
final RestRequestHandler restHandler = new CheckQueryTunnelHandler();
final StreamRequestHandler streamHandler = new StreamRequestHandlerAdapter(restHandler);
TransportDispatcher dispatcher = new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
restHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<RestResponse>(callback));
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
streamHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
}
};
_server = new HttpServerFactory(_servletType).createH2cServer(_port, dispatcher, _serverROS);
_server.start();
}
use of com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpNettyClient method testFailBackoff.
@Test
public void testFailBackoff() throws Exception {
final int WARM_UP = 10;
final int N = 5;
final int MAX_RATE_LIMITING_PERIOD = 500;
final CountDownLatch warmUpLatch = new CountDownLatch(WARM_UP);
final CountDownLatch latch = new CountDownLatch(N);
final AtomicReference<Boolean> isShutdown = new AtomicReference<>(false);
AsyncPool<Channel> testPool = new AsyncPoolImpl<>("test pool", new AsyncPool.Lifecycle<Channel>() {
@Override
public void create(Callback<Channel> callback) {
if (warmUpLatch.getCount() > 0) {
warmUpLatch.countDown();
} else {
latch.countDown();
}
callback.onError(new Throwable("Oops..."));
}
@Override
public boolean validateGet(Channel obj) {
return false;
}
@Override
public boolean validatePut(Channel obj) {
return false;
}
@Override
public void destroy(Channel obj, boolean error, Callback<Channel> callback) {
}
@Override
public PoolStats.LifecycleStats getStats() {
return null;
}
}, 200, 30000, _scheduler, Integer.MAX_VALUE, AsyncPoolImpl.Strategy.MRU, 0, new ExponentialBackOffRateLimiter(0, MAX_RATE_LIMITING_PERIOD, Math.max(10, MAX_RATE_LIMITING_PERIOD / 32), _scheduler));
HttpNettyClient client = new HttpNettyClient(address -> testPool, _scheduler, 500, 500, 1024 * 1024 * 2);
final RestRequest r = new RestRequestBuilder(URI.create("http://localhost:8080/")).setMethod("GET").build();
final ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
while (!isShutdown.get()) {
try {
FutureCallback<RestResponse> callback = new FutureCallback<>();
client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<RestResponse>(callback));
callback.get();
} catch (Exception e) {
// ignore
}
}
});
// First ensure a bunch fail to get the rate limiting going
warmUpLatch.await(120, TimeUnit.SECONDS);
// Now we should be rate limited
long start = System.currentTimeMillis();
System.err.println("Starting at " + start);
long lowTolerance = N * MAX_RATE_LIMITING_PERIOD * 4 / 5;
long highTolerance = N * MAX_RATE_LIMITING_PERIOD * 5 / 4;
Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
long elapsed = System.currentTimeMillis() - start;
Assert.assertTrue(elapsed > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + elapsed + ")");
// shutdown everything
isShutdown.set(true);
executor.shutdown();
}
use of com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testRestRequests.
@Test(dataProvider = "requestResponseParameters", expectedExceptions = UnsupportedOperationException.class)
public void testRestRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
for (int i = 0; i < REQUEST_COUNT; i++) {
RestRequest request = new RestRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).setEntity(ByteString.copy(new byte[requestSize])).build();
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.restRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse response) {
}
@Override
public void onError(Throwable e) {
}
}));
}
} finally {
server.stop();
}
}
use of com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter 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.transport.common.bridge.server.TransportCallbackAdapter 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");
}
}
Aggregations