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();
}
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();
}
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();
}
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));
}
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
}
Aggregations