use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testAsyncServer.
@SuppressWarnings({ "unchecked" })
@Test(dataProvider = "restOrStream")
public void testAsyncServer(final RestOrStream restOrStream) throws Exception {
final AsyncStatusCollectionResource statusResource = getMockResource(AsyncStatusCollectionResource.class);
statusResource.get(eq(1L), EasyMock.<Callback<Status>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
Callback<Status> callback = (Callback<Status>) EasyMock.getCurrentArguments()[1];
Status stat = buildStatusRecord();
callback.onSuccess(stat);
return null;
}
});
replay(statusResource);
final Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
assertEquals(restResponse.getStatus(), 200);
assertTrue(restResponse.getEntity().length() > 0);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
@Override
public void onError(Throwable e) {
fail();
}
};
if (restOrStream == RestOrStream.REST) {
final RestRequest request = new RestRequestBuilder(new URI("/asyncstatuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
final StreamRequest streamRequest = new StreamRequestBuilder(new URI("/asyncstatuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
final Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
restResponseCallback.onSuccess(result);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testRequestHandlers.
@Test(dataProvider = "requestHandlersData")
public void testRequestHandlers(URI uri, String expectedResponse) {
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames("com.linkedin.restli.server.twitter");
config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler() {
@Override
public void initialize(RestLiConfig config, Map<String, ResourceModel> rootResources) {
/* no-op */
}
@Override
public void handleRequest(RestRequest req, RequestContext ctx, Callback<RestResponse> cb) {
cb.onSuccess(new RestResponseBuilder().setEntity(toByteString(DOCUMENTATION_RESPONSE)).build());
}
});
config.addCustomRequestHandlers(new CustomRequestHandler());
config.addDebugRequestHandlers(new DebugRequestHandler("a", DEBUG_HANDLER_RESPONSE_A), new DebugRequestHandler("b", DEBUG_HANDLER_RESPONSE_B));
RestLiServer server = new RestLiServer(config, new EasyMockResourceFactory(), createMock(Engine.class));
RestRequest restReq = new RestRequestBuilder(uri).build();
server.handleRequest(restReq, createMock(RequestContext.class), new RestResponseAssertionCallback(expectedResponse));
StreamRequest streamReq = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
server.handleRequest(streamReq, createMock(RequestContext.class), new StreamResponseAssertionCallback(expectedResponse));
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestStreamRequest method testBackPressure.
@Test
public void testBackPressure() throws Exception {
final long totalBytes = SMALL_BYTES_NUM;
TimedBytesWriter writer = new TimedBytesWriter(totalBytes, BYTE);
EntityStream entityStream = EntityStreams.newEntityStream(writer);
StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
_client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
TimedBytesReader reader = _rateLimitedRequestHandler.getReader();
Assert.assertNotNull(reader);
Assert.assertEquals(totalBytes, reader.getTotalBytes());
Assert.assertTrue(reader.allBytesCorrect());
long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
long serverReceiveTimespan = reader.getStopTime() - reader.getStartTime();
Assert.assertTrue(serverReceiveTimespan > 1000);
double diff = Math.abs(serverReceiveTimespan - clientSendTimespan);
double diffRatio = diff / clientSendTimespan;
// make it generous to reduce the chance occasional test failures
Assert.assertTrue(diffRatio < 0.2);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestStreamRequest method test404.
// jetty 404 tests singled out
@Test(enabled = false)
public void test404() throws Exception {
final long totalBytes = TINY_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, URI.create("/boo")));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectErrorCallback(latch, status);
_client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), 404);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestStreamRequest method testErrorWriter.
@Test
public void testErrorWriter() throws Exception {
final long totalBytes = SMALL_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new ErrorWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, FOOBAR_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<>();
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
error.set(e);
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
}
};
_client.streamRequest(request, callback);
latch.await();
Assert.assertNotNull(error.get());
}
Aggregations