Search in sources :

Example 6 with DurableExecutorService

use of com.hazelcast.durableexecutor.DurableExecutorService in project hazelcast by hazelcast.

the class ClientDurableExecutorServiceSubmitTest method testSubmitCallable_withExecutionCallback.

@Test
public void testSubmitCallable_withExecutionCallback() {
    DurableExecutorService service = client.getDurableExecutorService(randomString());
    String msg = randomString();
    Callable<String> callable = new AppendCallable(msg);
    final AtomicReference<String> result = new AtomicReference<String>();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    service.submit(callable).andThen(new ExecutionCallback<String>() {

        public void onResponse(String response) {
            result.set(response);
            responseLatch.countDown();
        }

        public void onFailure(Throwable t) {
        }
    });
    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(msg + AppendCallable.APPENDAGE, result.get());
}
Also used : DurableExecutorService(com.hazelcast.durableexecutor.DurableExecutorService) AppendCallable(com.hazelcast.client.executor.tasks.AppendCallable) AtomicReference(java.util.concurrent.atomic.AtomicReference) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with DurableExecutorService

use of com.hazelcast.durableexecutor.DurableExecutorService in project hazelcast by hazelcast.

the class ClientDurableExecutorServiceSubmitTest method submitRunnableToKeyOwner.

@Test
public void submitRunnableToKeyOwner() {
    DurableExecutorService service = client.getDurableExecutorService(randomString());
    String mapName = randomString();
    Runnable runnable = new MapPutRunnable(mapName);
    final CountDownLatch responseLatch = new CountDownLatch(1);
    service.submitToKeyOwner(runnable, "key").andThen(new ExecutionCallback() {

        public void onResponse(Object response) {
            responseLatch.countDown();
        }

        public void onFailure(Throwable t) {
        }
    });
    IMap map = client.getMap(mapName);
    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(1, map.size());
}
Also used : DurableExecutorService(com.hazelcast.durableexecutor.DurableExecutorService) IMap(com.hazelcast.core.IMap) MapPutPartitionAwareRunnable(com.hazelcast.client.executor.tasks.MapPutPartitionAwareRunnable) MapPutRunnable(com.hazelcast.client.executor.tasks.MapPutRunnable) MapPutRunnable(com.hazelcast.client.executor.tasks.MapPutRunnable) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with DurableExecutorService

use of com.hazelcast.durableexecutor.DurableExecutorService in project hazelcast by hazelcast.

the class ClientDurableExecutorServiceSubmitTest method submitRunnablePartitionAware_withExecutionCallback.

@Test
public void submitRunnablePartitionAware_withExecutionCallback() {
    DurableExecutorService service = client.getDurableExecutorService(randomString());
    String mapName = randomString();
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();
    Runnable runnable = new MapPutPartitionAwareRunnable<String>(mapName, key);
    final CountDownLatch responseLatch = new CountDownLatch(1);
    service.submit(runnable).andThen(new ExecutionCallback() {

        @Override
        public void onResponse(Object response) {
            responseLatch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
        }
    });
    IMap map = client.getMap(mapName);
    assertOpenEventually("responseLatch", responseLatch);
    assertTrue(map.containsKey(member.getUuid()));
}
Also used : DurableExecutorService(com.hazelcast.durableexecutor.DurableExecutorService) IMap(com.hazelcast.core.IMap) MapPutPartitionAwareRunnable(com.hazelcast.client.executor.tasks.MapPutPartitionAwareRunnable) MapPutRunnable(com.hazelcast.client.executor.tasks.MapPutRunnable) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) MapPutPartitionAwareRunnable(com.hazelcast.client.executor.tasks.MapPutPartitionAwareRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.hazelcast.core.Member) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with DurableExecutorService

use of com.hazelcast.durableexecutor.DurableExecutorService in project hazelcast by hazelcast.

the class ClientDurableExecutorServiceTest method testSubmitFailingCallableException_withExecutionCallback.

@Test
public void testSubmitFailingCallableException_withExecutionCallback() throws Exception {
    DurableExecutorService service = client.getDurableExecutorService(randomString());
    final CountDownLatch latch = new CountDownLatch(1);
    service.submit(new FailingCallable()).andThen(new ExecutionCallback<String>() {

        @Override
        public void onResponse(String response) {
        }

        @Override
        public void onFailure(Throwable t) {
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : DurableExecutorService(com.hazelcast.durableexecutor.DurableExecutorService) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) FailingCallable(com.hazelcast.client.executor.tasks.FailingCallable) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with DurableExecutorService

use of com.hazelcast.durableexecutor.DurableExecutorService in project hazelcast by hazelcast.

the class ClientDurableExecutorServiceTest method test_whenRingBufferIsFull_thenClientDurableExecutorServiceCompletedFutureIsReturned.

@Test
public void test_whenRingBufferIsFull_thenClientDurableExecutorServiceCompletedFutureIsReturned() throws Exception {
    final AtomicBoolean onResponse = new AtomicBoolean();
    final CountDownLatch onFailureLatch = new CountDownLatch(1);
    String key = randomString();
    DurableExecutorService service = client.getDurableExecutorService(SINGLE_TASK + randomString());
    service.submitToKeyOwner(new SleepingTask(100), key);
    DurableExecutorServiceFuture<String> future = service.submitToKeyOwner(new BasicTestCallable(), key);
    future.andThen(new ExecutionCallback<String>() {

        @Override
        public void onResponse(String response) {
            onResponse.set(true);
        }

        @Override
        public void onFailure(Throwable t) {
            onFailureLatch.countDown();
        }
    });
    try {
        future.get(1, TimeUnit.HOURS);
        fail("We expected that future.get() throws an ExecutionException!");
    } catch (ExecutionException ignored) {
    }
    // assert TaskId
    try {
        future.getTaskId();
        fail("We expected that future.getTaskId() throws an IllegalStateException!");
    } catch (IllegalStateException ignored) {
    }
    // assert states of ClientDurableExecutorServiceCompletedFuture
    assertFalse(future.cancel(false));
    assertFalse(future.cancel(true));
    assertFalse(future.isCancelled());
    assertTrue(future.isDone());
    // assert that onFailure() has been called and not onResponse()
    onFailureLatch.await();
    assertFalse(onResponse.get());
}
Also used : DurableExecutorService(com.hazelcast.durableexecutor.DurableExecutorService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SleepingTask(com.hazelcast.executor.ExecutorServiceTestSupport.SleepingTask) BasicTestCallable(com.hazelcast.executor.ExecutorServiceTestSupport.BasicTestCallable) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

DurableExecutorService (com.hazelcast.durableexecutor.DurableExecutorService)41 ParallelTest (com.hazelcast.test.annotation.ParallelTest)39 QuickTest (com.hazelcast.test.annotation.QuickTest)39 Test (org.junit.Test)39 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)28 IMap (com.hazelcast.core.IMap)11 MapPutRunnable (com.hazelcast.client.executor.tasks.MapPutRunnable)10 BasicTestCallable (com.hazelcast.executor.ExecutorServiceTestSupport.BasicTestCallable)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 SleepingTask (com.hazelcast.executor.ExecutorServiceTestSupport.SleepingTask)7 MapPutPartitionAwareRunnable (com.hazelcast.client.executor.tasks.MapPutPartitionAwareRunnable)6 Member (com.hazelcast.core.Member)6 AppendCallable (com.hazelcast.client.executor.tasks.AppendCallable)5 AssertTask (com.hazelcast.test.AssertTask)5 Future (java.util.concurrent.Future)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 MapPutPartitionAwareCallable (com.hazelcast.client.executor.tasks.MapPutPartitionAwareCallable)4 ExecutionException (java.util.concurrent.ExecutionException)4 FailingCallable (com.hazelcast.client.executor.tasks.FailingCallable)3 ExecutionCallback (com.hazelcast.core.ExecutionCallback)3