This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 61819e0a57 Reduce test runtime
61819e0a57 is described below

commit 61819e0a57efd8948b39ba60d38769e8096a064c
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Oct 20 18:54:44 2026 +0100

    Reduce test runtime
    
    Co-authored by: Codex / GPT-6.1-Sol-High
---
 .../apache/catalina/core/TestAsyncContextImpl.java | 59 ++++++++++++++--------
 1 file changed, 39 insertions(+), 20 deletions(-)

diff --git a/test/org/apache/catalina/core/TestAsyncContextImpl.java 
b/test/org/apache/catalina/core/TestAsyncContextImpl.java
index afa1a704f8..bc8e7a3189 100644
--- a/test/org/apache/catalina/core/TestAsyncContextImpl.java
+++ b/test/org/apache/catalina/core/TestAsyncContextImpl.java
@@ -27,6 +27,7 @@ import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.logging.Level;
 import java.util.logging.LogManager;
@@ -64,6 +65,7 @@ import org.apache.catalina.startup.SimpleHttpClient;
 import org.apache.catalina.startup.Tomcat;
 import org.apache.catalina.startup.TomcatBaseTest;
 import org.apache.catalina.valves.TesterAccessLogValve;
+import org.apache.catalina.valves.ValveBase;
 import org.apache.tomcat.unittest.TesterContext;
 import org.apache.tomcat.util.buf.ByteChunk;
 import org.apache.tomcat.util.buf.EncodedSolidusHandling;
@@ -1003,6 +1005,24 @@ public class TestAsyncContextImpl extends TomcatBaseTest 
{
         // Setup Tomcat instance
         Tomcat tomcat = getTomcatInstance();
 
+        CountDownLatch initialRequestReturned = new CountDownLatch(1);
+        // Wait for the initial request to leave the host's ErrorReportValve 
before triggering the async error.
+        tomcat.getEngine().getPipeline().addValve(new ValveBase(true) {
+            private final AtomicBoolean firstInvocation = new 
AtomicBoolean(true);
+
+            @Override
+            public void invoke(Request request, Response response) throws 
IOException, ServletException {
+                boolean first = firstInvocation.getAndSet(false);
+                try {
+                    getNext().invoke(request, response);
+                } finally {
+                    if (first) {
+                        initialRequestReturned.countDown();
+                    }
+                }
+            }
+        });
+
         // No file system docBase required
         Context ctx = getProgrammaticRootContext();
 
@@ -1011,7 +1031,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest {
         wrapper.setAsyncSupported(true);
         ctx.addServletMapping("/stage1", "dispatch");
 
-        ErrorServlet error = new ErrorServlet();
+        ErrorServlet error = new ErrorServlet(initialRequestReturned);
         Tomcat.addServlet(ctx, "error", error);
         ctx.addServletMapping("/stage2", "error");
 
@@ -1061,15 +1081,26 @@ public class TestAsyncContextImpl extends 
TomcatBaseTest {
 
         private static final long serialVersionUID = 1L;
 
+        private final transient CountDownLatch initialRequestReturned;
+
+        ErrorServlet(CountDownLatch initialRequestReturned) {
+            this.initialRequestReturned = initialRequestReturned;
+        }
+
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {
             track("ErrorServletGet-");
-            try {
-                // Give the original thread a chance to exit the
-                // ErrorReportValve before we throw this exception
-                Thread.sleep(500);
-            } catch (InterruptedException e) {
-                throw new ServletException(e);
+            if (initialRequestReturned != null) {
+                try {
+                    if (!initialRequestReturned.await(10, TimeUnit.SECONDS)) {
+                        track("InitialRequestDidNotReturn-");
+                        throw new ServletException("Initial request did not 
return through the pipeline");
+                    }
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    track("InitialRequestWaitInterrupted-");
+                    throw new ServletException(e);
+                }
             }
             throw new ServletException("Oops.");
         }
@@ -1211,7 +1242,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest {
         // No file system docBase required
         Context ctx = getProgrammaticRootContext();
 
-        ErrorServlet error = new ErrorServlet();
+        ErrorServlet error = new ErrorServlet(null);
         Tomcat.addServlet(ctx, "error", error);
         ctx.addServletMapping("/error", "error");
 
@@ -1229,10 +1260,6 @@ public class TestAsyncContextImpl extends TomcatBaseTest 
{
 
         Assert.assertEquals(500, rc);
 
-        // Without this test may complete before access log has a chance to log
-        // the request
-        Thread.sleep(REQUEST_TIME);
-
         // Check the access log
         alv.validateAccessLog(1, 500, 0, REQUEST_TIME);
     }
@@ -1264,10 +1291,6 @@ public class TestAsyncContextImpl extends TomcatBaseTest 
{
 
         Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
 
-        // Without this test may complete before access log has a chance to log
-        // the request
-        Thread.sleep(REQUEST_TIME);
-
         // Check the access log
         alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, 0, 
REQUEST_TIME);
 
@@ -1363,10 +1386,6 @@ public class TestAsyncContextImpl extends TomcatBaseTest 
{
             Assert.assertTrue(responseBody, 
responseBody.contains(AsyncErrorServlet.ERROR_MESSAGE));
         }
 
-        // Without this test may complete before access log has a chance to log
-        // the request
-        Thread.sleep(REQUEST_TIME);
-
         // Check the access log
         alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, 0, 
REQUEST_TIME);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to