Search in sources :

Example 11 with IAtomicLong

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);
}
Also used : IAtomicLongMocks.mockIAtomicLong(com.hazelcast.mock.IAtomicLongMocks.mockIAtomicLong) IAtomicLong(com.hazelcast.core.IAtomicLong)

Example 12 with IAtomicLong

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CountDownLatch(java.util.concurrent.CountDownLatch) IAtomicLong(com.hazelcast.core.IAtomicLong) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 13 with IAtomicLong

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();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) IAtomicLong(com.hazelcast.core.IAtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with IAtomicLong

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());
}
Also used : ExecutorConfig(com.hazelcast.config.ExecutorConfig) Config(com.hazelcast.config.Config) IExecutorService(com.hazelcast.core.IExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) MultiExecutionCallback(com.hazelcast.core.MultiExecutionCallback) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IAtomicLong(com.hazelcast.core.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) Map(java.util.Map) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 15 with IAtomicLong

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());
}
Also used : ExecutorConfig(com.hazelcast.config.ExecutorConfig) Config(com.hazelcast.config.Config) IExecutorService(com.hazelcast.core.IExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) MultiExecutionCallback(com.hazelcast.core.MultiExecutionCallback) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IAtomicLong(com.hazelcast.core.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) Map(java.util.Map) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

IAtomicLong (com.hazelcast.core.IAtomicLong)49 ParallelTest (com.hazelcast.test.annotation.ParallelTest)45 Test (org.junit.Test)45 QuickTest (com.hazelcast.test.annotation.QuickTest)44 HazelcastInstance (com.hazelcast.core.HazelcastInstance)21 IExecutorService (com.hazelcast.core.IExecutorService)10 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)9 UndefinedErrorCodeException (com.hazelcast.client.UndefinedErrorCodeException)8 Random (java.util.Random)8 Config (com.hazelcast.config.Config)6 Member (com.hazelcast.core.Member)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutionException (java.util.concurrent.ExecutionException)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ExecutorConfig (com.hazelcast.config.ExecutorConfig)5 MultiExecutionCallback (com.hazelcast.core.MultiExecutionCallback)3 Map (java.util.Map)3 ICountDownLatch (com.hazelcast.core.ICountDownLatch)2 ClientCacheProxyFactory (com.hazelcast.client.cache.impl.ClientCacheProxyFactory)1 ProxyFactoryConfig (com.hazelcast.client.config.ProxyFactoryConfig)1