Search in sources :

Example 76 with ConcurrentLinkedQueue

use of java.util.concurrent.ConcurrentLinkedQueue in project pinpoint by naver.

the class DefaultServerMetaDataHolderTest method testRaceConditionWhenAddingAndInteratingServiceInfo.

@Test
public void testRaceConditionWhenAddingAndInteratingServiceInfo() throws InterruptedException {
    // Given
    final CountDownLatch initLatch = new CountDownLatch(THREAD_COUNT);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
    final Queue<Throwable> exceptions = new ConcurrentLinkedQueue<Throwable>();
    final TestableServerMetaDataListener listener = new TestableServerMetaDataListener();
    final ServerMetaDataHolder metaDataContext = new DefaultServerMetaDataHolder(VM_ARGS);
    metaDataContext.addListener(listener);
    metaDataContext.setServerName(SERVER_INFO);
    // When
    final List<ServerMetaData> serverMetaDatas = new Vector<ServerMetaData>(THREAD_COUNT / 2);
    for (int i = 0; i < THREAD_COUNT; ++i) {
        if (i % 2 == 0) {
            final String serviceName = "/name" + i;
            final List<String> serviceLibs = new ArrayList<String>();
            executorService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    initLatch.countDown();
                    try {
                        startLatch.await();
                        metaDataContext.addServiceInfo(serviceName, serviceLibs);
                        metaDataContext.notifyListeners();
                    } catch (Throwable t) {
                        exceptions.add(t);
                    } finally {
                        endLatch.countDown();
                    }
                    return null;
                }
            });
        } else {
            executorService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    initLatch.countDown();
                    try {
                        startLatch.await();
                        ServerMetaData serverMetaData = listener.getServerMetaData();
                        serverMetaDatas.add(serverMetaData);
                    } catch (Throwable t) {
                        exceptions.add(t);
                    } finally {
                        endLatch.countDown();
                    }
                    return null;
                }
            });
        }
    }
    initLatch.await();
    startLatch.countDown();
    endLatch.await();
    // Then
    assertTrue("Failed with exceptions : " + exceptions, exceptions.isEmpty());
    ServerMetaData metaData = listener.getServerMetaData();
    assertEquals(metaData.getServerInfo(), SERVER_INFO);
    assertEquals(metaData.getVmArgs(), VM_ARGS);
    assertEquals(metaData.getServiceInfos().size(), THREAD_COUNT / 2);
    assertEquals(serverMetaDatas.size(), THREAD_COUNT / 2);
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerMetaDataHolder(com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Vector(java.util.Vector) ServerMetaData(com.navercorp.pinpoint.bootstrap.context.ServerMetaData) Test(org.junit.Test)

Example 77 with ConcurrentLinkedQueue

use of java.util.concurrent.ConcurrentLinkedQueue in project redisson by redisson.

the class RedissonLiveObjectServiceTest method testNoTransformation.

@Test
public void testNoTransformation() {
    RLiveObjectService service = redisson.getLiveObjectService();
    TestClassNoTransformation ts = new TestClassNoTransformation();
    ts = service.persist(ts);
    HashMap<String, String> m = new HashMap<>();
    ts.setContent(m);
    assertTrue(HashMap.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RMap.class.isAssignableFrom(ts.getContent().getClass()));
    HashSet<String> s = new HashSet<>();
    ts.setContent(s);
    assertTrue(HashSet.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RSet.class.isAssignableFrom(ts.getContent().getClass()));
    TreeSet<String> ss = new TreeSet<>();
    ts.setContent(ss);
    assertTrue(TreeSet.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RSortedSet.class.isAssignableFrom(ts.getContent().getClass()));
    ArrayList<String> al = new ArrayList<>();
    ts.setContent(al);
    assertTrue(ArrayList.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RList.class.isAssignableFrom(ts.getContent().getClass()));
    ConcurrentHashMap<String, String> chm = new ConcurrentHashMap<>();
    ts.setContent(chm);
    assertTrue(ConcurrentHashMap.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RMap.class.isAssignableFrom(ts.getContent().getClass()));
    ArrayBlockingQueue<String> abq = new ArrayBlockingQueue<>(10);
    abq.add("111");
    ts.setContent(abq);
    assertTrue(ArrayBlockingQueue.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RBlockingQueue.class.isAssignableFrom(ts.getContent().getClass()));
    ConcurrentLinkedQueue<String> clq = new ConcurrentLinkedQueue<>();
    ts.setContent(clq);
    assertTrue(ConcurrentLinkedQueue.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RQueue.class.isAssignableFrom(ts.getContent().getClass()));
    LinkedBlockingDeque<String> lbdq = new LinkedBlockingDeque<>();
    ts.setContent(lbdq);
    assertTrue(LinkedBlockingDeque.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RBlockingDeque.class.isAssignableFrom(ts.getContent().getClass()));
    LinkedList<String> ll = new LinkedList<>();
    ts.setContent(ll);
    assertTrue(LinkedList.class.isAssignableFrom(ts.getContent().getClass()));
    assertFalse(RDeque.class.isAssignableFrom(ts.getContent().getClass()));
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RQueue(org.redisson.api.RQueue) ArrayList(java.util.ArrayList) RMap(org.redisson.api.RMap) RBlockingQueue(org.redisson.api.RBlockingQueue) RLiveObjectService(org.redisson.api.RLiveObjectService) RBlockingDeque(org.redisson.api.RBlockingDeque) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) RSet(org.redisson.api.RSet) TreeSet(java.util.TreeSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet) LinkedList(java.util.LinkedList) RDeque(org.redisson.api.RDeque) RList(org.redisson.api.RList) RSortedSet(org.redisson.api.RSortedSet) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 78 with ConcurrentLinkedQueue

use of java.util.concurrent.ConcurrentLinkedQueue in project redisson by redisson.

the class RedissonFairLockTest method testConcurrency_MultiInstance_Ordering.

@Test
public void testConcurrency_MultiInstance_Ordering() throws InterruptedException {
    final ConcurrentLinkedQueue<Thread> queue = new ConcurrentLinkedQueue<>();
    final AtomicInteger lockedCounter = new AtomicInteger();
    int totalThreads = Runtime.getRuntime().availableProcessors() * 2;
    for (int i = 0; i < totalThreads; i++) {
        Thread t1 = new Thread(() -> {
            Lock lock = redisson.getFairLock("testConcurrency_MultiInstance2");
            queue.add(Thread.currentThread());
            lock.lock();
            Thread t = queue.poll();
            assertThat(t).isEqualTo(Thread.currentThread());
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            lockedCounter.incrementAndGet();
            lock.unlock();
        });
        Thread.sleep(10);
        t1.start();
    }
    await().atMost(30, TimeUnit.SECONDS).until(() -> assertThat(lockedCounter.get()).isEqualTo(totalThreads));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Lock(java.util.concurrent.locks.Lock) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 79 with ConcurrentLinkedQueue

use of java.util.concurrent.ConcurrentLinkedQueue in project camel by apache.

the class BackpressureStrategyTest method testBackpressureBufferStrategy.

@Test
public void testBackpressureBufferStrategy() 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");
        }
    }.addRoutesToCamelContext(context);
    Flowable<Integer> integers = Flowable.fromPublisher(CamelReactiveStreams.get(context).fromStream("integers", Integer.class));
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    CountDownLatch latch = new CountDownLatch(1);
    Flowable.interval(0, 50, TimeUnit.MILLISECONDS).zipWith(integers, (l, i) -> i).timeout(2000, TimeUnit.MILLISECONDS, Flowable.empty()).doOnComplete(latch::countDown).subscribe(queue::add);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals(20, queue.size());
    int num = 1;
    for (int i : queue) {
        assertEquals(num++, i);
    }
}
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) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 80 with ConcurrentLinkedQueue

use of java.util.concurrent.ConcurrentLinkedQueue 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)

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