@Test public void testSubmitToKeyOwnerCallable() throws Exception { 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 / 2); final ExecutionCallback callback = new ExecutionCallback() { public void onResponse(Object response) { if ((Boolean) response) count.incrementAndGet(); latch.countDown(); } public void onFailure(Throwable t) {} }; for (int i = 0; i < k; i++) { final HazelcastInstance instance = instances[i]; final IExecutorService service = instance.getExecutorService("testSubmitToKeyOwnerCallable"); final String script = "hazelcast.getCluster().getLocalMember().equals(member)"; final HashMap map = new HashMap(); final Member localMember = instance.getCluster().getLocalMember(); map.put("member", localMember); int key = 0; while (!localMember.equals(instance.getPartitionService().getPartition(++key).getOwner())) ; if (i % 2 == 0) { final Future f = service.submitToKeyOwner(new ScriptCallable(script, map), key); assertTrue((Boolean) f.get(5, TimeUnit.SECONDS)); } else { service.submitToKeyOwner(new ScriptCallable(script, map), key, callback); } } assertTrue(latch.await(30, TimeUnit.SECONDS)); assertEquals(k / 2, count.get()); }
/** Execute a task that is executing something else inside. Nested Execution. */ @Test(timeout = 10000) public void testNestedExecution() throws Exception { Callable<String> task = new NestedExecutorTask(); ExecutorService executor = createSingleNodeExecutorService("testNestedExecution"); Future future = executor.submit(task); future.get(); }
/** Run a basic task */ @Test public void testBasicTask() throws Exception { Callable<String> task = new BasicTestTask(); ExecutorService executor = createSingleNodeExecutorService("testBasicTask"); Future future = executor.submit(task); assertEquals(future.get(), BasicTestTask.RESULT); }
@Test public void testGetAsync() throws Exception { HazelcastClient hClient = getHazelcastClient(); String key = "key"; String value1 = "value1"; IMap<String, String> map = hClient.getMap("map:test:getAsync"); map.put(key, value1); Future<String> f1 = map.getAsync(key); assertEquals(value1, f1.get()); }
/** Test the method isDone() */ @Test public void testIsDoneMethod() throws Exception { Callable<String> task = new BasicTestTask(); IExecutorService executor = createSingleNodeExecutorService("isDoneMethod"); Future future = executor.submit(task); if (future.isDone()) { assertTrue(future.isDone()); } assertEquals(future.get(), BasicTestTask.RESULT); assertTrue(future.isDone()); }
@Test public void testSubmitMultipleNode() throws ExecutionException, InterruptedException { 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("testSubmitMultipleNode"); final String script = "hazelcast.getAtomicLong('testSubmitMultipleNode').incrementAndGet();"; final Future future = service.submit(new ScriptCallable(script, null)); assertEquals((long) (i + 1), future.get()); } }
@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 multiple Future.get() invocation */ @Test public void testMultipleFutureGets() throws Exception { Callable<String> task = new BasicTestTask(); ExecutorService executor = createSingleNodeExecutorService("isTwoGetFromFuture"); Future<String> future = executor.submit(task); String s1 = future.get(); assertEquals(s1, BasicTestTask.RESULT); assertTrue(future.isDone()); String s2 = future.get(); assertEquals(s2, BasicTestTask.RESULT); assertTrue(future.isDone()); String s3 = future.get(); assertEquals(s3, BasicTestTask.RESULT); assertTrue(future.isDone()); String s4 = future.get(); assertEquals(s4, BasicTestTask.RESULT); assertTrue(future.isDone()); }
@Test public void testExecutorServiceStats() throws InterruptedException, ExecutionException { final IExecutorService executorService = createSingleNodeExecutorService("testExecutorServiceStats"); final int k = 10; final CountDownLatch latch = new CountDownLatch(k); final int executionTime = 200; for (int i = 0; i < k; i++) { executorService.execute( new Runnable() { public void run() { try { Thread.sleep(executionTime); } catch (InterruptedException e) { e.printStackTrace(); } latch.countDown(); } }); } latch.await(2, TimeUnit.MINUTES); final Future<Boolean> f = executorService.submit(new CancellationAwareTask(10000)); Thread.sleep(1000); f.cancel(true); try { f.get(); } catch (CancellationException e) { } final LocalExecutorStats stats = executorService.getLocalExecutorStats(); assertEquals(k + 1, stats.getStartedTaskCount()); assertEquals(k, stats.getCompletedTaskCount()); assertEquals(0, stats.getPendingTaskCount()); assertEquals(1, stats.getCancelledTaskCount()); }
/** Test for the issue 129. Repeatedly runs tasks and check for isDone() status after get(). */ @Test public void testIsDoneMethod2() throws Exception { ExecutorService executor = createSingleNodeExecutorService("isDoneMethod2"); for (int i = 0; i < COUNT; i++) { Callable<String> task1 = new BasicTestTask(); Callable<String> task2 = new BasicTestTask(); Future future1 = executor.submit(task1); Future future2 = executor.submit(task2); assertEquals(future2.get(), BasicTestTask.RESULT); assertTrue(future2.isDone()); assertEquals(future1.get(), BasicTestTask.RESULT); assertTrue(future1.isDone()); } }
@Test public void testCancellationAwareTask() throws ExecutionException, InterruptedException { CancellationAwareTask task = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask"); Future future = executor.submit(task); try { future.get(2, TimeUnit.SECONDS); fail("Should throw TimeoutException!"); } catch (TimeoutException expected) { } assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
@Test public void testCancellationAwareTask2() { Callable task1 = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1); executor.submit(task1); Callable task2 = new BasicTestTask(); Future future = executor.submit(task2); assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
public String call() throws Exception { Future future = instance.getExecutorService("NestedExecutorTask").submit(new BasicTestTask()); return (String) future.get(); }