Search in sources :

Example 66 with TimeoutException

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

the class SuppliersTest method testSupplierThreadSafe.

@GwtIncompatible("Thread")
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)

Example 67 with TimeoutException

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

the class CloudInstanceMapper method waitOperation.

private void waitOperation(AsyncServerOperation operation) throws OpsException {
    try {
        log.info("Waiting for server operation to complete");
        operation.waitComplete(2, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout waiting for server operation to complete", e);
    } catch (OpenstackException e) {
        throw new OpsException("Error waiting for server operation to complete", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackException(org.openstack.client.OpenstackException) TimeoutException(java.util.concurrent.TimeoutException)

Example 68 with TimeoutException

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

the class GoogleComputeClient method deleteFirewallRule.

public void deleteFirewallRule(Firewall rule) throws OpsException {
    try {
        log.debug("Deleting firewall rule: " + rule);
        Operation operation = compute.firewalls().delete(projectId, rule.getName()).execute();
        waitComplete(operation, 5, TimeUnit.MINUTES);
    // TODO: Check success of operation?
    } catch (IOException e) {
        throw new OpsException("Error deleting firewall", e);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout while waiting for firewall deletion", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 69 with TimeoutException

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

the class GoogleComputeMachine method terminate.

@Override
public void terminate() throws OpsException {
    try {
        Operation operation = computeClient.terminateInstance(instance.getName());
        computeClient.waitComplete(operation, 5, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout waiting for instance termination", e);
    }
    refreshState();
}
Also used : OpsException(org.platformlayer.ops.OpsException) Operation(com.google.api.services.compute.model.Operation) TimeoutException(java.util.concurrent.TimeoutException)

Example 70 with TimeoutException

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

the class TimeoutPoll method poll.

public static <T> T poll(TimeSpan timeout, TimeSpan pollInterval, PollFunction<T> untilNotNull) throws ExecutionException, TimeoutException {
    long start = System.currentTimeMillis();
    T value = null;
    do {
        try {
            value = untilNotNull.call();
        } catch (Exception e) {
            throw new ExecutionException(e);
        }
        if (value != null) {
            return value;
        }
        if (!pollInterval.doSafeSleep()) {
            throw new TimeoutException("Interrupted during sleep");
        }
    } while (!timeout.hasTimedOut(start));
    throw new TimeoutException();
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)788 ExecutionException (java.util.concurrent.ExecutionException)249 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 CancellationException (java.util.concurrent.CancellationException)44 Test (org.testng.annotations.Test)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