Search in sources :

Example 11 with Connection

use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.

the class LocalConnectorTest method testOpenClose.

@Test
public void testOpenClose() throws Exception {
    final CountDownLatch openLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    _connector.addBean(new Connection.Listener.Adapter() {

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

        @Override
        public void onClosed(Connection connection) {
            closeLatch.countDown();
        }
    });
    _connector.getResponses("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n");
    assertTrue(openLatch.await(5, TimeUnit.SECONDS));
    assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : Connection(org.eclipse.jetty.io.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 12 with Connection

use of org.eclipse.jetty.io.Connection in project metrics by dropwizard.

the class InstrumentedConnectionFactory method newConnection.

@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
    final Connection connection = connectionFactory.newConnection(connector, endPoint);
    connection.addListener(new Connection.Listener() {

        private Timer.Context context;

        @Override
        public void onOpened(Connection connection) {
            this.context = timer.time();
        }

        @Override
        public void onClosed(Connection connection) {
            context.stop();
        }
    });
    return connection;
}
Also used : Timer(com.codahale.metrics.Timer) Connection(org.eclipse.jetty.io.Connection)

Example 13 with Connection

use of org.eclipse.jetty.io.Connection in project dropwizard by dropwizard.

the class Jetty93InstrumentedConnectionFactory method newConnection.

@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
    final Connection connection = connectionFactory.newConnection(connector, endPoint);
    connection.addListener(new Connection.Listener() {

        private Timer.Context context;

        @Override
        public void onOpened(Connection connection) {
            this.context = timer.time();
        }

        @Override
        public void onClosed(Connection connection) {
            context.stop();
        }
    });
    return connection;
}
Also used : Timer(com.codahale.metrics.Timer) Connection(org.eclipse.jetty.io.Connection)

Example 14 with Connection

use of org.eclipse.jetty.io.Connection 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 15 with Connection

use of org.eclipse.jetty.io.Connection 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)

Aggregations

Connection (org.eclipse.jetty.io.Connection)18 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)7 Socket (java.net.Socket)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ServletException (javax.servlet.ServletException)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 AbstractConnection (org.eclipse.jetty.io.AbstractConnection)4 EndPoint (org.eclipse.jetty.io.EndPoint)3 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)3 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)3 Timer (com.codahale.metrics.Timer)2 InputStream (java.io.InputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 OutputStream (java.io.OutputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 AsyncContext (javax.servlet.AsyncContext)2 ReadListener (javax.servlet.ReadListener)2