use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortOnQueued.
@Test
public void testAbortOnQueued() throws Exception {
start(new EmptyServerHandler());
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean begin = new AtomicBoolean();
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).listener(new Request.Listener.Adapter() {
@Override
public void onQueued(Request request) {
aborted.set(request.abort(cause));
latch.countDown();
}
@Override
public void onBegin(Request request) {
begin.set(true);
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
Assert.assertFalse(begin.get());
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortOnBegin.
@Test
public void testAbortOnBegin() throws Exception {
start(new EmptyServerHandler());
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch committed = new CountDownLatch(1);
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).listener(new Request.Listener.Adapter() {
@Override
public void onBegin(Request request) {
aborted.set(request.abort(cause));
latch.countDown();
}
@Override
public void onCommit(Request request) {
committed.countDown();
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
Assert.assertFalse(committed.await(1, TimeUnit.SECONDS));
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortOnCommitWithContent.
@Test
public void testAbortOnCommitWithContent() throws Exception {
final AtomicReference<IOException> failure = new AtomicReference<>();
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
if (request.getDispatcherType() != DispatcherType.ERROR)
IO.copy(request.getInputStream(), response.getOutputStream());
} catch (IOException x) {
failure.set(x);
throw x;
}
}
});
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestCommit(request -> {
aborted.set(request.abort(cause));
latch.countDown();
}).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {
@Override
public long getLength() {
return -1;
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortLongPoll.
@Test
public void testAbortLongPoll() throws Exception {
final long delay = 1000;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
if (request.getDispatcherType() != DispatcherType.ERROR)
TimeUnit.MILLISECONDS.sleep(2 * delay);
} catch (InterruptedException x) {
throw new ServletException(x);
}
}
});
final Request request = client.newRequest("localhost", connector.getLocalPort()).timeout(3 * delay, TimeUnit.MILLISECONDS).scheme(scheme);
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
new Thread(() -> {
try {
TimeUnit.MILLISECONDS.sleep(delay);
aborted.set(request.abort(cause));
latch.countDown();
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
}).start();
try {
request.send();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.
the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingBigContent.
@Test
public void testProxyRequestFailureInTheMiddleOfProxyingBigContent() throws Exception {
int outputBufferSize = 1024;
final CountDownLatch chunk1Latch = new CountDownLatch(1);
final byte[] chunk1 = new byte[outputBufferSize];
new Random().nextBytes(chunk1);
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));
proxyParams.put("outputBufferSize", String.valueOf(outputBufferSize));
startProxy(proxyParams);
startClient();
InputStreamResponseListener listener = new InputStreamResponseListener();
int port = serverConnector.getLocalPort();
client.newRequest("localhost", port).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
InputStream input = listener.getInputStream();
for (int i = 0; i < chunk1.length; ++i) Assert.assertEquals(chunk1[i] & 0xFF, input.read());
TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
chunk1Latch.countDown();
try {
// Make sure the proxy does not receive chunk2.
input.read();
Assert.fail();
} catch (EOFException x) {
// Expected
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Aggregations