/** * {@inheritDoc} */ @Override public void onReadClosed(IOException cause) throws IOException { if (futureChannel.isDone()) { if (channel != null) { channel.terminate(cause == null ? new ClosedChannelException() : cause); } } else { futureChannel.setException(cause == null ? new ClosedChannelException() : cause); } }
/** * {@inheritDoc} */ @Override public void onRegistered(SelectionKey selectionKey) { future.set(selectionKey); }
/** * Creates a new {@link SettableFuture}. * * @param <V> generic type of value. * @return a new {@link SettableFuture}. */ public static <V> SettableFuture<V> create() { return new SettableFuture<V>(); }
/** * Completes the future with the supplied exception. * * @param throwable the exception. * @return {@code true} if the future is now completed, {@code false} if the future has already been completed. */ public boolean setException(@Nonnull Throwable throwable) { verifyNonnull(throwable); boolean result; synchronized (lock) { if (done) { result = false; } else { done = true; this.throwable = throwable; lock.notifyAll(); result = true; } } if (result) { notifyListeners(); } if (throwable instanceof Error) { throw (Error) throwable; } return result; }
@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()); }
if (!futureChannel.isDone()) { assert channel == null && transport == null && capabilityLength != null; if (capabilityLength.hasRemaining()) { futureChannel.setException(e); throw e; futureChannel.set(channel); capabilityContent = null; capabilityLength = null; assert futureChannel.isDone(); try { channel = futureChannel.get(); } catch (InterruptedException e) {
public void assertCompletedFuture(@Nullable Object expectedValue) throws InterruptedException, ExecutionException { assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertEquals(expectedValue, future.get()); }
@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()); }
@Override public void onRegistered(SelectionKey selectionKey) { this.selectionKey = selectionKey; SocketAddress localAddress; try { localAddress = serverSocketChannel.getLocalAddress(); addr.set(localAddress); } catch (IOException e) { addr.setException(e); return; } try { System.out.println("Accepting connections on port " + localAddress); } catch (Exception e) { // ignore } registered.set(null); }
@Test public void create() throws Exception { SettableFuture<Integer> future = SettableFuture.create(); assertFalse(future.isDone()); assertFalse(future.isCancelled()); }
/** * {@inheritDoc} */ @Override public void onClosedChannel(ClosedChannelException e) { future.setException(e); }
private SocketAddress startServer(String listen) throws IOException, ExecutionException, InterruptedException, TimeoutException { serverSocketChannel.bind(toSocketAddress(listen)); serverSocketChannel.configureBlocking(false); acceptorHub.register(serverSocketChannel, acceptor, true, false, false, false, acceptor); acceptor.registered.get(10, TimeUnit.SECONDS); return addr.get(); }
/** * Completes the future by cancellation. * * @param mayInterruptIfRunning ignored. * @return {@code true} if the future is now cancelled, {@code false} if the future has already been completed. */ @Override public boolean cancel(boolean mayInterruptIfRunning) { boolean result; synchronized (lock) { if (done) { result = false; } else { done = true; this.cancelled = true; lock.notifyAll(); result = true; } } if (result) { notifyListeners(); } return result; }
@Test public void cancel() throws Exception { assertTrue(future.cancel(true)); assertCancelledFuture(); }
@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()); } }
public void assertCancelledFuture() throws InterruptedException, ExecutionException { assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertTrue(future.isDone()); assertTrue(future.isCancelled()); try { future.get(); fail("Future should throw CancellationException on cancel."); } catch (CancellationException expected) { } }
/** * {@inheritDoc} */ @Override public void start() throws IOException { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(BinarySafeStream.wrap(bos)); try { oos.writeObject(new Capability()); } finally { oos.close(); } ByteBuffer buffer = ByteBufferUtils.wrapUTF8(bos.toString("US-ASCII")); write(buffer); } catch (IOException e) { futureChannel.setException(e); } }
@Test public void defaultState() throws Exception { try { future.get(5, TimeUnit.MILLISECONDS); fail(); } catch (TimeoutException expected) { assertTrue(true); } }
/** * Completes the future with the supplied value. * * @param value the value (may be {@code null}. * @return {@code true} if the future is now completed, {@code false} if the future has already been completed. */ public boolean set(@Nullable V value) { boolean result; synchronized (lock) { if (done) { result = false; } else { done = true; this.value = value; lock.notifyAll(); result = true; } } if (result) { notifyListeners(); } return result; }