Search in sources :

Example 86 with ConcurrentLinkedQueue

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

Example 87 with ConcurrentLinkedQueue

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();
    }
}
Also used : LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) Random(java.util.Random) TestClient(org.syncany.tests.util.TestClient) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Ignore(org.junit.Ignore)

Example 88 with ConcurrentLinkedQueue

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);
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) Callable(java.util.concurrent.Callable) LongHashSet(com.carrotsearch.hppc.LongHashSet) ConsistentKeyIDAuthority(com.thinkaurelius.titan.diskstorage.idmanagement.ConsistentKeyIDAuthority) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 89 with ConcurrentLinkedQueue

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();
}
Also used : IDBlockSizer(com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer) LongSet(com.carrotsearch.hppc.LongSet) LongHashSet(com.carrotsearch.hppc.LongHashSet) ConsistentKeyIDAuthority(com.thinkaurelius.titan.diskstorage.idmanagement.ConsistentKeyIDAuthority) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException) LongHashSet(com.carrotsearch.hppc.LongHashSet) Test(org.junit.Test)

Example 90 with ConcurrentLinkedQueue

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());
}
Also used : Iterator(java.util.Iterator) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

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