public class FutureSocketChannelTest {
@Test
public void testEchoServer() throws Exception {
final AtomicReference<String> result = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
SocketAddress serverSocket = new InetSocketAddress(0);
final FutureServerSocketChannel fssc = new FutureServerSocketChannel().bind(serverSocket);
Consumer<FutureSocketChannel> accepted = new Consumer<FutureSocketChannel>() {
public void accept(FutureSocketChannel fsc) {
fssc.accept().thenAccept(this);
ByteBuffer bb = ByteBuffer.allocate(1024);
fsc.read(bb).thenAccept(length -> {
bb.flip();
fsc.write(bb);
fsc.close();
});
}
};
fssc.accept().thenAccept(accepted);
FutureSocketChannel fsc = new FutureSocketChannel();
SocketAddress clientSocket = new InetSocketAddress("localhost", fssc.getLocalAddress().getPort());
fsc.connect(clientSocket).thenAccept(v -> {
fsc.write(ByteBuffer.wrap("hello".getBytes())).thenAccept(sent -> {
ByteBuffer readBuffer = ByteBuffer.allocate(sent);
fsc.read(readBuffer).thenAccept(recv -> {
readBuffer.flip();