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));
}
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;
}
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;
}
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());
}
}
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());
}
Aggregations