Index: test/org/apache/catalina/core/TestAsyncContextImpl.java =================================================================== --- test/org/apache/catalina/core/TestAsyncContextImpl.java (revision 1740624) +++ test/org/apache/catalina/core/TestAsyncContextImpl.java (working copy) @@ -1821,7 +1821,104 @@ } } + //Point 2. @Test + public void testBug59219a() throws Exception{ + resetTracker(); + Tomcat tomcat = getTomcatInstance(); + + Context ctx = tomcat.addContext("", null); + Wrapper wrapper = tomcat.addServlet("", "async", new Bug59219ServletA()); + wrapper.setAsyncSupported(true); + + ctx.addServletMapping("/async", "async"); + + tomcat.start(); + + getUrl("http://localhost:" + getPort() + "/async"); + String expected = "onError-onComplete-"; + + //Just let's give it 5 seconds :) + int count = 0; + while(!expected.equals(getTrack()) && count < 100) { + Thread.sleep(50); + count++; + } + + Assert.assertEquals(expected, getTrack()); + } + + private static class Bug59219ServletA extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + AsyncContext ctx = req.startAsync(); + ctx.addListener(new TrackingListener(true, false, "/async")); + + throw new ServletException("container be nice and process onError."); + } + } + + + // Point 3. + @Test + public void testBug59219b() throws Exception { + resetTracker(); + Tomcat tomcat = getTomcatInstance(); + + Context ctx = tomcat.addContext("", null); + Wrapper wrapper = tomcat.addServlet("", "async", new Bug59219ServletB()); + wrapper.setAsyncSupported(true); + + ctx.addServletMapping("/async", "async"); + + tomcat.start(); + + getUrl("http://localhost:" + getPort() + "/async?loop=2"); + String expected = "onStartAsync-onStartAsync-onError-onComplete-"; + + //Just let's give it 5 seconds :) + int count = 0; + while(!expected.equals(getTrack()) && count < 100) { + Thread.sleep(50); + count++; + } + + Assert.assertEquals(expected, getTrack()); + } + + private static class Bug59219ServletB extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + AsyncContext ctx = req.startAsync(); + ctx.addListener(new TrackingListener(true, false, "/async")); + + String loop = req.getParameter("loop"); + Integer iloop = (Integer) req.getAttribute("iloop"); + + if (iloop == null) { + iloop = 0; + if (loop != null) { + iloop = Integer.parseInt(loop); + } + } + + if (iloop > 0) { + iloop--; + req.setAttribute("iloop", iloop); + ctx.dispatch(); + } else + throw new ServletException("container be nice and process onError."); + } + } + + + @Test public void testForbiddenDispatching() throws Exception { resetTracker(); // Setup Tomcat instance