bug6057: implemented a yet untested connection handling with message streaming to SensorFixStore

This commit is contained in:
Axel Uhl
2024-09-17 18:17:29 +02:00
parent 2fff330497
commit e5473c7e82
10 changed files with 272 additions and 124 deletions
@@ -0,0 +1,63 @@
package com.sap.sse.test.nio;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
public class TestReadingFromAsynchronousServerSocket {
@Test
public void testAccepting() throws IOException, InterruptedException, ExecutionException {
final CompletableFuture<Byte> resultFuture = new CompletableFuture<>();
final ByteBuffer buf = ByteBuffer.allocateDirect(1024);
final AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(0));
final SocketAddress localAddress = serverSocketChannel.getLocalAddress();
serverSocketChannel.accept("Humba", new CompletionHandler<AsynchronousSocketChannel, String>() {
@Override
public void completed(AsynchronousSocketChannel result, String attachment) {
assertEquals("Humba", attachment);
handleClientConnections(result, buf, resultFuture);
}
@Override
public void failed(Throwable exc, String attachment) {
fail(exc.getMessage()+": "+attachment);
}
});
final Socket s = new Socket("127.0.0.1", ((InetSocketAddress) localAddress).getPort());
s.getOutputStream().write(42);
s.close();
assertEquals(42, resultFuture.get().byteValue());
}
private void handleClientConnections(AsynchronousSocketChannel socketChannel, ByteBuffer buf, CompletableFuture<Byte> resultFuture) {
socketChannel.read(buf, "Trala", new CompletionHandler<Integer, String>() {
@Override
public void completed(Integer result, String attachment) {
assertEquals("Trala", attachment);
buf.flip();
final byte[] bytesRead = new byte[result];
buf.get(bytesRead);
assertEquals(1, result.intValue());
resultFuture.complete(bytesRead[0]);
}
@Override
public void failed(Throwable exc, String attachment) {
fail(exc.getMessage()+": "+attachment);
}
});
}
}