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