Search in sources :

Example 61 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class FuturesTest method testCatchingAsync_customTypeMatch.

// non-Throwable exceptionType
@GwtIncompatible
public void testCatchingAsync_customTypeMatch() throws Exception {
    AsyncFunction<IOException, Integer> fallback = asyncFunctionReturningOne();
    ListenableFuture<Integer> originalFuture = immediateFailedFuture(new FileNotFoundException());
    ListenableFuture<Integer> faultTolerantFuture = catchingAsync(originalFuture, IOException.class, fallback, directExecutor());
    assertEquals(1, (int) getDone(faultTolerantFuture));
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 62 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class FuturesTest method testScheduleAsync_asyncCallable_cancelledBeforeCallingFunction.

// threads
@GwtIncompatible
public void testScheduleAsync_asyncCallable_cancelledBeforeCallingFunction() throws InterruptedException {
    final AtomicBoolean callableCalled = new AtomicBoolean();
    AsyncCallable<Integer> callable = new AsyncCallable<Integer>() {

        @Override
        public ListenableFuture<Integer> call() {
            callableCalled.set(true);
            return immediateFuture(1);
        }
    };
    ScheduledExecutorService executor = newSingleThreadScheduledExecutor();
    // Pause the executor.
    final CountDownLatch beforeFunction = new CountDownLatch(1);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            awaitUninterruptibly(beforeFunction);
        }
    });
    ListenableFuture<Integer> future = scheduleAsync(callable, 1, NANOSECONDS, executor);
    future.cancel(false);
    // Unpause the executor.
    beforeFunction.countDown();
    executor.shutdown();
    assertTrue(executor.awaitTermination(5, SECONDS));
    assertFalse(callableCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 63 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class FuturesTest method testCatchingAsync_interruptPropagatesToTransformingThread.

// threads
@GwtIncompatible
public void testCatchingAsync_interruptPropagatesToTransformingThread() throws Exception {
    SettableFuture<String> input = SettableFuture.create();
    final CountDownLatch inFunction = new CountDownLatch(1);
    final CountDownLatch shouldCompleteFunction = new CountDownLatch(1);
    final CountDownLatch gotException = new CountDownLatch(1);
    AsyncFunction<Throwable, String> function = new AsyncFunction<Throwable, String>() {

        @Override
        public ListenableFuture<String> apply(Throwable t) throws Exception {
            inFunction.countDown();
            try {
                shouldCompleteFunction.await();
            } catch (InterruptedException expected) {
                gotException.countDown();
                throw expected;
            }
            return immediateFuture("a");
        }
    };
    ListenableFuture<String> futureResult = catchingAsync(input, Exception.class, function, newSingleThreadExecutor());
    input.setException(new Exception());
    inFunction.await();
    futureResult.cancel(true);
    shouldCompleteFunction.countDown();
    try {
        futureResult.get();
        fail();
    } catch (CancellationException expected) {
    }
    // TODO(cpovirk): implement interruption, updating this test:
    // https://github.com/google/guava/issues/1989
    assertEquals(1, gotException.getCount());
// gotException.await();
}
Also used : CancellationException(java.util.concurrent.CancellationException) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) FileNotFoundException(java.io.FileNotFoundException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 64 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class FuturesTest method testTransformAsync_asyncFunction_cancelledBeforeApplyingFunction.

// threads
@GwtIncompatible
public void testTransformAsync_asyncFunction_cancelledBeforeApplyingFunction() throws InterruptedException {
    final AtomicBoolean functionCalled = new AtomicBoolean();
    AsyncFunction<String, Integer> function = new AsyncFunction<String, Integer>() {

        @Override
        public ListenableFuture<Integer> apply(String input) throws Exception {
            functionCalled.set(true);
            return immediateFuture(1);
        }
    };
    SettableFuture<String> inputFuture = SettableFuture.create();
    ExecutorService executor = newSingleThreadExecutor();
    ListenableFuture<Integer> future = transformAsync(inputFuture, function, executor);
    // Pause the executor.
    final CountDownLatch beforeFunction = new CountDownLatch(1);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            awaitUninterruptibly(beforeFunction);
        }
    });
    // Cancel the future after making input available.
    inputFuture.set("value");
    future.cancel(false);
    // Unpause the executor.
    beforeFunction.countDown();
    executor.shutdown();
    assertTrue(executor.awaitTermination(5, SECONDS));
    assertFalse(functionCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 65 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class FuturesTest method testWhenAllComplete_cancelledNotInterrupted.

// threads
@GwtIncompatible
public void testWhenAllComplete_cancelledNotInterrupted() throws Exception {
    SettableFuture<String> stringFuture = SettableFuture.create();
    SettableFuture<Boolean> booleanFuture = SettableFuture.create();
    final CountDownLatch inFunction = new CountDownLatch(1);
    final CountDownLatch shouldCompleteFunction = new CountDownLatch(1);
    final SettableFuture<String> resultFuture = SettableFuture.create();
    AsyncCallable<String> combiner = new AsyncCallable<String>() {

        @Override
        public ListenableFuture<String> call() throws Exception {
            inFunction.countDown();
            shouldCompleteFunction.await();
            return resultFuture;
        }
    };
    ListenableFuture<String> futureResult = whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor());
    stringFuture.set("value");
    booleanFuture.set(true);
    inFunction.await();
    futureResult.cancel(false);
    shouldCompleteFunction.countDown();
    try {
        futureResult.get();
        fail();
    } catch (CancellationException expected) {
    }
    try {
        resultFuture.get();
        fail();
    } catch (CancellationException expected) {
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Aggregations

GwtIncompatible (com.google.common.annotations.GwtIncompatible)361 NullPointerTester (com.google.common.testing.NullPointerTester)105 TestSuite (junit.framework.TestSuite)54 BigInteger (java.math.BigInteger)40 RoundingMode (java.math.RoundingMode)39 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutorService (java.util.concurrent.ExecutorService)18 CancellationException (java.util.concurrent.CancellationException)17 Random (java.util.Random)16 ListTestSuiteBuilder (com.google.common.collect.testing.ListTestSuiteBuilder)15 ExecutionException (java.util.concurrent.ExecutionException)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 BigDecimal (java.math.BigDecimal)14 TestStringSetGenerator (com.google.common.collect.testing.TestStringSetGenerator)11 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)11 TimeoutException (java.util.concurrent.TimeoutException)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 EqualsTester (com.google.common.testing.EqualsTester)10 List (java.util.List)10