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