use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnResponse.
@Test
public void testRequestFilterAdapterCallsOnResponse() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onResponse(simpleRestResponse(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
StreamResponse capturedReq = _beforeFilter.getResponse();
capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("shouldn't have error");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "12345");
}
}));
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestBuilders method testChainBuildStreamResponseFromStreamResponseBuilder.
@Test
public void testChainBuildStreamResponseFromStreamResponseBuilder() {
final StreamResponse res = new StreamResponseBuilder().setHeader("k1", "v1").setStatus(300).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 1, 2, 3, 4 })))).builder().setHeader("k2", "v2").setStatus(400).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 5, 6, 7, 8 }))));
Messages.toRestResponse(res, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
Assert.assertEquals(new byte[] { 5, 6, 7, 8 }, result.getEntity().copyBytes());
Assert.assertEquals("v1", res.getHeader("k1"));
Assert.assertEquals("v2", res.getHeader("k2"));
Assert.assertEquals(400, res.getStatus());
}
});
}
use of com.linkedin.r2.message.stream.StreamResponse 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.message.stream.StreamResponse 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));
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestDisruptFilter method testSchedulerRejectExecution.
@Test
public void testSchedulerRejectExecution() throws Exception {
ScheduledExecutorService rejectedScheduler = EasyMock.createStrictMock(ScheduledExecutorService.class);
EasyMock.expect(rejectedScheduler.schedule(EasyMock.anyObject(Runnable.class), EasyMock.anyLong(), EasyMock.anyObject(TimeUnit.class))).andThrow(new RejectedExecutionException());
EasyMock.replay(rejectedScheduler);
final RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
final DisruptFilter filter = new DisruptFilter(rejectedScheduler, _executor, REQUEST_TIMEOUT);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
final NextFilter<StreamRequest, StreamResponse> next = new NextFilter<StreamRequest, StreamResponse>() {
@Override
public void onRequest(StreamRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
success.set(true);
latch.countDown();
}
@Override
public void onResponse(StreamResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
latch.countDown();
}
@Override
public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
latch.countDown();
}
};
filter.onStreamRequest(new StreamRequestBuilder(new URI(URI)).build(EntityStreams.emptyStream()), requestContext, Collections.emptyMap(), next);
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
Assert.assertTrue(success.get(), "Unexpected method invocation");
EasyMock.verify(rejectedScheduler);
EasyMock.reset(rejectedScheduler);
}
Aggregations