use of org.spf4j.io.tcp.proxy.ProxyClientHandler in project spf4j by zolyfarkas.
the class TcpServerTest method testRestart.
@Test(timeout = 100000)
public void testRestart() throws IOException, InterruptedException, TimeoutException {
ForkJoinPool pool = new ForkJoinPool(1024);
try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts("bla", 80), null, null, 10000, 5000), 1979, 10)) {
server.startAsync().awaitRunning(10, TimeUnit.SECONDS);
server.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
server.startAsync().awaitRunning(10, TimeUnit.SECONDS);
Assert.assertTrue(server.isRunning());
}
}
use of org.spf4j.io.tcp.proxy.ProxyClientHandler in project spf4j by zolyfarkas.
the class TcpServerTest method testProxy.
@Test(timeout = 100000)
public void testProxy() throws IOException, InterruptedException {
ForkJoinPool pool = new ForkJoinPool(1024);
try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts(TEST_SITE, TEST_PORT), printSnifferFactory, printSnifferFactory, 10000, 5000), 1976, 10)) {
server.startAsync().awaitRunning();
long start = System.currentTimeMillis();
byte[] originalContent = readfromSite("http://" + TEST_SITE + ':' + TEST_PORT);
long time1 = System.currentTimeMillis();
byte[] proxiedContent = readfromSite("http://localhost:1976");
long time2 = System.currentTimeMillis();
LOG.debug("Direct = {} ms, proxied = {}", (time1 - start), (time2 - time1));
Assert.assertArrayEquals(originalContent, proxiedContent);
}
}
use of org.spf4j.io.tcp.proxy.ProxyClientHandler 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));
}
}
}
use of org.spf4j.io.tcp.proxy.ProxyClientHandler in project spf4j by zolyfarkas.
the class TcpServerTest method testKill.
@Test(expected = java.net.SocketException.class, timeout = 10000)
public void testKill() throws IOException, InterruptedException {
String testSite = "10.10.10.10";
ForkJoinPool pool = new ForkJoinPool(1024);
try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts(testSite, 80), null, null, 10000, 10000), 1982, 10)) {
server.startAsync().awaitRunning();
DefaultScheduler.INSTANCE.schedule(new AbstractRunnable(true) {
@Override
public void doRun() throws IOException {
server.close();
}
}, 2, TimeUnit.SECONDS);
// results in a socket exception after 5 seconds
readfromSite("http://localhost:1982");
Assert.fail("Should timeout");
}
}
use of org.spf4j.io.tcp.proxy.ProxyClientHandler in project spf4j by zolyfarkas.
the class TcpServerTest method testProxySimple.
@Test(timeout = 100000)
public void testProxySimple() throws IOException, InterruptedException {
ForkJoinPool pool = new ForkJoinPool(1024);
try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts(TEST_SITE, TEST_PORT), printSnifferFactory, printSnifferFactory, 10000, 5000), 1977, 10)) {
server.startAsync().awaitRunning();
byte[] proxiedContent = readfromSite("http://localhost:1977");
Assert.assertNotNull(proxiedContent);
}
}
Aggregations