Search in sources :

Example 21 with AllocateIdOutput

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput in project genius by opendaylight.

the class IdManagerTest method requestIdsConcurrently.

// OK as exceptionInExecutor can't be Exception & AssertionFailedError
@SuppressWarnings("checkstyle:IllegalThrows")
private void requestIdsConcurrently(boolean isSameKey) throws Throwable {
    int numberOfTasks = 3;
    CountDownLatch latch = new CountDownLatch(numberOfTasks);
    Set<Long> idSet = Sets.newConcurrentHashSet();
    ExecutorService executor = Executors.newCachedThreadPool("requestIdsConcurrently()", LOG);
    AtomicReference<Throwable> exceptionInExecutorAtomic = new AtomicReference<>();
    for (int i = 0; i < numberOfTasks; i++) {
        final String idKey;
        if (isSameKey) {
            idKey = TEST_KEY1;
        } else {
            idKey = TEST_KEY1 + i;
        }
        executor.execute(() -> {
            // Any exception thrown inside this background thread will not cause the test to fail
            // so you cannot use assert* here but must set the exceptionInExecutor which is checked after
            Future<RpcResult<AllocateIdOutput>> result;
            result = idManagerService.allocateId(new AllocateIdInputBuilder().setPoolName(ID_POOL_NAME).setIdKey(idKey).build());
            try {
                if (result.get().isSuccessful()) {
                    Long idValue = result.get().getResult().getIdValue();
                    idSet.add(idValue);
                    if (idValue > ID_LOW + BLOCK_SIZE) {
                        exceptionInExecutorAtomic.set(new AssertionFailedError("idValue <= ID_LOW + BLOCK_SIZE"));
                    }
                } else {
                    RpcError error = result.get().getErrors().iterator().next();
                    if (!error.getCause().getMessage().contains("Ids exhausted for pool : " + ID_POOL_NAME)) {
                        exceptionInExecutorAtomic.set(error.getCause());
                    }
                }
            } catch (InterruptedException | ExecutionException e) {
                exceptionInExecutorAtomic.set(e);
            } finally {
                latch.countDown();
            }
        });
    }
    if (!latch.await(13, SECONDS)) {
        fail("latch.await(13, SECONDS) timed out :(");
    }
    Throwable exceptionInExecutor = exceptionInExecutorAtomic.get();
    if (exceptionInExecutor != null) {
        throw exceptionInExecutor;
    }
    if (isSameKey) {
        assertEquals(1, idSet.size());
    } else {
        assertEquals(numberOfTasks, idSet.size());
    }
}
Also used : RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) RpcError(org.opendaylight.yangtools.yang.common.RpcError) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AllocateIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder) ExecutorService(java.util.concurrent.ExecutorService) AssertionFailedError(junit.framework.AssertionFailedError) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)20 AllocateIdInputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder)20 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)20 AllocateIdInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput)19 AllocateIdOutput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput)19 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AllocateIdOutputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutputBuilder)2 Optional (com.google.common.base.Optional)1 Futures (com.google.common.util.concurrent.Futures)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BigInteger (java.math.BigInteger)1 Collections (java.util.Collections)1 Comparator.comparing (java.util.Comparator.comparing)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1