Search in sources :

Example 66 with GwtIncompatible

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

the class FuturesTest method testWhenAllCompleteRunnable_resultCanceledWithInterrupt_InterruptsRunnable.

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

        @Override
        public void run() {
            inFunction.countDown();
            try {
                // wait for interrupt
                new CountDownLatch(1).await();
            } catch (InterruptedException expected) {
                // Ensure the thread's interrupt status is preserved.
                Thread.currentThread().interrupt();
                gotException.countDown();
            }
        }
    };
    ListenableFuture<?> futureResult = whenAllComplete(stringFuture, booleanFuture).run(combiner, newSingleThreadExecutor());
    stringFuture.set("value");
    booleanFuture.set(true);
    inFunction.await();
    futureResult.cancel(true);
    try {
        futureResult.get();
        fail();
    } catch (CancellationException expected) {
    }
    gotException.await();
}
Also used : CancellationException(java.util.concurrent.CancellationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 67 with GwtIncompatible

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

the class FuturesTest method testCatchingAsync_functionToString.

// Threads
@GwtIncompatible
public void testCatchingAsync_functionToString() throws Exception {
    final CountDownLatch functionCalled = new CountDownLatch(1);
    final CountDownLatch functionBlocking = new CountDownLatch(1);
    AsyncFunction<Object, Object> function = tagged("Called my toString", new AsyncFunction<Object, Object>() {

        @Override
        public ListenableFuture<Object> apply(Object input) throws Exception {
            functionCalled.countDown();
            functionBlocking.await();
            return immediateFuture(null);
        }
    });
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        ListenableFuture<?> output = Futures.catchingAsync(immediateFailedFuture(new RuntimeException()), Throwable.class, function, executor);
        functionCalled.await();
        assertThat(output.toString()).contains(function.toString());
    } finally {
        functionBlocking.countDown();
        executor.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) 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 68 with GwtIncompatible

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

the class Futures method lazyTransform.

/**
 * Like {@link #transform(ListenableFuture, Function, Executor)} except that the transformation
 * {@code function} is invoked on each call to {@link Future#get() get()} on the returned future.
 *
 * <p>The returned {@code Future} reflects the input's cancellation state directly, and any
 * attempt to cancel the returned Future is likewise passed through to the input Future.
 *
 * <p>Note that calls to {@linkplain Future#get(long, TimeUnit) timed get} only apply the timeout
 * to the execution of the underlying {@code Future}, <em>not</em> to the execution of the
 * transformation function.
 *
 * <p>The primary audience of this method is callers of {@code transform} who don't have a {@code
 * ListenableFuture} available and do not mind repeated, lazy function evaluation.
 *
 * @param input The future to transform
 * @param function A Function to transform the results of the provided future to the results of
 *     the returned future.
 * @return A future that returns the result of the transformation.
 * @since 10.0
 */
@Beta
// TODO
@GwtIncompatible
public static <I extends @Nullable Object, O extends @Nullable Object> Future<O> lazyTransform(final Future<I> input, final Function<? super I, ? extends O> function) {
    checkNotNull(input);
    checkNotNull(function);
    return new Future<O>() {

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return input.cancel(mayInterruptIfRunning);
        }

        @Override
        public boolean isCancelled() {
            return input.isCancelled();
        }

        @Override
        public boolean isDone() {
            return input.isDone();
        }

        @Override
        public O get() throws InterruptedException, ExecutionException {
            return applyTransformation(input.get());
        }

        @Override
        public O get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return applyTransformation(input.get(timeout, unit));
        }

        private O applyTransformation(I input) throws ExecutionException {
            try {
                return function.apply(input);
            } catch (Throwable t) {
                throw new ExecutionException(t);
            }
        }
    };
}
Also used : ImmediateFailedFuture(com.google.common.util.concurrent.ImmediateFuture.ImmediateFailedFuture) ImmediateCancelledFuture(com.google.common.util.concurrent.ImmediateFuture.ImmediateCancelledFuture) Future(java.util.concurrent.Future) ListFuture(com.google.common.util.concurrent.CollectionFuture.ListFuture) TimeUnit(java.util.concurrent.TimeUnit) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) GwtIncompatible(com.google.common.annotations.GwtIncompatible) Beta(com.google.common.annotations.Beta)

Example 69 with GwtIncompatible

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

the class BigIntegerMathTest method testRoundToDouble_negativeWayTooBig.

@GwtIncompatible
public void testRoundToDouble_negativeWayTooBig() {
    BigInteger bi = BigInteger.ONE.shiftLeft(2 * Double.MAX_EXPONENT).negate();
    new RoundToDoubleTester(bi).setExpectation(-Double.MAX_VALUE, DOWN, CEILING, HALF_EVEN, HALF_UP, HALF_DOWN).setExpectation(Double.NEGATIVE_INFINITY, UP, FLOOR).roundUnnecessaryShouldThrow().test();
}
Also used : BigInteger(java.math.BigInteger) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 70 with GwtIncompatible

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

the class BigIntegerMathTest method testNullPointers.

// NullPointerTester
@GwtIncompatible
public void testNullPointers() {
    NullPointerTester tester = new NullPointerTester();
    tester.setDefault(BigInteger.class, ONE);
    tester.setDefault(int.class, 1);
    tester.setDefault(long.class, 1L);
    tester.testAllPublicStaticMethods(BigIntegerMath.class);
}
Also used : NullPointerTester(com.google.common.testing.NullPointerTester) 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