@Override public void destroy() { OutputBuffer outputBuffer; List<PendingRead> pendingReads = ImmutableList.of(); synchronized (this) { if (delegate == null) { // ignore destroy if the buffer already in a terminal state. if (!state.setIf(FINISHED, state -> !state.isTerminal())) { return; } pendingReads = ImmutableList.copyOf(this.pendingReads); this.pendingReads.clear(); } outputBuffer = delegate; } // if there is no output buffer, free the pending reads if (outputBuffer == null) { for (PendingRead pendingRead : pendingReads) { pendingRead.getFutureResult().set(emptyResults(taskInstanceId, 0, true)); } return; } outputBuffer.destroy(); }
public void process(OutputBuffer delegate) { if (futureResult.isDone()) { return; } try { ListenableFuture<BufferResult> result = delegate.get(bufferId, startingSequenceId, maxSize); futureResult.setAsync(result); } catch (Exception e) { futureResult.setException(e); } } }
ExtendedSettableFuture<String> fromFuture = ExtendedSettableFuture.create(); ExtendedSettableFuture<String> toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); fromFuture.set("abc"); assertEquals(toFuture.get(), "abc"); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); fromFuture.setException(new RuntimeException()); assertThrows(ExecutionException.class, toFuture::get); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); toFuture.cancel(false); assertTrue(fromFuture.isCancelled()); assertFalse(fromFuture.checkWasInterrupted()); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); toFuture.cancel(true); assertTrue(fromFuture.isCancelled()); assertTrue(fromFuture.checkWasInterrupted());
@Test public void testCancelWithInterrupt() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.cancel(true); assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(future.checkWasInterrupted()); assertThrows(CancellationException.class, future::get); }
@Test public void testSet() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.set("abc"); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertFalse(future.checkWasInterrupted()); assertEquals(future.get(), "abc"); }
ExtendedSettableFuture<String> fromFuture = ExtendedSettableFuture.create(); SettableFuture<String> toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, true); fromFuture.set("abc"); assertEquals(toFuture.get(), "abc"); fromFuture = ExtendedSettableFuture.create(); toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, true); fromFuture.setException(new RuntimeException()); assertThrows(ExecutionException.class, toFuture::get); fromFuture = ExtendedSettableFuture.create(); toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, false); toFuture.cancel(true); assertTrue(fromFuture.isCancelled()); assertFalse(fromFuture.checkWasInterrupted()); fromFuture = ExtendedSettableFuture.create(); toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, true); toFuture.cancel(false); assertTrue(fromFuture.isCancelled()); assertTrue(fromFuture.checkWasInterrupted());
@Test public void testSetException() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.setException(new Exception("")); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertFalse(future.checkWasInterrupted()); assertThrows(ExecutionException.class, future::get); }
set(getDone(delegate)); setException(e.getCause()); setException(e);
/** * Creates a future that completes when the first future completes either normally * or exceptionally. Cancellation of the future propagates to the supplied futures. */ public static <V> ListenableFuture<V> whenAnyComplete(Iterable<? extends ListenableFuture<? extends V>> futures) { requireNonNull(futures, "futures is null"); checkArgument(!isEmpty(futures), "futures is empty"); ExtendedSettableFuture<V> firstCompletedFuture = ExtendedSettableFuture.create(); for (ListenableFuture<? extends V> future : futures) { firstCompletedFuture.setAsync(future); } return firstCompletedFuture; }
public static <V> ExtendedSettableFuture<V> create() { return new ExtendedSettableFuture<>(); }
ExtendedSettableFuture<String> fromFuture = ExtendedSettableFuture.create(); ExtendedSettableFuture<String> toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); fromFuture.set("abc"); assertEquals(toFuture.get(), "abc"); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); fromFuture.setException(new RuntimeException()); assertThrows(ExecutionException.class, toFuture::get); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); toFuture.cancel(false); assertTrue(fromFuture.isCancelled()); assertFalse(fromFuture.checkWasInterrupted()); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); toFuture.setAsync(fromFuture); toFuture.cancel(true); assertTrue(fromFuture.isCancelled()); assertTrue(fromFuture.checkWasInterrupted());
@Test public void testCancelWithoutInterrupt() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.cancel(false); assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertFalse(future.checkWasInterrupted()); assertThrows(CancellationException.class, future::get); }
@Test public void testSet() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.set("abc"); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertFalse(future.checkWasInterrupted()); assertEquals(future.get(), "abc"); }
ExtendedSettableFuture<String> fromFuture = ExtendedSettableFuture.create(); SettableFuture<String> toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, true); fromFuture.set("abc"); assertEquals(toFuture.get(), "abc"); fromFuture = ExtendedSettableFuture.create(); toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, true); fromFuture.setException(new RuntimeException()); assertThrows(ExecutionException.class, toFuture::get); fromFuture = ExtendedSettableFuture.create(); toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, false); toFuture.cancel(true); assertTrue(fromFuture.isCancelled()); assertFalse(fromFuture.checkWasInterrupted()); fromFuture = ExtendedSettableFuture.create(); toFuture = SettableFuture.create(); mirror(fromFuture, toFuture, true); toFuture.cancel(false); assertTrue(fromFuture.isCancelled()); assertTrue(fromFuture.checkWasInterrupted());
@Test public void testSetException() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.setException(new Exception("")); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertFalse(future.checkWasInterrupted()); assertThrows(ExecutionException.class, future::get); }
set(getDone(delegate)); setException(e.getCause()); setException(e);
/** * Creates a future that completes when the first future completes either normally * or exceptionally. Cancellation of the future propagates to the supplied futures. */ public static <V> ListenableFuture<V> whenAnyComplete(Iterable<? extends ListenableFuture<? extends V>> futures) { requireNonNull(futures, "futures is null"); checkArgument(!isEmpty(futures), "futures is empty"); ExtendedSettableFuture<V> firstCompletedFuture = ExtendedSettableFuture.create(); for (ListenableFuture<? extends V> future : futures) { firstCompletedFuture.setAsync(future); } return firstCompletedFuture; }
public static <V> ExtendedSettableFuture<V> create() { return new ExtendedSettableFuture<>(); }
@Test public void testCancelWithInterrupt() throws Exception { ExtendedSettableFuture<String> future = ExtendedSettableFuture.create(); future.cancel(true); assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(future.checkWasInterrupted()); assertThrows(CancellationException.class, future::get); }
public void process(OutputBuffer delegate) { if (futureResult.isDone()) { return; } try { ListenableFuture<BufferResult> result = delegate.get(bufferId, startingSequenceId, maxSize); futureResult.setAsync(result); } catch (Exception e) { futureResult.setException(e); } } }