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