Search in sources :

Example 1 with ReplaceableFilter

use of com.linkedin.r2.caprep.ReplaceableFilter 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 2 with ReplaceableFilter

use of com.linkedin.r2.caprep.ReplaceableFilter in project rest.li by linkedin.

the class TestReplaceableFilter method testSetAndGet.

@Test
public void testSetAndGet() {
    final ReplaceableFilter filter = getFilter();
    final RestFilter newFilter = new PassThroughFilter();
    Assert.assertTrue(!filter.getFilter().equals(newFilter));
    filter.setFilter(newFilter);
    Assert.assertEquals(newFilter, filter.getFilter());
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ReplaceableFilter(com.linkedin.r2.caprep.ReplaceableFilter) PassThroughFilter(com.linkedin.r2.caprep.PassThroughFilter) BaseFilterTest(com.linkedin.r2.testutils.filter.BaseFilterTest) Test(org.testng.annotations.Test)

Aggregations

ReplaceableFilter (com.linkedin.r2.caprep.ReplaceableFilter)2 BaseFilterTest (com.linkedin.r2.testutils.filter.BaseFilterTest)2 Test (org.testng.annotations.Test)2 PassThroughFilter (com.linkedin.r2.caprep.PassThroughFilter)1 FilterChain (com.linkedin.r2.filter.FilterChain)1 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)1 RequestContext (com.linkedin.r2.message.RequestContext)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)1 WriteHandle (com.linkedin.r2.message.stream.entitystream.WriteHandle)1 Writer (com.linkedin.r2.message.stream.entitystream.Writer)1