/** * {@inheritDoc} */ @Override public void onClosedChannel(ClosedChannelException e) { future.setException(e); }
/** * {@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 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 setFailure() throws Exception { assertTrue(future.setException(new Exception("failure"))); assertFailedFuture("failure"); }
futureChannel.setException(e); throw e;
@Test public void setFailureNull() throws Exception { try { future.setException(null); fail(); } catch (NullPointerException expected) { } assertFalse(future.isDone()); assertTrue(future.setException(new Exception("failure"))); assertFailedFuture("failure"); }
@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 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()); }