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