Search in sources :

Example 26 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class SelectorManagerTest method testConnectTimeoutBeforeSuccessfulConnect.

@Slow
@Test
public void testConnectTimeoutBeforeSuccessfulConnect() throws Exception {
    ServerSocketChannel server = ServerSocketChannel.open();
    server.bind(new InetSocketAddress("localhost", 0));
    SocketAddress address = server.getLocalAddress();
    final AtomicLong timeoutConnection = new AtomicLong();
    final long connectTimeout = 1000;
    SelectorManager selectorManager = new SelectorManager(executor, scheduler) {

        @Override
        protected EndPoint newEndPoint(SelectableChannel channel, ManagedSelector selector, SelectionKey key) throws IOException {
            SocketChannelEndPoint endp = new SocketChannelEndPoint(channel, selector, key, getScheduler());
            endp.setIdleTimeout(connectTimeout / 2);
            return endp;
        }

        @Override
        protected boolean doFinishConnect(SelectableChannel channel) throws IOException {
            try {
                long timeout = timeoutConnection.get();
                if (timeout > 0)
                    TimeUnit.MILLISECONDS.sleep(timeout);
                return super.doFinishConnect(channel);
            } catch (InterruptedException e) {
                return false;
            }
        }

        @Override
        public Connection newConnection(SelectableChannel channel, EndPoint endpoint, Object attachment) throws IOException {
            ((Callback) attachment).succeeded();
            return new AbstractConnection(endpoint, executor) {

                @Override
                public void onFillable() {
                }
            };
        }

        @Override
        protected void connectionFailed(SelectableChannel channel, Throwable ex, Object attachment) {
            ((Callback) attachment).failed(ex);
        }
    };
    selectorManager.setConnectTimeout(connectTimeout);
    selectorManager.start();
    try {
        SocketChannel client1 = SocketChannel.open();
        client1.configureBlocking(false);
        client1.connect(address);
        long timeout = connectTimeout * 2;
        timeoutConnection.set(timeout);
        final CountDownLatch latch1 = new CountDownLatch(1);
        selectorManager.connect(client1, new Callback() {

            @Override
            public void failed(Throwable x) {
                latch1.countDown();
            }
        });
        Assert.assertTrue(latch1.await(connectTimeout * 3, TimeUnit.MILLISECONDS));
        Assert.assertFalse(client1.isOpen());
        // Wait for the first connect to finish, as the selector thread is waiting in finishConnect().
        Thread.sleep(timeout);
        // Verify that after the failure we can connect successfully.
        try (SocketChannel client2 = SocketChannel.open()) {
            client2.configureBlocking(false);
            client2.connect(address);
            timeoutConnection.set(0);
            final CountDownLatch latch2 = new CountDownLatch(1);
            selectorManager.connect(client2, new Callback() {

                @Override
                public void succeeded() {
                    latch2.countDown();
                }
            });
            Assert.assertTrue(latch2.await(connectTimeout * 5, TimeUnit.MILLISECONDS));
            Assert.assertTrue(client2.isOpen());
        }
    } finally {
        selectorManager.stop();
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Callback(org.eclipse.jetty.util.Callback) SelectableChannel(java.nio.channels.SelectableChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 27 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class ConnectionOpenCloseTest method testOpenClose.

@Slow
@Test
public void testOpenClose() throws Exception {
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            throw new IllegalStateException();
        }
    });
    server.start();
    final AtomicInteger callbacks = new AtomicInteger();
    final CountDownLatch openLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    connector.addBean(new Connection.Listener.Adapter() {

        @Override
        public void onOpened(Connection connection) {
            callbacks.incrementAndGet();
            openLatch.countDown();
        }

        @Override
        public void onClosed(Connection connection) {
            callbacks.incrementAndGet();
            closeLatch.countDown();
        }
    });
    try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
        socket.setSoTimeout((int) connector.getIdleTimeout());
        Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
        socket.shutdownOutput();
        Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
        String response = IO.toString(socket.getInputStream());
        Assert.assertEquals(0, response.length());
        // Wait some time to see if the callbacks are called too many times
        TimeUnit.MILLISECONDS.sleep(200);
        Assert.assertEquals(2, callbacks.get());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 28 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class ConnectionOpenCloseTest method testSSLOpenRequestClose.

@Slow
@Test
public void testSSLOpenRequestClose() throws Exception {
    SslContextFactory sslContextFactory = new SslContextFactory();
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    sslContextFactory.setKeyStoreResource(Resource.newResource(keystore));
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    server.addBean(sslContextFactory);
    server.removeConnector(connector);
    connector = new ServerConnector(server, sslContextFactory);
    server.addConnector(connector);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    server.start();
    final AtomicInteger callbacks = new AtomicInteger();
    final CountDownLatch openLatch = new CountDownLatch(2);
    final CountDownLatch closeLatch = new CountDownLatch(2);
    connector.addBean(new Connection.Listener.Adapter() {

        @Override
        public void onOpened(Connection connection) {
            callbacks.incrementAndGet();
            openLatch.countDown();
        }

        @Override
        public void onClosed(Connection connection) {
            callbacks.incrementAndGet();
            closeLatch.countDown();
        }
    });
    Socket socket = sslContextFactory.getSslContext().getSocketFactory().createSocket("localhost", connector.getLocalPort());
    socket.setSoTimeout((int) connector.getIdleTimeout());
    OutputStream output = socket.getOutputStream();
    output.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost:" + connector.getLocalPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
    output.flush();
    InputStream inputStream = socket.getInputStream();
    HttpTester.Response response = HttpTester.parseResponse(inputStream);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(-1, inputStream.read());
    socket.close();
    Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
    // Wait some time to see if the callbacks are called too many times
    TimeUnit.SECONDS.sleep(1);
    Assert.assertEquals(4, callbacks.get());
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 29 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_IdleConnection_IsClosed_OnRemoteClose.

@Slow
@Test
public void test_IdleConnection_IsClosed_OnRemoteClose() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(30, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    connector.stop();
    // Give the connection some time to process the remote close
    TimeUnit.SECONDS.sleep(1);
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Connection(org.eclipse.jetty.client.api.Connection) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 30 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class HttpClientTimeoutTest method testConnectTimeoutIsCancelledByShorterRequestTimeout.

@Slow
@Test
public void testConnectTimeoutIsCancelledByShorterRequestTimeout() throws Exception {
    String host = "10.255.255.1";
    int port = 80;
    int connectTimeout = 2000;
    assumeConnectTimeout(host, port, connectTimeout);
    start(new EmptyServerHandler());
    client.stop();
    client.setConnectTimeout(connectTimeout);
    client.start();
    final AtomicInteger completes = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(2);
    Request request = client.newRequest(host, port);
    request.scheme(scheme).timeout(connectTimeout / 2, TimeUnit.MILLISECONDS).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completes.incrementAndGet();
            latch.countDown();
        }
    });
    Assert.assertFalse(latch.await(2 * connectTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals(1, completes.get());
    Assert.assertNotNull(request.getAbortCause());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)37 Test (org.junit.Test)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 IOException (java.io.IOException)11 Result (org.eclipse.jetty.client.api.Result)11 ServletException (javax.servlet.ServletException)10 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)10 Request (org.eclipse.jetty.client.api.Request)9 Socket (java.net.Socket)7 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 Response (org.eclipse.jetty.client.api.Response)6 Connection (org.eclipse.jetty.client.api.Connection)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)5 Request (org.eclipse.jetty.server.Request)5 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)5 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)5