use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.
the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingSmallContent.
@Test
public void testProxyRequestFailureInTheMiddleOfProxyingSmallContent() throws Exception {
final CountDownLatch chunk1Latch = new CountDownLatch(1);
final int chunk1 = 'q';
final int chunk2 = 'w';
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
response.flushBuffer();
// Wait for the client to receive this chunk.
await(chunk1Latch, 5000);
// Send second chunk, must not be received by proxy.
output.write(chunk2);
}
private boolean await(CountDownLatch latch, long ms) throws IOException {
try {
return latch.await(ms, TimeUnit.MILLISECONDS);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
final long proxyTimeout = 1000;
Map<String, String> proxyParams = new HashMap<>();
proxyParams.put("timeout", String.valueOf(proxyTimeout));
startProxy(proxyParams);
startClient();
InputStreamResponseListener listener = new InputStreamResponseListener();
int port = serverConnector.getLocalPort();
client.newRequest("localhost", port).send(listener);
// Make the proxy request fail; given the small content, the
// proxy-to-client response is not committed yet so it will be reset.
TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(504, response.getStatus());
// Make sure there is no content, as the proxy-to-client response has been reset.
InputStream input = listener.getInputStream();
Assert.assertEquals(-1, input.read());
chunk1Latch.countDown();
// Result succeeds because a 504 is a valid HTTP response.
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertTrue(result.isSucceeded());
// Make sure the proxy does not receive chunk2.
Assert.assertEquals(-1, input.read());
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.
the class HttpDestinationOverHTTPTest method test_IdleConnection_IdleTimeout.
@Test
public void test_IdleConnection_IdleTimeout() throws Exception {
long idleTimeout = 1000;
client.setIdleTimeout(idleTimeout);
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
destination.start();
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Connection connection1 = connectionPool.acquire();
if (connection1 == null) {
// There are no queued requests, so the newly created connection will be idle
long start = System.nanoTime();
while (connection1 == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
TimeUnit.MILLISECONDS.sleep(50);
connection1 = connectionPool.getIdleConnections().peek();
}
Assert.assertNotNull(connection1);
TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
connection1 = connectionPool.getIdleConnections().poll();
Assert.assertNull(connection1);
}
}
use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.
the class HttpDestinationOverHTTPTest method test_Acquire_Process_Release_Acquire_ReturnsSameConnection.
@Test
public void test_Acquire_Process_Release_Acquire_ReturnsSameConnection() throws Exception {
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
destination.start();
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
HttpConnectionOverHTTP connection1 = (HttpConnectionOverHTTP) connectionPool.acquire();
long start = System.nanoTime();
while (connection1 == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
TimeUnit.MILLISECONDS.sleep(50);
connection1 = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
}
Assert.assertNotNull(connection1);
// Acquire the connection to make it active
Assert.assertSame(connection1, connectionPool.acquire());
destination.process(connection1);
destination.release(connection1);
Connection connection2 = connectionPool.acquire();
Assert.assertSame(connection1, connection2);
}
use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.
the class HttpDestinationOverHTTPTest method test_FirstAcquire_WithEmptyQueue.
@Test
public void test_FirstAcquire_WithEmptyQueue() throws Exception {
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
destination.start();
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Connection connection = connectionPool.acquire();
if (connection == null) {
// There are no queued requests, so the newly created connection will be idle
connection = timedPoll(connectionPool.getIdleConnections(), 5, TimeUnit.SECONDS);
}
Assert.assertNotNull(connection);
}
use of org.eclipse.jetty.client.DuplexConnectionPool in project jetty.project by eclipse.
the class HttpDestinationOverHTTPTest method test_SecondAcquire_ConcurrentWithFirstAcquire_WithEmptyQueue_CreatesTwoConnections.
@Test
public void test_SecondAcquire_ConcurrentWithFirstAcquire_WithEmptyQueue_CreatesTwoConnections() throws Exception {
final CountDownLatch idleLatch = new CountDownLatch(1);
final CountDownLatch latch = new CountDownLatch(1);
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort())) {
@Override
protected ConnectionPool newConnectionPool(HttpClient client) {
return new DuplexConnectionPool(this, client.getMaxConnectionsPerDestination(), this) {
@Override
protected void onCreated(Connection connection) {
try {
idleLatch.countDown();
latch.await(5, TimeUnit.SECONDS);
super.onCreated(connection);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
};
}
};
destination.start();
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Connection connection1 = connectionPool.acquire();
// Make sure we entered idleCreated().
Assert.assertTrue(idleLatch.await(5, TimeUnit.SECONDS));
// There are no available existing connections, so acquire()
// returns null because we delayed idleCreated() above
Assert.assertNull(connection1);
// Second attempt also returns null because we delayed idleCreated() above.
Connection connection2 = connectionPool.acquire();
Assert.assertNull(connection2);
latch.countDown();
// There must be 2 idle connections.
Queue<Connection> idleConnections = connectionPool.getIdleConnections();
Connection connection = timedPoll(idleConnections, 5, TimeUnit.SECONDS);
Assert.assertNotNull(connection);
connection = timedPoll(idleConnections, 5, TimeUnit.SECONDS);
Assert.assertNotNull(connection);
}
Aggregations