@Before public void setUp() throws Exception { exec = Executors.newCachedThreadPool(); latch = new CountDownLatch(1); future = SettableFuture.create(); future.addListener(new Runnable() { @Override public void run() { latch.countDown(); } }, exec); assertEquals(1, latch.getCount()); assertFalse(future.isDone()); assertFalse(future.isCancelled()); }
@Test public void cancel_beforeSet() throws Exception { SettableFuture<Object> async = SettableFuture.create(); async.cancel(true); assertFalse(async.set(42)); }
@Test public void create() throws Exception { SettableFuture<Integer> future = SettableFuture.create(); assertFalse(future.isDone()); assertFalse(future.isCancelled()); }
@Test public void setException() throws Exception { SettableFuture<Object> future = SettableFuture.create(); Exception e = new Exception("foobarbaz"); assertTrue(future.setException(e)); // Later attempts to set the future should return false. assertFalse(future.set(23)); assertFalse(future.setException(new Exception("quux"))); // Check that the future has been set properly. assertTrue(future.isDone()); assertFalse(future.isCancelled()); try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException ee) { assertSame(e, ee.getCause()); } }
@Test public void setValue_simpleThreaded() throws Exception { SettableFuture<Integer> future = SettableFuture.create(); assertTrue(future.set(42)); // Later attempts to set the future should return false. assertFalse(future.set(23)); assertFalse(future.setException(new Exception("bar"))); // Check that the future has been set properly. assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertEquals(42, (int) future.get()); }