1 /*
2  * Copyright (C) 2015 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.util.concurrent;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.base.Preconditions.checkState;
21 import static com.google.common.util.concurrent.FuturesTest.failureWithCause;
22 import static com.google.common.util.concurrent.FuturesTest.pseudoTimedGetUninterruptibly;
23 import static com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly;
24 import static java.util.concurrent.TimeUnit.MILLISECONDS;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static junit.framework.Assert.assertFalse;
27 import static junit.framework.Assert.fail;
28 
29 import com.google.common.annotations.GwtCompatible;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.Future;
32 import java.util.concurrent.TimeoutException;
33 import junit.framework.AssertionFailedError;
34 
35 /** Methods factored out so that they can be emulated differently in GWT. */
36 @GwtCompatible(emulated = true)
37 final class TestPlatform {
verifyGetOnPendingFuture(Future<?> future)38   static void verifyGetOnPendingFuture(Future<?> future) {
39     checkNotNull(future);
40     try {
41       pseudoTimedGetUninterruptibly(future, 10, MILLISECONDS);
42       fail();
43     } catch (TimeoutException expected) {
44     } catch (ExecutionException e) {
45       throw failureWithCause(e, "");
46     }
47   }
48 
verifyTimedGetOnPendingFuture(Future<?> future)49   static void verifyTimedGetOnPendingFuture(Future<?> future) {
50     try {
51       getUninterruptibly(future, 0, SECONDS);
52       fail();
53     } catch (TimeoutException expected) {
54     } catch (ExecutionException e) {
55       throw failureWithCause(e, "");
56     }
57   }
58 
verifyThreadWasNotInterrupted()59   static void verifyThreadWasNotInterrupted() {
60     assertFalse(Thread.currentThread().isInterrupted());
61   }
62 
clearInterrupt()63   static void clearInterrupt() {
64     Thread.interrupted();
65   }
66 
67   /**
68    * Retrieves the result of a {@code Future} known to be done but uses the {@code get(long,
69    * TimeUnit)} overload in order to test that method.
70    */
getDoneFromTimeoutOverload(Future<V> future)71   static <V> V getDoneFromTimeoutOverload(Future<V> future) throws ExecutionException {
72     checkState(future.isDone(), "Future was expected to be done: %s", future);
73     try {
74       return getUninterruptibly(future, 0, SECONDS);
75     } catch (TimeoutException e) {
76       AssertionFailedError error = new AssertionFailedError(e.getMessage());
77       error.initCause(e);
78       throw error;
79     }
80   }
81 
TestPlatform()82   private TestPlatform() {}
83 }
84