use of java.util.concurrent.ConcurrentLinkedQueue 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 java.util.concurrent.ConcurrentLinkedQueue 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 java.util.concurrent.ConcurrentLinkedQueue in project cassandra by apache.
the class RefCountedTest method testConcurrentLinkedQueueImpl.
private void testConcurrentLinkedQueueImpl(boolean bugTest) {
final Queue<Object> iterable = new ConcurrentLinkedQueue<Object>();
Pair<Object, Object> p = Pair.create(iterable, iterable);
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = iterable;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<Queue<Object>>(iterable), tidier);
for (int i = 0; i < entryCount; i++) {
iterable.add(p);
}
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("ConcurrentLinkedQueue visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations + " bug test " + bugTest);
if (bugTest) {
//Should have to visit a lot of queue nodes
Assert.assertTrue(visitor.lastVisitedCount > entryCount && visitor.lastVisitedCount < entryCount + fudgeFactor);
//Should have a lot of iterations to walk the queue, but linear to the number of entries
Assert.assertTrue(visitor.iterations > (entryCount * 2) && visitor.iterations < (entryCount * 2) + fudgeFactor);
} else {
//There are almost no objects in this linked list once it's iterated as a collection so visited count
//should be small
Assert.assertTrue(visitor.lastVisitedCount < 10);
//Should have a lot of iterations to walk the collection, but linear to the number of entries
Assert.assertTrue(visitor.iterations > entryCount && visitor.iterations < entryCount + fudgeFactor);
}
}
use of java.util.concurrent.ConcurrentLinkedQueue in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testShutdownWithMultiplePendingDispose.
@Test
public void testShutdownWithMultiplePendingDispose() throws Exception {
final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
pool.start();
final CountDownLatch latch = new CountDownLatch(GET_COUNT);
final Collection<FutureCallback<Object>> getCallbacks = new ConcurrentLinkedQueue<>();
IntStream.range(0, GET_COUNT).forEach(i -> SCHEDULER.execute(() -> {
FutureCallback<Object> getCallback = new FutureCallback<>();
pool.get(getCallback);
getCallbacks.add(getCallback);
latch.countDown();
}));
if (!latch.await(OPERATION_TIMEOUT, TIME_UNIT)) {
Assert.fail("Timeout waiting for get calls");
}
final Collection<Object> items = new ConcurrentLinkedQueue<>();
getCallbacks.stream().forEach(callback -> {
try {
items.add(callback.get(GET_TIMEOUT, TIME_UNIT));
} catch (Exception e) {
}
});
Assert.assertEquals(items.size(), GET_COUNT);
verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
// Put items back to the pool
items.stream().forEach(item -> SCHEDULER.execute(() -> pool.dispose(item)));
shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
use of java.util.concurrent.ConcurrentLinkedQueue in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testShutdownWithMultiplePendingPut.
@Test
public void testShutdownWithMultiplePendingPut() throws Exception {
final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
pool.start();
final CountDownLatch latch = new CountDownLatch(GET_COUNT);
final Collection<FutureCallback<Object>> getCallbacks = new ConcurrentLinkedQueue<>();
IntStream.range(0, GET_COUNT).forEach(i -> SCHEDULER.execute(() -> {
FutureCallback<Object> getCallback = new FutureCallback<>();
pool.get(getCallback);
getCallbacks.add(getCallback);
latch.countDown();
}));
if (!latch.await(OPERATION_TIMEOUT, TIME_UNIT)) {
Assert.fail("Timeout waiting for get calls");
}
final Collection<Object> items = new ConcurrentLinkedQueue<>();
getCallbacks.stream().forEach(callback -> {
try {
items.add(callback.get(GET_TIMEOUT, TIME_UNIT));
} catch (Exception e) {
}
});
Assert.assertEquals(items.size(), GET_COUNT);
verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
// Put items back to the pool
items.stream().forEach(item -> SCHEDULER.execute(() -> pool.put(item)));
shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Aggregations