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();
}
}
}
}
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));
}
}
});
}
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);
}
});
}
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));
}
}
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());
}
Aggregations