Search in sources :

Example 1 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestCapRepFilter method testStreamResponse.

@Test
public void testStreamResponse() throws IOException {
    Path dirPath = Files.createTempDirectory("caprep-test");
    StreamCountFilter after = new StreamCountFilter();
    StreamCountFilter before = new StreamCountFilter();
    CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
    FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), before, _filter, after);
    RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
    RestResponse myResponse = new RestResponseBuilder().setStatus(201).setEntity("321".getBytes()).build();
    _filter.capture(dirPath.toString());
    RequestContext requestContext = new RequestContext();
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
    FilterUtil.fireStreamResponse(fc, Messages.toStreamResponse(myResponse), requestContext, FilterUtil.emptyWireAttrs());
    Assert.assertEquals(after.getStreamReqCount(), 1);
    Assert.assertEquals(after.getStreamResCount(), 1);
    Assert.assertEquals(before.getStreamReqCount(), 1);
    Assert.assertEquals(before.getStreamResCount(), 1);
    lastCallFilter.reset();
    _filter.passThrough();
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
    Assert.assertEquals(after.getStreamReqCount(), 2);
    Assert.assertEquals(after.getStreamResCount(), 1);
    Assert.assertEquals(before.getStreamReqCount(), 2);
    Assert.assertEquals(before.getStreamResCount(), 1);
    Assert.assertNull(lastCallFilter.getLastRes());
    _filter.replay(dirPath.toString());
    FilterUtil.fireSimpleStreamRequest(fc);
    Assert.assertNull(lastCallFilter.getLastRes());
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), new RequestContext(), FilterUtil.emptyWireAttrs());
    Assert.assertEquals(lastCallFilter.getLastRes(), myResponse);
    Assert.assertEquals(after.getStreamReqCount(), 3);
    Assert.assertEquals(after.getStreamResCount(), 1);
    Assert.assertEquals(before.getStreamReqCount(), 4);
    Assert.assertEquals(before.getStreamResCount(), 2);
}
Also used : Path(java.nio.file.Path) StreamCountFilter(com.linkedin.r2.testutils.filter.StreamCountFilter) RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 2 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestCapRepFilter method testStreamException.

@Test
public void testStreamException() throws IOException {
    Path dirPath = Files.createTempDirectory("caprep-test");
    CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
    FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), _filter);
    RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
    RestResponse myErrorResponse = new RestResponseBuilder().setStatus(400).setEntity("321".getBytes()).build();
    RestException myRestException = new RestException(myErrorResponse);
    _filter.capture(dirPath.toString());
    RequestContext requestContext = new RequestContext();
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
    FilterUtil.fireStreamError(fc, Messages.toStreamException(myRestException), requestContext, FilterUtil.emptyWireAttrs());
    lastCallFilter.reset();
    _filter.replay(dirPath.toString());
    FilterUtil.fireSimpleStreamRequest(fc);
    Assert.assertNull(lastCallFilter.getLastErr());
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
    Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
    Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
Also used : Path(java.nio.file.Path) RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 3 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestReplaceableFilter method testStreamingPassThrough.

@Test
public void testStreamingPassThrough() {
    ReplaceableFilter filter = getFilter();
    CountRequestChunksFilter countRequestChunksFilter = new CountRequestChunksFilter();
    CountResponseChunksFilter countResponseChunksFilter = new CountResponseChunksFilter();
    FilterChain filterChain = FilterChains.empty().addLast(countResponseChunksFilter).addLast(filter).addLast(countRequestChunksFilter);
    int requestChunks = 100;
    int responseChunks = 200;
    StreamRequest streamRequest = new StreamRequestBuilder(URI.create("/test")).build(EntityStreams.newEntityStream(new Writer() {

        private WriteHandle _wh;

        int _count = 0;

        @Override
        public void onInit(WriteHandle wh) {
            _wh = wh;
        }

        @Override
        public void onWritePossible() {
            while (_wh.remaining() > 0) {
                if (_count < requestChunks) {
                    _wh.write(ByteString.copy(new byte[10]));
                    _count++;
                } else {
                    _wh.done();
                }
            }
        }

        @Override
        public void onAbort(Throwable e) {
        }
    }));
    StreamResponse streamResponse = new StreamResponseBuilder().build(EntityStreams.newEntityStream(new Writer() {

        private WriteHandle _wh;

        int _count = 0;

        @Override
        public void onInit(WriteHandle wh) {
            _wh = wh;
        }

        @Override
        public void onWritePossible() {
            while (_wh.remaining() > 0) {
                if (_count < responseChunks) {
                    _wh.write(ByteString.copy(new byte[10]));
                    _count++;
                } else {
                    _wh.done();
                }
            }
        }

        @Override
        public void onAbort(Throwable e) {
        }
    }));
    FilterUtil.fireStreamRequest(filterChain, streamRequest);
    FilterUtil.fireStreamResponse(filterChain, streamResponse, new RequestContext(), new HashMap<>());
    Assert.assertEquals(countRequestChunksFilter.getRequestChunks(), requestChunks);
    Assert.assertEquals(countResponseChunksFilter.getResponseChunks(), responseChunks);
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) WriteHandle(com.linkedin.r2.message.stream.entitystream.WriteHandle) ReplaceableFilter(com.linkedin.r2.caprep.ReplaceableFilter) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) Writer(com.linkedin.r2.message.stream.entitystream.Writer) BaseFilterTest(com.linkedin.r2.testutils.filter.BaseFilterTest) Test(org.testng.annotations.Test)

Example 4 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class AbstractReplayFilterTest method testReplayWithNoMatch.

@Test
public void testReplayWithNoMatch() {
    final RestRequest req = request();
    final RestResponse res = response();
    final CaptureLastCallFilter captureFilter = new CaptureLastCallFilter();
    final FilterChain fc = getFilterChain().addFirstRest(captureFilter);
    FilterUtil.fireUntypedRequestResponse(fc, req, res);
    Assert.assertEquals(res, captureFilter.getLastRes());
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) Test(org.testng.annotations.Test)

Example 5 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestServerTimeout method setup.

@BeforeClass
public void setup() throws IOException {
    _clientFactory = new HttpClientFactory();
    Map<String, Object> clientProperties = new HashMap<String, Object>();
    clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(SERVER_IOHANDLER_TIMEOUT * 20));
    clientProperties.put(HttpClientFactory.HTTP_POOL_MIN_SIZE, "1");
    clientProperties.put(HttpClientFactory.HTTP_POOL_SIZE, "1");
    _client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
    final Map<URI, StreamRequestHandler> handlers = new HashMap<URI, StreamRequestHandler>();
    handlers.put(BUGGY_SERVER_URI, new BuggyRequestHandler());
    handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
    handlers.put(BUGGY_FILTER_URI, new NormalHandler());
    TransportDispatcher transportDispatcher = new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            throw new UnsupportedOperationException("This dispatcher only supports stream");
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            StreamRequestHandler handler = handlers.get(req.getURI());
            if (handler != null) {
                handler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
            } else {
                req.getEntityStream().setReader(new DrainReader());
                callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
            }
        }
    };
    FilterChain filterChain = FilterChains.createStreamChain(new BuggyFilter());
    _server = new HttpServerFactory(filterChain).createRAPServer(PORT, transportDispatcher, SERVER_IOHANDLER_TIMEOUT, true);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) HashMap(java.util.HashMap) FilterChain(com.linkedin.r2.filter.FilterChain) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RequestContext(com.linkedin.r2.message.RequestContext) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) Map(java.util.Map) HashMap(java.util.HashMap) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

FilterChain (com.linkedin.r2.filter.FilterChain)45 Test (org.testng.annotations.Test)38 RequestContext (com.linkedin.r2.message.RequestContext)28 RestRequest (com.linkedin.r2.message.rest.RestRequest)20 RestResponse (com.linkedin.r2.message.rest.RestResponse)17 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)15 HashMap (java.util.HashMap)12 ByteString (com.linkedin.data.ByteString)11 Map (java.util.Map)11 RestException (com.linkedin.r2.message.rest.RestException)10 RestCountFilter (com.linkedin.r2.testutils.filter.RestCountFilter)10 StreamCountFilter (com.linkedin.r2.testutils.filter.StreamCountFilter)10 NextFilter (com.linkedin.r2.filter.NextFilter)9 Callback (com.linkedin.common.callback.Callback)8 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)8 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 CaptureLastCallFilter (com.linkedin.r2.testutils.filter.CaptureLastCallFilter)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)5