Search in sources :

Example 1 with TestSubscriber

use of org.apache.camel.component.reactive.streams.support.TestSubscriber in project camel by apache.

the class BackpressureStrategyTest method testBackpressureDropStrategy.

@Test
public void testBackpressureDropStrategy() throws Exception {
    ReactiveStreamsComponent comp = (ReactiveStreamsComponent) context().getComponent("reactive-streams");
    comp.setBackpressureStrategy(ReactiveStreamsBackpressureStrategy.DROP);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers");
        }
    }.addRoutesToCamelContext(context);
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer o) {
            queue.add(o);
            latch.countDown();
            latch2.countDown();
        }
    };
    subscriber.setInitiallyRequested(1);
    CamelReactiveStreams.get(context).fromStream("integers", Integer.class).subscribe(subscriber);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // wait for all numbers to be generated
    Thread.sleep(1000);
    subscriber.request(19);
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    // add other time to ensure no other items arrive
    Thread.sleep(200);
    assertEquals(2, queue.size());
    int sum = queue.stream().reduce((i, j) -> i + j).get();
    // 1 + 2 = 3
    assertEquals(3, sum);
    subscriber.cancel();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with TestSubscriber

use of org.apache.camel.component.reactive.streams.support.TestSubscriber in project camel by apache.

the class BackpressureStrategyTest method testBackpressureDropStrategyInEndpoint.

@Test
public void testBackpressureDropStrategyInEndpoint() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers?backpressureStrategy=DROP");
        }
    }.addRoutesToCamelContext(context);
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer o) {
            queue.add(o);
            latch.countDown();
            latch2.countDown();
        }
    };
    subscriber.setInitiallyRequested(1);
    CamelReactiveStreams.get(context).fromStream("integers", Integer.class).subscribe(subscriber);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // wait for all numbers to be generated
    Thread.sleep(1000);
    subscriber.request(19);
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    // add other time to ensure no other items arrive
    Thread.sleep(200);
    assertEquals(2, queue.size());
    int sum = queue.stream().reduce((i, j) -> i + j).get();
    // 1 + 2 = 3
    assertEquals(3, sum);
    subscriber.cancel();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with TestSubscriber

use of org.apache.camel.component.reactive.streams.support.TestSubscriber in project camel by apache.

the class BackpressureStrategyTest method testBackpressureLatestStrategy.

@Test
public void testBackpressureLatestStrategy() throws Exception {
    ReactiveStreamsComponent comp = (ReactiveStreamsComponent) context().getComponent("reactive-streams");
    comp.setBackpressureStrategy(ReactiveStreamsBackpressureStrategy.LATEST);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers");
        }
    }.addRoutesToCamelContext(context);
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer o) {
            queue.add(o);
            latch.countDown();
            latch2.countDown();
        }
    };
    subscriber.setInitiallyRequested(1);
    CamelReactiveStreams.get(context).fromStream("integers", Integer.class).subscribe(subscriber);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // wait for all numbers to be generated
    Thread.sleep(1000);
    subscriber.request(19);
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    // add other time to ensure no other items arrive
    Thread.sleep(200);
    assertEquals(2, queue.size());
    int sum = queue.stream().reduce((i, j) -> i + j).get();
    // 1 + 20 = 21
    assertEquals(21, sum);
    subscriber.cancel();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with TestSubscriber

use of org.apache.camel.component.reactive.streams.support.TestSubscriber in project camel by apache.

the class BackpressurePublisherRoutePolicyTest method testThatRouteRestartsOnUnsubscription.

@Test
public void testThatRouteRestartsOnUnsubscription() throws Exception {
    CountDownLatch generationLatch = new CountDownLatch(25);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            ThrottlingInflightRoutePolicy policy = new ThrottlingInflightRoutePolicy();
            policy.setMaxInflightExchanges(10);
            policy.setScope(ThrottlingInflightRoutePolicy.ThrottlingScope.Route);
            policy.setResumePercentOfMax(70);
            policy.setLoggingLevel(LoggingLevel.INFO);
            // unbounded
            from("timer:tick?period=50").id("policy-route").routePolicy(policy).process(x -> generationLatch.countDown()).to("reactive-streams:pub");
        }
    }.addRoutesToCamelContext(context);
    CountDownLatch receptionLatch = new CountDownLatch(35);
    Publisher<Exchange> pub = CamelReactiveStreams.get(context()).fromStream("pub", Exchange.class);
    TestSubscriber<Exchange> subscriber = new TestSubscriber<Exchange>() {

        @Override
        public void onNext(Exchange o) {
            super.onNext(o);
            receptionLatch.countDown();
        }
    };
    subscriber.setInitiallyRequested(10);
    pub.subscribe(subscriber);
    // Add another (fast) subscription that should not affect the backpressure on the route
    Observable.fromPublisher(pub).subscribe();
    context.start();
    // after 25 messages are generated
    generationLatch.await(5, TimeUnit.SECONDS);
    // The number of exchanges should be 10 (requested by the subscriber), so 35-10=25
    assertEquals(25, receptionLatch.getCount());
    // fire a delayed request from the subscriber (required by camel core)
    subscriber.request(1);
    Thread.sleep(250);
    StatefulService service = (StatefulService) context().getRoute("policy-route").getConsumer();
    // ensure the route is stopped or suspended
    assertTrue(service.isStopped() || service.isSuspended());
    subscriber.cancel();
    // request other exchanges to ensure that the route works
    CountDownLatch latch = new CountDownLatch(20);
    Observable.fromPublisher(pub).subscribe(n -> {
        latch.countDown();
    });
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Exchange(org.apache.camel.Exchange) ThrottlingInflightRoutePolicy(org.apache.camel.impl.ThrottlingInflightRoutePolicy) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) StatefulService(org.apache.camel.StatefulService) Test(org.junit.Test)

Example 5 with TestSubscriber

use of org.apache.camel.component.reactive.streams.support.TestSubscriber in project camel by apache.

the class BackpressurePublisherRoutePolicyTest method testThatBackpressureCausesTemporaryRouteStop.

@Test
public void testThatBackpressureCausesTemporaryRouteStop() throws Exception {
    CountDownLatch generationLatch = new CountDownLatch(25);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            ThrottlingInflightRoutePolicy policy = new ThrottlingInflightRoutePolicy();
            policy.setMaxInflightExchanges(10);
            policy.setScope(ThrottlingInflightRoutePolicy.ThrottlingScope.Route);
            policy.setResumePercentOfMax(70);
            policy.setLoggingLevel(LoggingLevel.INFO);
            from("timer:tick?period=50&repeatCount=35").id("policy-route").routePolicy(policy).process(x -> generationLatch.countDown()).to("reactive-streams:pub");
        }
    }.addRoutesToCamelContext(context);
    CountDownLatch receptionLatch = new CountDownLatch(35);
    Publisher<Exchange> pub = CamelReactiveStreams.get(context()).fromStream("pub", Exchange.class);
    TestSubscriber<Exchange> subscriber = new TestSubscriber<Exchange>() {

        @Override
        public void onNext(Exchange o) {
            super.onNext(o);
            receptionLatch.countDown();
        }
    };
    subscriber.setInitiallyRequested(10);
    pub.subscribe(subscriber);
    // Add another (fast) subscription that should not affect the backpressure on the route
    Observable.fromPublisher(pub).subscribe();
    context.start();
    // after 25 messages are generated
    generationLatch.await(5, TimeUnit.SECONDS);
    // The number of exchanges should be 10 (requested by the subscriber), so 35-10=25
    assertEquals(25, receptionLatch.getCount());
    // fire a delayed request from the subscriber (required by camel core)
    subscriber.request(1);
    Thread.sleep(250);
    StatefulService service = (StatefulService) context().getRoute("policy-route").getConsumer();
    // ensure the route is stopped or suspended
    assertTrue(service.isStopped() || service.isSuspended());
    // request all the remaining exchanges
    subscriber.request(24);
    assertTrue(receptionLatch.await(5, TimeUnit.SECONDS));
// The reception latch has gone to 0
}
Also used : Exchange(org.apache.camel.Exchange) ThrottlingInflightRoutePolicy(org.apache.camel.impl.ThrottlingInflightRoutePolicy) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) StatefulService(org.apache.camel.StatefulService) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)5 Exchange (org.apache.camel.Exchange)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)5 TestSubscriber (org.apache.camel.component.reactive.streams.support.TestSubscriber)5 Test (org.junit.Test)5 Flowable (io.reactivex.Flowable)3 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)3 TimeUnit (java.util.concurrent.TimeUnit)3 CamelReactiveStreams (org.apache.camel.component.reactive.streams.api.CamelReactiveStreams)3 CamelTestSupport (org.apache.camel.test.junit4.CamelTestSupport)3 StatefulService (org.apache.camel.StatefulService)2 ThrottlingInflightRoutePolicy (org.apache.camel.impl.ThrottlingInflightRoutePolicy)2