Author: markt
Date: Sat Jul 31 09:58:17 2010
New Revision: 981027

URL: http://svn.apache.org/viewvc?rev=981027&view=rev
Log:
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.

Modified:
    tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java

Modified: tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java?rev=981027&r1=981026&r2=981027&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java 
(original)
+++ tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java Sat 
Jul 31 09:58:17 2010
@@ -53,6 +53,12 @@ public class TestAsyncContextImpl extend
         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 extend
         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 extend
         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 extend
                         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 extend
         }
     }
 
+    /*
+     * 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 extend
                                 req.getAsyncContext().complete();
                                 result.append('5');
                                 result.append(req.isAsyncStarted());
+                                done = true;
                             } catch (InterruptedException e) {
                                 result.append(e);
                             } catch (IOException e) {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to