use of org.spf4j.io.tcp.ClientHandler in project spf4j by zolyfarkas.
the class TcpServerTest method testRejectingServer.
@Test(expected = IOException.class, timeout = 10000)
public void testRejectingServer() throws IOException, InterruptedException {
String testSite = "localhost";
ForkJoinPool pool = new ForkJoinPool(1024);
try (TcpServer rejServer = new TcpServer(pool, new ClientHandler() {
@Override
public void handle(final Selector serverSelector, final SocketChannel clientChannel, final ExecutorService exec, final BlockingQueue<Runnable> tasksToRunBySelector, final UpdateablePriorityQueue<DeadlineAction> deadlineActions) throws IOException {
clientChannel.configureBlocking(true);
ByteBuffer allocate = ByteBuffer.allocate(1024);
// read something
clientChannel.read(allocate);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
allocate.flip();
clientChannel.write(allocate);
clientChannel.close();
}
}, 1980, 10)) {
rejServer.startAsync().awaitRunning();
try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts(testSite, 1980), null, null, 10000, 5000), 1981, 10)) {
server.startAsync().awaitRunning();
byte[] readfromSite = readfromSite("http://localhost:1981");
// probably wrong charset assumtion
LOG.debug("Response: {}", new String(readfromSite, StandardCharsets.UTF_8));
}
}
}
Aggregations