@Test public void testConnect() throws Exception { System.out.println("connect"); SocketAddress remote = new InetSocketAddress("localhost", 80); SocketChannelWithTimeoutsEmul instance = new SocketChannelWithTimeoutsEmul(); instance.setSelectedKeys(new HashSet<SelectionKey>()); boolean expResult = true; boolean result = instance.connect(remote); Assert.assertEquals(expResult, result); }
@Override public boolean connect(SocketAddress remote) throws IOException { long start = System.currentTimeMillis(); socketChannel.connect(remote); if (selector.select(connectTimeout) > 0) { selector.selectedKeys().remove(channelKey); if (!channelKey.isConnectable()) { throw new IllegalStateException("Socket channel is in not connectable state"); } socketChannel.finishConnect(); channelKey = socketChannel.register(selector, SelectionKey.OP_READ); if (log.isDebugEnabled()) { log.debug("Connected socket in " + (System.currentTimeMillis() - start)); } if (!socketChannel.isConnected()) { throw new SocketException("SocketChannel not connected on some reason"); } return true; } throw new SocketTimeoutException("Failed to connect to " + remote.toString()); }
SocketChannelWithTimeouts extends SocketChannel { @Override public boolean connect(SocketAddress remote) throws IOException { long start = System.currentTimeMillis(); socketChannel.connect(remote); if (selector.select(connectTimeout) > 0) { selector.selectedKeys().remove(channelKey); if (!channelKey.isConnectable()) { throw new IllegalStateException("Socket channel is in not connectable state"); } socketChannel.finishConnect(); channelKey = socketChannel.register(selector, SelectionKey.OP_READ); if (log.isDebugEnabled()) { log.debug("Connected socket in " + (System.currentTimeMillis() - start)); } if (!socketChannel.isConnected()) { throw new SocketException("SocketChannel not connected on some reason"); } return true; } throw new SocketTimeoutException("Failed to connect to " + remote.toString()); } }
SocketChannelWithTimeouts extends SocketChannel { @Override public boolean connect(SocketAddress remote) throws IOException { long start = System.currentTimeMillis(); socketChannel.connect(remote); if (selector.select(connectTimeout) > 0) { selector.selectedKeys().remove(channelKey); if (!channelKey.isConnectable()) { throw new IllegalStateException("Socket channel is in not connectable state"); } socketChannel.finishConnect(); channelKey = socketChannel.register(selector, SelectionKey.OP_READ); if (log.isDebugEnabled()) { log.debug("Connected socket in " + (System.currentTimeMillis() - start)); } if (!socketChannel.isConnected()) { throw new SocketException("SocketChannel not connected on some reason"); } return true; } throw new SocketTimeoutException("Failed to connect to " + remote.toString()); } protected  SocketChannelWithTimeouts(); }
SocketChannelWithTimeouts extends SocketChannel { @Override public boolean connect(SocketAddress remote) throws IOException { long start = System.currentTimeMillis(); socketChannel.connect(remote); if (selector.select(connectTimeout) > 0) { selector.selectedKeys().remove(channelKey); if (!channelKey.isConnectable()) { throw new IllegalStateException("Socket channel is in not connectable state"); } socketChannel.finishConnect(); channelKey = socketChannel.register(selector, SelectionKey.OP_READ); if (log.isDebugEnabled()) { log.debug("Connected socket in " + (System.currentTimeMillis() - start)); } if (!socketChannel.isConnected()) { throw new SocketException("SocketChannel not connected on some reason"); } return true; } throw new SocketTimeoutException("Failed to connect to " + remote.toString()); } protected  SocketChannelWithTimeouts(); static SocketChannel open(); @Override SocketChannel bind(SocketAddress socketAddress); @Override SocketAddress getLocalAddress(); @Override SocketChannel setOption(SocketOption<T> socketOption, T t); @Override T getOption(SocketOption<T> socketOption); @Override Set<SocketOption<?>> supportedOptions(); @Override SocketChannel shutdownInput(); @Override SocketChannel shutdownOutput(); @Override boolean connect(SocketAddress remote); @Override int read(ByteBuffer dst); @Override long read(ByteBuffer[] dsts, int offset, int length); @Override int write(ByteBuffer src); @Override long write(ByteBuffer[] srcs, int offset, int length); @Override Socket socket(); @Override boolean isConnected(); @Override boolean isConnectionPending(); @Override boolean finishConnect(); void setConnectTimeout(int t); void setReadTimeout(int t); SocketAddress getRemoteAddress(); }
SocketChannelWithTimeouts extends SocketChannel { @Override public boolean connect(SocketAddress remote) throws IOException { long start = System.currentTimeMillis(); socketChannel.connect(remote); if (selector.select(connectTimeout) > 0) { selector.selectedKeys().remove(channelKey); if (!channelKey.isConnectable()) { throw new IllegalStateException("Socket channel is in not connectable state"); } socketChannel.finishConnect(); channelKey = socketChannel.register(selector, SelectionKey.OP_READ); if (log.isDebugEnabled()) { log.debug("Connected socket in " + (System.currentTimeMillis() - start)); } if (!socketChannel.isConnected()) { throw new SocketException("SocketChannel not connected on some reason"); } return true; } throw new SocketTimeoutException("Failed to connect to " + remote.toString()); } protected  SocketChannelWithTimeouts(); static SocketChannel open(); @Override SocketChannel bind(SocketAddress socketAddress); @Override SocketAddress getLocalAddress(); @Override SocketChannel setOption(SocketOption<T> socketOption, T t); @Override T getOption(SocketOption<T> socketOption); @Override Set<SocketOption<?>> supportedOptions(); @Override SocketChannel shutdownInput(); @Override SocketChannel shutdownOutput(); @Override boolean connect(SocketAddress remote); @Override int read(ByteBuffer dst); @Override long read(ByteBuffer[] dsts, int offset, int length); @Override int write(ByteBuffer src); @Override long write(ByteBuffer[] srcs, int offset, int length); @Override Socket socket(); @Override boolean isConnected(); @Override boolean isConnectionPending(); @Override boolean finishConnect(); void setConnectTimeout(int t); void setReadTimeout(int t); SocketAddress getRemoteAddress();  }