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