• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import java.util.concurrent.CancellationException;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import junit.framework.TestCase;
27 
28 /**
29  * Test cases for {@link SettableFuture}.
30  *
31  * @author Sven Mawson
32  */
33 public class SettableFutureTest extends TestCase {
34 
35   private SettableFuture<String> future;
36   private ListenableFutureTester tester;
37 
38   @Override
setUp()39   protected void setUp() throws Exception {
40     super.setUp();
41 
42     future = SettableFuture.create();
43     tester = new ListenableFutureTester(future);
44     tester.setUp();
45   }
46 
testDefaultState()47   public void testDefaultState() throws Exception {
48     assertThrows(TimeoutException.class, () -> future.get(5, TimeUnit.MILLISECONDS));
49   }
50 
testSetValue()51   public void testSetValue() throws Exception {
52     assertTrue(future.set("value"));
53     tester.testCompletedFuture("value");
54   }
55 
testSetFailure()56   public void testSetFailure() throws Exception {
57     assertTrue(future.setException(new Exception("failure")));
58     tester.testFailedFuture("failure");
59   }
60 
testSetFailureNull()61   public void testSetFailureNull() throws Exception {
62     assertThrows(NullPointerException.class, () -> future.setException(null));
63     assertFalse(future.isDone());
64     assertTrue(future.setException(new Exception("failure")));
65     tester.testFailedFuture("failure");
66   }
67 
testCancel()68   public void testCancel() throws Exception {
69     assertTrue(future.cancel(true));
70     tester.testCancelledFuture();
71   }
72 
73   /** Tests the initial state of the future. */
testCreate()74   public void testCreate() throws Exception {
75     SettableFuture<Integer> future = SettableFuture.create();
76     assertFalse(future.isDone());
77     assertFalse(future.isCancelled());
78   }
79 
testSetValue_simpleThreaded()80   public void testSetValue_simpleThreaded() throws Exception {
81     SettableFuture<Integer> future = SettableFuture.create();
82     assertTrue(future.set(42));
83     // Later attempts to set the future should return false.
84     assertFalse(future.set(23));
85     assertFalse(future.setException(new Exception("bar")));
86     assertFalse(future.setFuture(SettableFuture.<Integer>create()));
87     // Check that the future has been set properly.
88     assertTrue(future.isDone());
89     assertFalse(future.isCancelled());
90     assertEquals(42, (int) future.get());
91   }
92 
testSetException()93   public void testSetException() throws Exception {
94     SettableFuture<Object> future = SettableFuture.create();
95     Exception e = new Exception("foobarbaz");
96     assertTrue(future.setException(e));
97     // Later attempts to set the future should return false.
98     assertFalse(future.set(23));
99     assertFalse(future.setException(new Exception("quux")));
100     assertFalse(future.setFuture(SettableFuture.create()));
101     // Check that the future has been set properly.
102     assertTrue(future.isDone());
103     assertFalse(future.isCancelled());
104     ExecutionException ee = assertThrows(ExecutionException.class, () -> future.get());
105     assertThat(ee).hasCauseThat().isSameInstanceAs(e);
106   }
107 
testSetFuture()108   public void testSetFuture() throws Exception {
109     SettableFuture<String> future = SettableFuture.create();
110     SettableFuture<String> nested = SettableFuture.create();
111     assertTrue(future.setFuture(nested));
112     // Later attempts to set the future should return false.
113     assertFalse(future.set("x"));
114     assertFalse(future.setException(new Exception("bar")));
115     assertFalse(future.setFuture(SettableFuture.<String>create()));
116     // Check that the future has been set properly.
117     assertFalse(future.isDone());
118     assertFalse(future.isCancelled());
119     assertThrows(TimeoutException.class, () -> future.get(0, TimeUnit.MILLISECONDS));
120     nested.set("foo");
121     assertTrue(future.isDone());
122     assertFalse(future.isCancelled());
123     assertEquals("foo", future.get());
124   }
125 
126   private static class Foo {}
127 
128   private static class FooChild extends Foo {}
129 
testSetFuture_genericsHierarchy()130   public void testSetFuture_genericsHierarchy() throws Exception {
131     SettableFuture<Foo> future = SettableFuture.create();
132     SettableFuture<FooChild> nested = SettableFuture.create();
133     assertTrue(future.setFuture(nested));
134     // Later attempts to set the future should return false.
135     assertFalse(future.set(new Foo()));
136     assertFalse(future.setException(new Exception("bar")));
137     assertFalse(future.setFuture(SettableFuture.<Foo>create()));
138     // Check that the future has been set properly.
139     assertFalse(future.isDone());
140     assertFalse(future.isCancelled());
141     assertThrows(TimeoutException.class, () -> future.get(0, TimeUnit.MILLISECONDS));
142     FooChild value = new FooChild();
143     nested.set(value);
144     assertTrue(future.isDone());
145     assertFalse(future.isCancelled());
146     assertSame(value, future.get());
147   }
148 
testCancel_innerCancelsAsync()149   public void testCancel_innerCancelsAsync() throws Exception {
150     SettableFuture<Object> async = SettableFuture.create();
151     SettableFuture<Object> inner = SettableFuture.create();
152     async.setFuture(inner);
153     inner.cancel(true);
154     assertTrue(async.isCancelled());
155     assertThrows(CancellationException.class, () -> async.get());
156   }
157 
testCancel_resultCancelsInner_interrupted()158   public void testCancel_resultCancelsInner_interrupted() throws Exception {
159     SettableFuture<Object> async = SettableFuture.create();
160     SettableFuture<Object> inner = SettableFuture.create();
161     async.setFuture(inner);
162     async.cancel(true);
163     assertTrue(inner.isCancelled());
164     assertTrue(inner.wasInterrupted());
165     assertThrows(CancellationException.class, () -> inner.get());
166   }
167 
testCancel_resultCancelsInner()168   public void testCancel_resultCancelsInner() throws Exception {
169     SettableFuture<Object> async = SettableFuture.create();
170     SettableFuture<Object> inner = SettableFuture.create();
171     async.setFuture(inner);
172     async.cancel(false);
173     assertTrue(inner.isCancelled());
174     assertFalse(inner.wasInterrupted());
175     assertThrows(CancellationException.class, () -> inner.get());
176   }
177 
testCancel_beforeSet()178   public void testCancel_beforeSet() throws Exception {
179     SettableFuture<Object> async = SettableFuture.create();
180     async.cancel(true);
181     assertFalse(async.set(42));
182   }
183 
testCancel_multipleBeforeSetFuture_noInterruptFirst()184   public void testCancel_multipleBeforeSetFuture_noInterruptFirst() throws Exception {
185     SettableFuture<Object> async = SettableFuture.create();
186     async.cancel(false);
187     async.cancel(true);
188     SettableFuture<Object> inner = SettableFuture.create();
189     assertFalse(async.setFuture(inner));
190     assertTrue(inner.isCancelled());
191     assertFalse(inner.wasInterrupted());
192   }
193 
testCancel_multipleBeforeSetFuture_interruptFirst()194   public void testCancel_multipleBeforeSetFuture_interruptFirst() throws Exception {
195     SettableFuture<Object> async = SettableFuture.create();
196     async.cancel(true);
197     async.cancel(false);
198     SettableFuture<Object> inner = SettableFuture.create();
199     assertFalse(async.setFuture(inner));
200     assertTrue(inner.isCancelled());
201     assertTrue(inner.wasInterrupted());
202   }
203 }
204