Ejemplo n.º 1
0
  public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IAtomicLong atomicLong = hz.getAtomicLong("counter");

    atomicLong.set(1);
    long result = atomicLong.apply(new Add2Function());
    System.out.println("apply.result:" + result);
    System.out.println("apply.value:" + atomicLong.get());

    atomicLong.set(1);
    atomicLong.alter(new Add2Function());
    System.out.println("alter.value:" + atomicLong.get());

    atomicLong.set(1);
    result = atomicLong.alterAndGet(new Add2Function());
    System.out.println("alterAndGet.result:" + result);
    System.out.println("alterAndGet.value:" + atomicLong.get());

    atomicLong.set(1);
    result = atomicLong.getAndAlter(new Add2Function());
    System.out.println("getAndAlter.result:" + result);
    System.out.println("getAndAlter.value:" + atomicLong.get());

    System.exit(0);

    for (; ; ) {
      long oldValue = atomicLong.get();
      long newValue = oldValue + 2;
      if (atomicLong.compareAndSet(oldValue, newValue)) {
        break;
      }
    }
  }
 @Test
 public void testGet() {
   when(atomicNumber.get()).thenReturn(1234L);
   long body = template.requestBody("direct:get", null, Long.class);
   verify(atomicNumber).get();
   assertEquals(1234, body);
 }
Ejemplo n.º 3
0
  @Test
  public void testSubmitToAllMembersCallable() throws InterruptedException {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(k * k);
    final MultiExecutionCallback callback =
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            count.incrementAndGet();
            countDownLatch.countDown();
          }

          public void onComplete(Map<Member, Object> values) {}
        };
    for (int i = 0; i < k; i++) {
      final IExecutorService service =
          instances[i].getExecutorService("testSubmitToAllMembersCallable");
      final String script =
          "hazelcast.getAtomicLong('testSubmitToAllMembersCallable').incrementAndGet();";
      service.submitToAllMembers(new ScriptCallable(script, null), callback);
    }
    countDownLatch.await(30, TimeUnit.SECONDS);
    final IAtomicLong result = instances[0].getAtomicLong("testSubmitToAllMembersCallable");
    assertEquals(k * k, result.get());
    assertEquals(k * k, count.get());
  }
 @Override
 public void doRun() throws Exception {
   while (!isStopped()) {
     int key = random.nextInt(REFERENCE_COUNT);
     IAtomicLong reference = references[key];
     long value = reference.get();
     assertEquals(
         format("The value for atomic reference: %s was not consistent", reference), key, value);
   }
 }
Ejemplo n.º 5
0
 @Test
 public void testExecuteMultipleNode() throws InterruptedException, ExecutionException {
   final int k = simpleTestNodeCount;
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
   final HazelcastInstance[] instances = factory.newInstances(new Config());
   for (int i = 0; i < k; i++) {
     final IExecutorService service = instances[i].getExecutorService("testExecuteMultipleNode");
     final String script = "hazelcast.getAtomicLong('count').incrementAndGet();";
     final int rand = new Random().nextInt(100);
     final Future<Integer> future = service.submit(new ScriptRunnable(script, null), rand);
     assertEquals(Integer.valueOf(rand), future.get());
   }
   final IAtomicLong count = instances[0].getAtomicLong("count");
   assertEquals(k, count.get());
 }
Ejemplo n.º 6
0
  @Test
  public void testSubmitToMembersRunnable() throws InterruptedException {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k);
    final 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;
    final Set<Member> membersSet = instances[0].getCluster().getMembers();
    final Member[] members = membersSet.toArray(new Member[membersSet.size()]);
    final Random random = new Random();
    for (int i = 0; i < k; i++) {
      final IExecutorService service =
          instances[i].getExecutorService("testSubmitToMembersRunnable");
      final String script =
          "hazelcast.getAtomicLong('testSubmitToMembersRunnable').incrementAndGet();";
      final int n = random.nextInt(k) + 1;
      sum += n;
      Member[] m = new Member[n];
      System.arraycopy(members, 0, m, 0, n);
      service.submitToMembers(new ScriptRunnable(script, null), Arrays.asList(m), callback);
    }

    assertTrue(latch.await(30, TimeUnit.SECONDS));
    final IAtomicLong result = instances[0].getAtomicLong("testSubmitToMembersRunnable");
    assertEquals(sum, result.get());
    assertEquals(sum, count.get());
  }
 @Verify
 public void verify() {
   long expected = workQueue.size() + consumed.get();
   long actual = produced.get();
   assertEquals(expected, actual);
 }