|
22 | 22 | import com.google.common.collect.Iterables;
|
23 | 23 | import com.google.common.collect.Range;
|
24 | 24 | import com.google.common.collect.Sets;
|
| 25 | +import com.google.common.util.concurrent.internal.InternalFutureFailureAccess; |
25 | 26 | import java.util.ArrayList;
|
26 | 27 | import java.util.Collections;
|
27 | 28 | import java.util.List;
|
@@ -971,6 +972,113 @@ public void run() {
|
971 | 972 | t.join();
|
972 | 973 | }
|
973 | 974 |
|
| 975 | + public void testTrustedGetFailure_Completed() { |
| 976 | + SettableFuture<String> future = SettableFuture.create(); |
| 977 | + future.set("261"); |
| 978 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 979 | + } |
| 980 | + |
| 981 | + public void testTrustedGetFailure_Failed() { |
| 982 | + SettableFuture<String> future = SettableFuture.create(); |
| 983 | + Throwable failure = new Throwable(); |
| 984 | + future.setException(failure); |
| 985 | + assertThat(future.tryInternalFastPathGetFailure()).isEqualTo(failure); |
| 986 | + } |
| 987 | + |
| 988 | + public void testTrustedGetFailure_NotCompleted() { |
| 989 | + SettableFuture<String> future = SettableFuture.create(); |
| 990 | + assertThat(future.isDone()).isFalse(); |
| 991 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 992 | + } |
| 993 | + |
| 994 | + public void testTrustedGetFailure_CanceledNoCause() { |
| 995 | + SettableFuture<String> future = SettableFuture.create(); |
| 996 | + future.cancel(false); |
| 997 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 998 | + } |
| 999 | + |
| 1000 | + public void testGetFailure_Completed() { |
| 1001 | + AbstractFuture<String> future = new AbstractFuture<String>() {}; |
| 1002 | + future.set("261"); |
| 1003 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 1004 | + } |
| 1005 | + |
| 1006 | + public void testGetFailure_Failed() { |
| 1007 | + AbstractFuture<String> future = new AbstractFuture<String>() {}; |
| 1008 | + final Throwable failure = new Throwable(); |
| 1009 | + future.setException(failure); |
| 1010 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 1011 | + } |
| 1012 | + |
| 1013 | + public void testGetFailure_NotCompleted() { |
| 1014 | + AbstractFuture<String> future = new AbstractFuture<String>() {}; |
| 1015 | + assertThat(future.isDone()).isFalse(); |
| 1016 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 1017 | + } |
| 1018 | + |
| 1019 | + public void testGetFailure_CanceledNoCause() { |
| 1020 | + AbstractFuture<String> future = new AbstractFuture<String>() {}; |
| 1021 | + future.cancel(false); |
| 1022 | + assertThat(future.tryInternalFastPathGetFailure()).isNull(); |
| 1023 | + } |
| 1024 | + |
| 1025 | + public void testForwardExceptionFastPath() throws Exception { |
| 1026 | + class FailFuture extends InternalFutureFailureAccess implements ListenableFuture<String> { |
| 1027 | + Throwable failure; |
| 1028 | + |
| 1029 | + FailFuture(Throwable throwable) { |
| 1030 | + failure = throwable; |
| 1031 | + } |
| 1032 | + |
| 1033 | + @Override |
| 1034 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 1035 | + throw new AssertionFailedError("cancel shouldn't be called on this object"); |
| 1036 | + } |
| 1037 | + |
| 1038 | + @Override |
| 1039 | + public boolean isCancelled() { |
| 1040 | + return false; |
| 1041 | + } |
| 1042 | + |
| 1043 | + @Override |
| 1044 | + public boolean isDone() { |
| 1045 | + return true; |
| 1046 | + } |
| 1047 | + |
| 1048 | + @Override |
| 1049 | + public String get() throws InterruptedException, ExecutionException { |
| 1050 | + throw new AssertionFailedError("get() shouldn't be called on this object"); |
| 1051 | + } |
| 1052 | + |
| 1053 | + @Override |
| 1054 | + public String get(long timeout, TimeUnit unit) |
| 1055 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 1056 | + return get(); |
| 1057 | + } |
| 1058 | + |
| 1059 | + @Override |
| 1060 | + protected Throwable tryInternalFastPathGetFailure() { |
| 1061 | + return failure; |
| 1062 | + } |
| 1063 | + |
| 1064 | + @Override |
| 1065 | + public void addListener(Runnable listener, Executor executor) { |
| 1066 | + throw new AssertionFailedError("addListener() shouldn't be called on this object"); |
| 1067 | + } |
| 1068 | + } |
| 1069 | + |
| 1070 | + final RuntimeException exception = new RuntimeException("you still didn't say the magic word!"); |
| 1071 | + SettableFuture<String> normalFuture = SettableFuture.create(); |
| 1072 | + normalFuture.setFuture(new FailFuture(exception)); |
| 1073 | + assertTrue(normalFuture.isDone()); |
| 1074 | + try { |
| 1075 | + normalFuture.get(); |
| 1076 | + fail(); |
| 1077 | + } catch (ExecutionException e) { |
| 1078 | + assertSame(exception, e.getCause()); |
| 1079 | + } |
| 1080 | + } |
| 1081 | + |
974 | 1082 | private static void awaitUnchecked(final CyclicBarrier barrier) {
|
975 | 1083 | try {
|
976 | 1084 | barrier.await();
|
|
0 commit comments