Search in sources :

Example 1 with TcpServer

use of org.spf4j.io.tcp.TcpServer 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());
    }
}
Also used : ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) TcpServer(org.spf4j.io.tcp.TcpServer) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 2 with TcpServer

use of org.spf4j.io.tcp.TcpServer 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);
    }
}
Also used : ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) TcpServer(org.spf4j.io.tcp.TcpServer) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 3 with TcpServer

use of org.spf4j.io.tcp.TcpServer 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));
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ClientHandler(org.spf4j.io.tcp.ClientHandler) ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) IOException(java.io.IOException) TcpServer(org.spf4j.io.tcp.TcpServer) ByteBuffer(java.nio.ByteBuffer) ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) DeadlineAction(org.spf4j.io.tcp.DeadlineAction) AbstractRunnable(org.spf4j.base.AbstractRunnable) ExecutorService(java.util.concurrent.ExecutorService) ForkJoinPool(java.util.concurrent.ForkJoinPool) Selector(java.nio.channels.Selector) Test(org.junit.Test)

Example 4 with TcpServer

use of org.spf4j.io.tcp.TcpServer 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");
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) IOException(java.io.IOException) TcpServer(org.spf4j.io.tcp.TcpServer) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 5 with TcpServer

use of org.spf4j.io.tcp.TcpServer 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);
    }
}
Also used : ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) TcpServer(org.spf4j.io.tcp.TcpServer) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Aggregations

ForkJoinPool (java.util.concurrent.ForkJoinPool)6 Test (org.junit.Test)6 TcpServer (org.spf4j.io.tcp.TcpServer)6 ProxyClientHandler (org.spf4j.io.tcp.proxy.ProxyClientHandler)6 IOException (java.io.IOException)2 AbstractRunnable (org.spf4j.base.AbstractRunnable)2 ByteBuffer (java.nio.ByteBuffer)1 Selector (java.nio.channels.Selector)1 SocketChannel (java.nio.channels.SocketChannel)1 ExecutorService (java.util.concurrent.ExecutorService)1 ClientHandler (org.spf4j.io.tcp.ClientHandler)1 DeadlineAction (org.spf4j.io.tcp.DeadlineAction)1