Search in sources :

Example 81 with ConcurrentLinkedQueue

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();
}
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 82 with ConcurrentLinkedQueue

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();
}
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 83 with ConcurrentLinkedQueue

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);
    }
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 84 with ConcurrentLinkedQueue

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);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 85 with ConcurrentLinkedQueue

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);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)236 Test (org.junit.Test)102 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)56 Watermark (org.apache.flink.streaming.api.watermark.Watermark)52 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)43 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)40 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)40 CountDownLatch (java.util.concurrent.CountDownLatch)37 ArrayList (java.util.ArrayList)31 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)28 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)18 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)17 IOException (java.io.IOException)15 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)15 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)14 ExecutionException (java.util.concurrent.ExecutionException)13 ExecutorService (java.util.concurrent.ExecutorService)13 Map (java.util.Map)12 OperatorStateHandles (org.apache.flink.streaming.runtime.tasks.OperatorStateHandles)12 Iterator (java.util.Iterator)11