Search in sources :

Example 36 with AsyncContext

use of javax.servlet.AsyncContext in project jetty.project by eclipse.

the class HttpClientStreamTest method testUploadWithConcurrentServerCloseClosesStream.

@Test
public void testUploadWithConcurrentServerCloseClosesStream() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            serverLatch.countDown();
        }
    });
    final AtomicBoolean commit = new AtomicBoolean();
    final CountDownLatch closeLatch = new CountDownLatch(1);
    InputStream stream = new InputStream() {

        @Override
        public int read() throws IOException {
            if (commit.get()) {
                try {
                    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
                    connector.stop();
                    return 0;
                } catch (Throwable x) {
                    throw new IOException(x);
                }
            } else {
                return connector.isStopped() ? -1 : 0;
            }
        }

        @Override
        public void close() throws IOException {
            super.close();
            closeLatch.countDown();
        }
    };
    InputStreamContentProvider provider = new InputStreamContentProvider(stream, 1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(provider).onRequestCommit(request -> commit.set(true)).send(result -> {
        Assert.assertTrue(result.isFailed());
        completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) Request(org.eclipse.jetty.server.Request) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) BufferUtil(org.eclipse.jetty.util.BufferUtil) Result(org.eclipse.jetty.client.api.Result) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 37 with AsyncContext

use of javax.servlet.AsyncContext in project jetty.project by eclipse.

the class HttpClientIdleTimeoutTest method testClientIdleTimeout.

@Test
public void testClientIdleTimeout() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals("/timeout")) {
                AsyncContext asyncContext = request.startAsync();
                asyncContext.setTimeout(0);
            }
        }
    });
    client.stop();
    client.setIdleTimeout(idleTimeout);
    client.start();
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).path("/timeout").send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    // Verify that after the timeout we can make another request.
    ContentResponse response = client.newRequest(newURI()).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 38 with AsyncContext

use of javax.servlet.AsyncContext in project jetty.project by eclipse.

the class HttpClientIdleTimeoutTest method testRequestIdleTimeout.

@Test
public void testRequestIdleTimeout() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals("/timeout")) {
                AsyncContext asyncContext = request.startAsync();
                asyncContext.setTimeout(0);
            }
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).path("/timeout").idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    // Verify that after the timeout we can make another request.
    ContentResponse response = client.newRequest(newURI()).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 39 with AsyncContext

use of javax.servlet.AsyncContext in project atmosphere by Atmosphere.

the class AtmosphereRequestImpl method startAsync.

@Override
public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
    AsyncContext ac;
    if (AtmosphereResource.TRANSPORT.WEBSOCKET == resource().transport()) {
        noopsAsyncContextStarted = true;
        ac = new NoOpsAsyncContext(request, response);
    } else {
        ac = b.request.startAsync(request, response);
    }
    return isCompletionAware() ? new CompletionAwareAsyncContext(ac, (CompletionAware) resource().getResponse()) : ac;
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Example 40 with AsyncContext

use of javax.servlet.AsyncContext in project atmosphere by Atmosphere.

the class AtmosphereResourceTest method verifyTestCompletionAwareForGetAsync.

private void verifyTestCompletionAwareForGetAsync(boolean aware) throws IOException {
    if (aware) {
        framework.addInitParameter(ApplicationConfig.RESPONSE_COMPLETION_AWARE, "true");
    }
    AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
    AtmosphereResponseImpl response = mock(AtmosphereResponseImpl.class);
    AtmosphereResourceImpl res = new AtmosphereResourceImpl();
    res.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, null, null);
    res.transport(AtmosphereResource.TRANSPORT.WEBSOCKET);
    request.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, res);
    AsyncContext ac = request.getAsyncContext();
    verify(response, times(0)).onComplete();
    ac.complete();
    verify(response, times(aware ? 1 : 0)).onComplete();
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Aggregations

AsyncContext (javax.servlet.AsyncContext)194 IOException (java.io.IOException)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)76 ServletException (javax.servlet.ServletException)61 Test (org.junit.Test)57 CountDownLatch (java.util.concurrent.CountDownLatch)38 AsyncEvent (javax.servlet.AsyncEvent)35 AsyncListener (javax.servlet.AsyncListener)35 HttpServlet (javax.servlet.http.HttpServlet)34 ServletOutputStream (javax.servlet.ServletOutputStream)27 InterruptedIOException (java.io.InterruptedIOException)24 ReadListener (javax.servlet.ReadListener)20 ServletInputStream (javax.servlet.ServletInputStream)20 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 Request (org.eclipse.jetty.server.Request)16 WriteListener (javax.servlet.WriteListener)15 PrintWriter (java.io.PrintWriter)14 UncheckedIOException (java.io.UncheckedIOException)14 ByteBuffer (java.nio.ByteBuffer)13