use of com.hazelcast.core.IAtomicLong in project hazelcast by hazelcast.
the class IdGeneratorProxyTest method createIdGenerator.
private static IdGenerator createIdGenerator() {
String name = "id-" + UUID.randomUUID().toString();
IAtomicLong blockGenerator = mockIAtomicLong();
return new IdGeneratorProxy(blockGenerator, name, null, null);
}
use of com.hazelcast.core.IAtomicLong in project hazelcast by hazelcast.
the class AtomicLongAdvancedTest method testMultipleThreadAtomicLong.
@Test
public void testMultipleThreadAtomicLong() throws InterruptedException {
final HazelcastInstance instance = createHazelcastInstance();
final int k = 10;
final CountDownLatch countDownLatch = new CountDownLatch(k);
final IAtomicLong atomicLong = instance.getAtomicLong("testMultipleThreadAtomicLong");
for (int i = 0; i < k; i++) {
new Thread() {
public void run() {
long delta = (long) (Math.random() * 1000);
for (int j = 0; j < 10000; j++) {
atomicLong.addAndGet(delta);
}
for (int j = 0; j < 10000; j++) {
atomicLong.addAndGet(-1 * delta);
}
countDownLatch.countDown();
}
}.start();
}
assertOpenEventually(countDownLatch, 300);
assertEquals(0, atomicLong.get());
}
use of com.hazelcast.core.IAtomicLong in project hazelcast by hazelcast.
the class AtomicLongAdvancedTest method testAtomicLongSpawnNodeInParallel.
@Test
public void testAtomicLongSpawnNodeInParallel() throws InterruptedException {
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);
waitAllForSafeState(nodeFactory.getAllHazelcastInstances());
// 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();
}
}
use of com.hazelcast.core.IAtomicLong in project hazelcast by hazelcast.
the class ExecutorServiceTest method testSubmitToAllMembersRunnable.
@Test
public void testSubmitToAllMembersRunnable() throws Exception {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(NODE_COUNT);
HazelcastInstance[] instances = factory.newInstances(new Config());
final AtomicInteger nullResponseCount = new AtomicInteger(0);
final CountDownLatch responseLatch = new CountDownLatch(NODE_COUNT * NODE_COUNT);
MultiExecutionCallback callback = new MultiExecutionCallback() {
public void onResponse(Member member, Object value) {
if (value == null) {
nullResponseCount.incrementAndGet();
}
responseLatch.countDown();
}
public void onComplete(Map<Member, Object> values) {
}
};
for (int i = 0; i < NODE_COUNT; i++) {
IExecutorService service = instances[i].getExecutorService("testSubmitToAllMembersRunnable");
service.submitToAllMembers(new IncrementAtomicLongRunnable("testSubmitToAllMembersRunnable"), callback);
}
assertTrue(responseLatch.await(30, TimeUnit.SECONDS));
IAtomicLong result = instances[0].getAtomicLong("testSubmitToAllMembersRunnable");
assertEquals(NODE_COUNT * NODE_COUNT, result.get());
assertEquals(NODE_COUNT * NODE_COUNT, nullResponseCount.get());
}
use of com.hazelcast.core.IAtomicLong in project hazelcast by hazelcast.
the class ExecutorServiceTest method testSubmitToMembersCallable.
@Test
public void testSubmitToMembersCallable() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(NODE_COUNT);
HazelcastInstance[] instances = factory.newInstances(new Config());
final AtomicInteger count = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(NODE_COUNT);
MultiExecutionCallback callback = new MultiExecutionCallback() {
public void onResponse(Member member, Object value) {
count.incrementAndGet();
}
public void onComplete(Map<Member, Object> values) {
latch.countDown();
}
};
int sum = 0;
Set<Member> membersSet = instances[0].getCluster().getMembers();
Member[] members = membersSet.toArray(new Member[membersSet.size()]);
Random random = new Random();
String name = "testSubmitToMembersCallable";
for (int i = 0; i < NODE_COUNT; i++) {
IExecutorService service = instances[i].getExecutorService(name);
int n = random.nextInt(NODE_COUNT) + 1;
sum += n;
Member[] m = new Member[n];
System.arraycopy(members, 0, m, 0, n);
service.submitToMembers(new IncrementAtomicLongCallable(name), Arrays.asList(m), callback);
}
assertOpenEventually(latch);
IAtomicLong result = instances[0].getAtomicLong(name);
assertEquals(sum, result.get());
assertEquals(sum, count.get());
}
Aggregations