use of java.util.concurrent.ConcurrentLinkedQueue in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testGetExceedMaxWaiters.
@Test
public void testGetExceedMaxWaiters() throws Exception {
final int maxWaiters = 5;
final CountDownLatch latch = new CountDownLatch(1);
final LifecycleMock lifecycle = new LifecycleMock();
lifecycle.setCreateConsumer(callback -> {
try {
latch.await();
callback.onSuccess(new Object());
} catch (Exception e) {
callback.onError(e);
}
});
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, maxWaiters);
pool.start();
CountDownLatch getLatch = new CountDownLatch(maxWaiters - 1);
ConcurrentLinkedQueue<FutureCallback<Object>> callbacks = new ConcurrentLinkedQueue<>();
for (int i = 0; i < maxWaiters; i++) {
SCHEDULER.execute(() -> {
FutureCallback<Object> callback = new FutureCallback<>();
Cancellable cancellable = pool.get(callback);
Assert.assertNotNull(cancellable);
callbacks.add(callback);
getLatch.countDown();
});
}
getLatch.await(GET_TIMEOUT, TIME_UNIT);
FutureCallback<Object> waiterCallback = new FutureCallback<>();
Cancellable cancellable = pool.get(waiterCallback);
Assert.assertNotNull(cancellable);
Assert.assertFalse(cancellable.cancel());
try {
waiterCallback.get(GET_TIMEOUT, TIME_UNIT);
Assert.fail("Callback should fail but did not");
} catch (ExecutionException e) {
// Exception is recoverable and expected
}
latch.countDown();
callbacks.forEach(callback -> {
try {
Object item = callback.get();
Assert.assertNotNull(item);
pool.put(item);
} catch (Exception e) {
Assert.fail("Unexpected exception during #get()");
}
});
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
use of java.util.concurrent.ConcurrentLinkedQueue in project syncany by syncany.
the class Issue429ScenarioTest method testSameFileDifferentNameFuzzy.
@Ignore
public void testSameFileDifferentNameFuzzy() throws Exception {
for (int seed = 0; seed < 1000; seed++) {
LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();
TestClient clientA = new TestClient("A", testConnection);
TestClient clientB = new TestClient("B", testConnection);
Random randomA = new Random(2 * seed);
Random randomB = new Random(2 * seed + 1);
Queue<String> queue = new ConcurrentLinkedQueue<>();
activeThread A = new activeThread(randomA, clientA, queue);
activeThread B = new activeThread(randomB, clientB, queue);
Thread AThread = new Thread(A, "A");
Thread BThread = new Thread(B, "B");
try {
AThread.start();
BThread.start();
for (int i = 0; i < 50; i++) {
TestClient clientC = new TestClient("C", testConnection);
clientC.down();
if (!AThread.isAlive() || !BThread.isAlive()) {
throw new RuntimeException("One of the threads died");
}
FileUtils.deleteDirectory(clientC.getLocalFile(""));
Thread.sleep(2000);
}
AThread.interrupt();
BThread.interrupt();
} catch (Exception e) {
logger.log(Level.INFO, "Queue:" + queue.toString());
logger.log(Level.INFO, "Something went wrong at seed: " + seed);
throw e;
}
clientA.deleteTestData();
clientB.deleteTestData();
}
}
use of java.util.concurrent.ConcurrentLinkedQueue in project titan by thinkaurelius.
the class IDAuthorityTest method testManyThreadsOneIDAuthority.
@Test
public void testManyThreadsOneIDAuthority() throws BackendException, InterruptedException, ExecutionException {
ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
final IDAuthority targetAuthority = idAuthorities[0];
targetAuthority.setIDBlockSizer(new InnerIDBlockSizer());
final int targetPartition = 0;
final int targetNamespace = 2;
final ConcurrentLinkedQueue<IDBlock> blocks = new ConcurrentLinkedQueue<IDBlock>();
final int blocksPerThread = 40;
Assert.assertTrue(0 < blocksPerThread);
List<Future<Void>> futures = new ArrayList<Future<Void>>(CONCURRENCY);
// Start some concurrent threads getting blocks the same ID authority and same partition in that authority
for (int c = 0; c < CONCURRENCY; c++) {
futures.add(es.submit(new Callable<Void>() {
@Override
public Void call() {
try {
getBlock();
} catch (BackendException e) {
throw new RuntimeException(e);
}
return null;
}
private void getBlock() throws BackendException {
for (int i = 0; i < blocksPerThread; i++) {
IDBlock block = targetAuthority.getIDBlock(targetPartition, targetNamespace, GET_ID_BLOCK_TIMEOUT);
Assert.assertNotNull(block);
blocks.add(block);
}
}
}));
}
for (Future<Void> f : futures) {
try {
f.get();
} catch (ExecutionException e) {
throw e;
}
}
es.shutdownNow();
assertEquals(blocksPerThread * CONCURRENCY, blocks.size());
LongSet ids = new LongHashSet((int) blockSize * blocksPerThread * CONCURRENCY);
for (IDBlock block : blocks) checkBlock(block, ids);
}
use of java.util.concurrent.ConcurrentLinkedQueue in project titan by thinkaurelius.
the class IDAuthorityTest method testMultiIDAcquisition.
@Test
public void testMultiIDAcquisition() throws Throwable {
final int numPartitions = MAX_NUM_PARTITIONS;
final int numAcquisitionsPerThreadPartition = 100;
final IDBlockSizer blockSizer = new InnerIDBlockSizer();
for (int i = 0; i < CONCURRENCY; i++) idAuthorities[i].setIDBlockSizer(blockSizer);
final List<ConcurrentLinkedQueue<IDBlock>> ids = new ArrayList<ConcurrentLinkedQueue<IDBlock>>(numPartitions);
for (int i = 0; i < numPartitions; i++) {
ids.add(new ConcurrentLinkedQueue<IDBlock>());
}
final int maxIterations = numAcquisitionsPerThreadPartition * numPartitions * 2;
final Collection<Future<?>> futures = new ArrayList<Future<?>>(CONCURRENCY);
ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
Set<String> uids = new HashSet<String>(CONCURRENCY);
for (int i = 0; i < CONCURRENCY; i++) {
final IDAuthority idAuthority = idAuthorities[i];
final IDStressor stressRunnable = new IDStressor(numAcquisitionsPerThreadPartition, numPartitions, maxIterations, idAuthority, ids);
uids.add(idAuthority.getUniqueID());
futures.add(es.submit(stressRunnable));
}
// If this fails, it's likely to be a bug in the test rather than the
// IDAuthority (the latter is technically possible, just less likely)
assertEquals(CONCURRENCY, uids.size());
for (Future<?> f : futures) {
try {
f.get();
} catch (ExecutionException e) {
throw e.getCause();
}
}
for (int i = 0; i < numPartitions; i++) {
ConcurrentLinkedQueue<IDBlock> list = ids.get(i);
assertEquals(numAcquisitionsPerThreadPartition * CONCURRENCY, list.size());
LongSet idset = new LongHashSet((int) blockSize * list.size());
for (IDBlock block : list) checkBlock(block, idset);
}
es.shutdownNow();
}
use of java.util.concurrent.ConcurrentLinkedQueue in project j2objc by google.
the class ConcurrentLinkedQueueTest method testWeaklyConsistentIteration.
/**
* Modifications do not cause iterators to fail
*/
public void testWeaklyConsistentIteration() {
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
q.add(one);
q.add(two);
q.add(three);
for (Iterator it = q.iterator(); it.hasNext(); ) {
q.remove();
it.next();
}
assertEquals("queue should be empty again", 0, q.size());
}
Aggregations