@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()); }
@Test public void testSubmitToMemberRunnable() 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 ExecutionCallback callback = new ExecutionCallback() { public void onResponse(Object response) { if (response == null) { 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("testSubmitToMemberRunnable"); final String script = "if(!hazelcast.getCluster().getLocalMember().equals(member)) " + "hazelcast.getAtomicLong('testSubmitToMemberRunnable').incrementAndGet();"; final HashMap map = new HashMap(); map.put("member", instance.getCluster().getLocalMember()); service.submitToMember( new ScriptRunnable(script, map), instance.getCluster().getLocalMember(), callback); } latch.await(10, TimeUnit.SECONDS); assertEquals(0, instances[0].getAtomicLong("testSubmitToMemberRunnable").get()); assertEquals(k, count.get()); }
@Test public void hazelcastInstanceAwareAndLocal() throws Exception { final Config config = new Config(); config.addExecutorConfig(new ExecutorConfig("test", 1)); final HazelcastInstance instance = createHazelcastInstance(config); IExecutorService executor = instance.getExecutorService("test"); HazelcastInstanceAwareRunnable task = new HazelcastInstanceAwareRunnable(); executor.submit(task).get(); assertTrue("The setHazelcastInstance should have been called", task.initializeCalled); }
@Test public void testExecuteOnAllMembers() { final IExecutorService service = client.getExecutorService(randomString()); final String mapName = randomString(); service.executeOnAllMembers(new MapPutRunnable(mapName)); final IMap map = client.getMap(mapName); assertTrueEventually( new AssertTask() { public void run() throws Exception { assertEquals(CLUSTER_SIZE, map.size()); } }); }
@Test public void testExecuteOnMembers_withEmptyCollection() { final IExecutorService service = client.getExecutorService(randomString()); final String mapName = randomString(); final Collection collection = new ArrayList(); service.executeOnMembers(new MapPutRunnable(mapName), collection); final IMap map = client.getMap(mapName); assertTrueEventually( new AssertTask() { public void run() throws Exception { assertTrue(map.isEmpty()); } }); }
@Test public void testExecuteOnMember() { final IExecutorService service = client.getExecutorService(randomString()); final String mapName = randomString(); final Member member = instance1.getCluster().getLocalMember(); final String targetUuid = member.getUuid(); service.executeOnMember(new MapPutRunnable(mapName), member); final IMap map = client.getMap(mapName); assertTrueEventually( new AssertTask() { public void run() throws Exception { assertTrue(map.containsKey(targetUuid)); } }); }
@Test public void testExecuteOnMembers() { final IExecutorService service = client.getExecutorService(randomString()); final String mapName = randomString(); final Collection collection = new ArrayList(); final Member member1 = instance1.getCluster().getLocalMember(); final Member member3 = instance3.getCluster().getLocalMember(); collection.add(member1); collection.add(member3); service.executeOnMembers(new MapPutRunnable(mapName), collection); final IMap map = client.getMap(mapName); assertTrueEventually( new AssertTask() { public void run() throws Exception { assertTrue(map.containsKey(member1.getUuid())); assertTrue(map.containsKey(member3.getUuid())); } }); }
@Test public void testManagedContextAndLocal() throws Exception { final Config config = new Config(); config.addExecutorConfig(new ExecutorConfig("test", 1)); config.setManagedContext( new ManagedContext() { @Override public Object initialize(Object obj) { if (obj instanceof RunnableWithManagedContext) { RunnableWithManagedContext task = (RunnableWithManagedContext) obj; task.initializeCalled = true; } return obj; } }); final HazelcastInstance instance = createHazelcastInstance(config); IExecutorService executor = instance.getExecutorService("test"); RunnableWithManagedContext task = new RunnableWithManagedContext(); executor.submit(task).get(); assertTrue( "The task should have been initialized by the ManagedContext", task.initializeCalled); }
private IExecutorService createSingleNodeExecutorService(String name, int poolSize) { final Config config = new Config(); config.addExecutorConfig(new ExecutorConfig(name, poolSize)); final HazelcastInstance instance = createHazelcastInstance(config); return instance.getExecutorService(name); }
public String call() throws Exception { Future future = instance.getExecutorService("NestedExecutorTask").submit(new BasicTestTask()); return (String) future.get(); }
@Test(expected = NullPointerException.class) public void testExecute_whenTaskNull() { IExecutorService service = client.getExecutorService(randomString()); service.execute(null); }
@Test(expected = IllegalArgumentException.class) public void testExecuteOnMembers_whenSelectorNull() { IExecutorService service = client.getExecutorService(randomString()); MemberSelector selector = null; service.executeOnMembers(new MapPutRunnable("task"), selector); }
@Test(expected = NullPointerException.class) public void testExecuteOnMembers_WhenCollectionNull() { IExecutorService service = client.getExecutorService(randomString()); Collection collection = null; service.executeOnMembers(new MapPutRunnable("task"), collection); }
@Test(expected = NullPointerException.class) public void testExecuteOnMember_WhenMemberNull() { IExecutorService service = client.getExecutorService(randomString()); service.executeOnMember(new MapPutRunnable("map"), null); }