Search in sources :

Example 31 with TimeoutException

use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.

the class ClusterMergeTask method waitOnFutureInterruptible.

private <V> V waitOnFutureInterruptible(Future<V> future, long timeout, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException {
    isNotNull(timeUnit, "timeUnit");
    long totalTimeoutMs = timeUnit.toMillis(timeout);
    while (true) {
        long timeoutStepMs = Math.min(MIN_WAIT_ON_FUTURE_TIMEOUT_MILLIS, totalTimeoutMs);
        try {
            return future.get(timeoutStepMs, TimeUnit.MILLISECONDS);
        } catch (TimeoutException t) {
            totalTimeoutMs -= timeoutStepMs;
            if (totalTimeoutMs <= 0) {
                throw t;
            }
            if (!node.isRunning()) {
                future.cancel(true);
                throw new HazelcastInstanceNotActiveException();
            }
        }
    }
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TimeoutException(java.util.concurrent.TimeoutException)

Example 32 with TimeoutException

use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.

the class ReplicatedMapTest method testPutAll.

private void testPutAll(Config config) throws TimeoutException {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");
    final int partitionCount = getPartitionService(instance1).getPartitionCount();
    final Set<String> keys = generateRandomKeys(instance1, partitionCount);
    final Map<String, String> mapTest = new HashMap<String, String>();
    for (String key : keys) {
        mapTest.put(key, "bar");
    }
    map1.putAll(mapTest);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            for (String key : keys) {
                assertEquals("bar", map1.get(key));
                assertEquals("bar", map2.get(key));
            }
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HashMap(java.util.HashMap) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TimeoutException(java.util.concurrent.TimeoutException)

Example 33 with TimeoutException

use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.

the class Invocation_TimeoutTest method async_whenLongRunningOperation.

@Test
public void async_whenLongRunningOperation() throws InterruptedException, ExecutionException, TimeoutException {
    long callTimeout = 5000;
    Config config = new Config().setProperty(GroupProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), "" + callTimeout);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance(config);
    HazelcastInstance remote = factory.newHazelcastInstance(config);
    warmUpPartitions(local, remote);
    OperationService opService = getOperationService(local);
    ICompletableFuture<Object> future = opService.invokeOnPartition(null, new SlowOperation(6 * callTimeout, RESPONSE), getPartitionId(remote));
    final ExecutionCallback<Object> callback = getExecutionCallbackMock();
    future.andThen(callback);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            verify(callback).onResponse(RESPONSE);
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) AssertTask(com.hazelcast.test.AssertTask) OperationService(com.hazelcast.spi.OperationService) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 34 with TimeoutException

use of java.util.concurrent.TimeoutException in project guava by hceylan.

the class FuturesTest method pseudoTimedGet.

/**
   * Very rough equivalent of a timed get, produced by calling the no-arg get
   * method in another thread and waiting a short time for it.
   *
   * <p>We need this to test the behavior of no-arg get methods without hanging
   * the main test thread forever in the case of failure.
   */
private static <V> V pseudoTimedGet(final Future<V> input, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    ExecutorService executor = newSingleThreadExecutor();
    Future<V> waiter = executor.submit(new Callable<V>() {

        @Override
        public V call() throws Exception {
            return input.get();
        }
    });
    try {
        return waiter.get(timeout, unit);
    } catch (ExecutionException e) {
        propagateIfInstanceOf(e.getCause(), ExecutionException.class);
        propagateIfInstanceOf(e.getCause(), CancellationException.class);
        AssertionFailedError error = new AssertionFailedError("Unexpected exception");
        error.initCause(e);
        throw error;
    } finally {
        executor.shutdownNow();
        assertTrue(executor.awaitTermination(10, SECONDS));
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) AssertionFailedError(junit.framework.AssertionFailedError) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 35 with TimeoutException

use of java.util.concurrent.TimeoutException in project guava by google.

the class SuppliersTest method testSupplierThreadSafe.

// Thread
@GwtIncompatible
public void testSupplierThreadSafe(Function<Supplier<Boolean>, Supplier<Boolean>> memoizer) throws Throwable {
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicReference<Throwable> thrown = new AtomicReference<Throwable>(null);
    final int numThreads = 3;
    final Thread[] threads = new Thread[numThreads];
    final long timeout = TimeUnit.SECONDS.toNanos(60);
    final Supplier<Boolean> supplier = new Supplier<Boolean>() {

        boolean isWaiting(Thread thread) {
            switch(thread.getState()) {
                case BLOCKED:
                case WAITING:
                case TIMED_WAITING:
                    return true;
                default:
                    return false;
            }
        }

        int waitingThreads() {
            int waitingThreads = 0;
            for (Thread thread : threads) {
                if (isWaiting(thread)) {
                    waitingThreads++;
                }
            }
            return waitingThreads;
        }

        @Override
        public Boolean get() {
            // Check that this method is called exactly once, by the first
            // thread to synchronize.
            long t0 = System.nanoTime();
            while (waitingThreads() != numThreads - 1) {
                if (System.nanoTime() - t0 > timeout) {
                    thrown.set(new TimeoutException("timed out waiting for other threads to block" + " synchronizing on supplier"));
                    break;
                }
                Thread.yield();
            }
            count.getAndIncrement();
            return Boolean.TRUE;
        }
    };
    final Supplier<Boolean> memoizedSupplier = memoizer.apply(supplier);
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                assertSame(Boolean.TRUE, memoizedSupplier.get());
            }
        };
    }
    for (Thread t : threads) {
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    if (thrown.get() != null) {
        throw thrown.get();
    }
    assertEquals(1, count.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeoutException(java.util.concurrent.TimeoutException) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)793 ExecutionException (java.util.concurrent.ExecutionException)252 IOException (java.io.IOException)184 Test (org.junit.Test)149 ArrayList (java.util.ArrayList)75 CountDownLatch (java.util.concurrent.CountDownLatch)73 ExecutorService (java.util.concurrent.ExecutorService)71 Future (java.util.concurrent.Future)54 Test (org.testng.annotations.Test)46 CancellationException (java.util.concurrent.CancellationException)44 List (java.util.List)39 HashMap (java.util.HashMap)38 Map (java.util.Map)38 File (java.io.File)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)36 TimeUnit (java.util.concurrent.TimeUnit)34 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 URI (java.net.URI)21 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)21