From 108821aacbd363d8eb1cf5a9eeed1476dbec49e9 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 13 Aug 2015 18:44:25 +1000 Subject: [PATCH] 474634 - AsyncListener.onError() handling. Fixed more tests in jetty-servlets --- .../servlet/AsyncContextListenersTest.java | 33 ++++++++++--------- .../jetty/servlet/AsyncIOServletTest.java | 6 ++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextListenersTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextListenersTest.java index 11485cacae6..ce254a7ada2 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextListenersTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextListenersTest.java @@ -24,7 +24,10 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import javax.servlet.AsyncContext; import javax.servlet.AsyncEvent; @@ -68,7 +71,7 @@ public class AsyncContextListenersTest @Test public void testListenerClearedOnSecondRequest() throws Exception { - final AtomicInteger completes = new AtomicInteger(); + final AtomicReference completes = new AtomicReference<>(new CountDownLatch(1)); String path = "/path"; prepare(path, new HttpServlet() { @@ -86,7 +89,7 @@ public class AsyncContextListenersTest @Override public void onComplete(AsyncEvent event) throws IOException { - completes.incrementAndGet(); + completes.get().countDown(); } @Override @@ -118,23 +121,23 @@ public class AsyncContextListenersTest SimpleHttpParser parser = new SimpleHttpParser(); SimpleHttpResponse response = parser.readResponse(reader); Assert.assertEquals("200", response.getCode()); - Assert.assertEquals(1, completes.get()); + completes.get().await(10,TimeUnit.SECONDS); // Send a second request - completes.set(0); + completes.set(new CountDownLatch(1)); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = parser.readResponse(reader); Assert.assertEquals("200", response.getCode()); - Assert.assertEquals(1, completes.get()); + completes.get().await(10,TimeUnit.SECONDS); } } @Test public void testListenerAddedFromListener() throws Exception { - final AtomicInteger completes = new AtomicInteger(); + final AtomicReference completes = new AtomicReference<>(new CountDownLatch(1)); String path = "/path"; prepare(path, new HttpServlet() { @@ -157,7 +160,7 @@ public class AsyncContextListenersTest @Override public void onComplete(AsyncEvent event) throws IOException { - completes.incrementAndGet(); + completes.get().countDown(); } @Override @@ -189,22 +192,22 @@ public class AsyncContextListenersTest SimpleHttpParser parser = new SimpleHttpParser(); SimpleHttpResponse response = parser.readResponse(reader); Assert.assertEquals("200", response.getCode()); - Assert.assertEquals(1, completes.get()); + completes.get().await(10,TimeUnit.SECONDS); // Send a second request - completes.set(0); + completes.set(new CountDownLatch(1)); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = parser.readResponse(reader); Assert.assertEquals("200", response.getCode()); - Assert.assertEquals(1, completes.get()); + completes.get().await(10,TimeUnit.SECONDS); } } @Test public void testAsyncDispatchAsyncCompletePreservesListener() throws Exception { - final AtomicInteger completes = new AtomicInteger(); + final AtomicReference completes = new AtomicReference<>(new CountDownLatch(1)); final String path = "/path"; prepare(path + "/*", new HttpServlet() { @@ -226,7 +229,7 @@ public class AsyncContextListenersTest @Override public void onComplete(AsyncEvent event) throws IOException { - completes.incrementAndGet(); + completes.get().countDown(); } @Override @@ -267,16 +270,16 @@ public class AsyncContextListenersTest SimpleHttpParser parser = new SimpleHttpParser(); SimpleHttpResponse response = parser.readResponse(reader); Assert.assertEquals("200", response.getCode()); - Assert.assertEquals(1, completes.get()); + completes.get().await(10,TimeUnit.SECONDS); // Send a second request - completes.set(0); + completes.set(new CountDownLatch(1)); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = parser.readResponse(reader); Assert.assertEquals("200", response.getCode()); - Assert.assertEquals(1, completes.get()); + completes.get().await(10,TimeUnit.SECONDS); } } } diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java index d99055ff40f..057278b08eb 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncIOServletTest.java @@ -245,6 +245,12 @@ public class AsyncIOServletTest @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if (request.getDispatcherType()==DispatcherType.ERROR) + { + response.flushBuffer(); + return; + } + final AsyncContext asyncContext = request.startAsync(request, response); request.getInputStream().setReadListener(new ReadListener() {