int total = 6;
int parallel = 2;
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(total + 1);
HazelcastInstance instance = nodeFactory.newHazelcastInstance();
final String name = "testAtomicLongSpawnNodeInParallel";
IAtomicLong atomicLong = instance.getAtomicLong(name);
atomicLong.set(100);
final ExecutorService ex = Executors.newFixedThreadPool(parallel);
try {
for (int i = 0; i < total / parallel; i++) {
final HazelcastInstance[] instances = new HazelcastInstance[parallel];
final CountDownLatch countDownLatch = new CountDownLatch(parallel);
final AtomicInteger exceptionCount = new AtomicInteger(0);
for (int j = 0; j < parallel; j++) {
final int id = j;
ex.execute(new Runnable() {
public void run() {
try {
instances[id] = nodeFactory.newHazelcastInstance();
instances[id].getAtomicLong(name).incrementAndGet();
} catch (Exception e) {
exceptionCount.incrementAndGet();
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}
});
}
assertOpenEventually(countDownLatch);
// if there is an exception while incrementing in parallel threads, find number of exceptions
// and subtract the number from expectedValue.
final int thrownExceptionCount = exceptionCount.get();
final long expectedValue = (long) 100 + (i + 1) * parallel - thrownExceptionCount;
IAtomicLong newAtomicLong = instance.getAtomicLong(name);
assertEquals(expectedValue, newAtomicLong.get());
instance.shutdown();
instance = instances[0];
}
} finally {
ex.shutdownNow();