From 980a780a1fffb478fa5f02ca8c9388e459c83514 Mon Sep 17 00:00:00 2001 From: markt Date: Sat, 31 Jul 2010 09:58:17 +0000 Subject: [PATCH] Fix possible threading issue in unit tests. Adding some Thread.sleep() calls in the right place can cause the tests to fail. This is probably one of the causes of the current Gump failures. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@981027 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/core/TestAsyncContextImpl.java | 39 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/test/org/apache/catalina/core/TestAsyncContextImpl.java b/test/org/apache/catalina/core/TestAsyncContextImpl.java index 34e12f3bf..cf049faee 100644 --- a/test/org/apache/catalina/core/TestAsyncContextImpl.java +++ b/test/org/apache/catalina/core/TestAsyncContextImpl.java @@ -53,6 +53,12 @@ public class TestAsyncContextImpl extends TomcatBaseTest { ByteChunk bc = getUrl("http://localhost:" + getPort() + "/"); assertEquals("OK", bc.toString()); + // Give the async thread a chance to finish (but not too long) + int counter = 0; + while (!servlet.isDone() && counter < 10) { + Thread.sleep(1000); + counter++; + } assertEquals("1false2true3true4true5false", servlet.getResult()); } @@ -77,6 +83,13 @@ public class TestAsyncContextImpl extends TomcatBaseTest { ByteChunk bc = getUrl("http://localhost:" + getPort() + "/"); assertEquals("OK", bc.toString()); + // Give the async thread a chance to finish (but not too long) + int counter = 0; + while (!servlet.isDone() && counter < 10) { + Thread.sleep(1000); + counter++; + } + assertEquals("1false2true3true4true5false", servlet.getResult()); } @@ -134,21 +147,31 @@ public class TestAsyncContextImpl extends TomcatBaseTest { assertEquals("OK", bc.toString()); } + /* + * NOTE: This servlet is only intended to be used in single-threaded tests. + */ private static class Bug49528Servlet extends HttpServlet { private static final long serialVersionUID = 1L; - private StringBuilder result = new StringBuilder(); + private volatile boolean done = false; + + private StringBuilder result; public String getResult() { return result.toString(); } + public boolean isDone() { + return done; + } + @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { + result = new StringBuilder(); result.append('1'); result.append(req.isAsyncStarted()); req.startAsync(); @@ -169,6 +192,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest { req.getAsyncContext().complete(); result.append('5'); result.append(req.isAsyncStarted()); + done = true; } catch (InterruptedException e) { result.append(e); } catch (IOException e) { @@ -182,21 +206,31 @@ public class TestAsyncContextImpl extends TomcatBaseTest { } } + /* + * NOTE: This servlet is only intended to be used in single-threaded tests. + */ private static class Bug49567Servlet extends HttpServlet { private static final long serialVersionUID = 1L; - private StringBuilder result = new StringBuilder(); + private volatile boolean done = false; + + private StringBuilder result; public String getResult() { return result.toString(); } + public boolean isDone() { + return done; + } + @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { + result = new StringBuilder(); result.append('1'); result.append(req.isAsyncStarted()); req.startAsync(); @@ -220,6 +254,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest { req.getAsyncContext().complete(); result.append('5'); result.append(req.isAsyncStarted()); + done = true; } catch (InterruptedException e) { result.append(e); } catch (IOException e) { -- 2.11.0