@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 index = random.nextInt(REFERENCE_COUNT);
     int increment = random.nextInt(100);
     increments[index] += increment;
     IAtomicLong reference = references[index];
     reference.addAndGet(increment);
   }
 }
 @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);
   }
 }
 @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());
 }
 @Test
 public void testDecrement() {
   when(atomicNumber.decrementAndGet()).thenReturn(9L);
   long body = template.requestBody("direct:decrement", null, Long.class);
   verify(atomicNumber).decrementAndGet();
   assertEquals(9, body);
 }
 @Test
 public void testGet() {
   when(atomicNumber.get()).thenReturn(1234L);
   long body = template.requestBody("direct:get", null, Long.class);
   verify(atomicNumber).get();
   assertEquals(1234, body);
 }
 @Test
 public void testHazelcastInstances() {
   assertNotNull(map1);
   assertNotNull(map2);
   assertNotNull(multiMap);
   assertNotNull(replicatedMap);
   assertNotNull(queue);
   assertNotNull(topic);
   assertNotNull(set);
   assertNotNull(list);
   assertNotNull(executorService);
   assertNotNull(idGenerator);
   assertNotNull(atomicLong);
   assertNotNull(atomicReference);
   assertNotNull(countDownLatch);
   assertNotNull(semaphore);
   assertNotNull(lock);
   assertEquals("map1", map1.getName());
   assertEquals("map2", map2.getName());
   assertEquals("testMultimap", multiMap.getName());
   assertEquals("replicatedMap", replicatedMap.getName());
   assertEquals("testQ", queue.getName());
   assertEquals("testTopic", topic.getName());
   assertEquals("set", set.getName());
   assertEquals("list", list.getName());
   assertEquals("idGenerator", idGenerator.getName());
   assertEquals("atomicLong", atomicLong.getName());
   assertEquals("atomicReference", atomicReference.getName());
   assertEquals("countDownLatch", countDownLatch.getName());
   assertEquals("semaphore", semaphore.getName());
 }
  @RequestMapping(
      consumes = "application/json",
      method = RequestMethod.POST,
      produces = "application/json")
  @ResponseBody
  public Long submitOrder(@RequestBody OrderRequest request) throws Exception {
    IAtomicLong atomicLong = hazelcastClient.getAtomicLong(ORDER_ID_SEQUENCE);
    Long id = atomicLong.getAndIncrement();

    // We could change this to get the sum total of all the books
    Collection<Book> books = booksService.getBooks(request.getIsbns());
    BigDecimal result =
        books.stream().map(book -> book.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);

    Order order =
        new Order(id, request.getCustomer().getId(), request.getIsbns(), new Date(), result);
    ordersService.addOrder(order);
    return id;
  }
  @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());
  }
  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;
      }
    }
  }
 @Verify
 public void verify() {
   long expected = workQueue.size() + consumed.get();
   long actual = produced.get();
   assertEquals(expected, actual);
 }
 @Teardown
 public void teardown() throws Exception {
   produced.destroy();
   workQueue.destroy();
   consumed.destroy();
 }