Revision: 10389
Author:   gwt.mirror...@gmail.com
Date:     Thu Jun 23 20:53:02 2011
Log:      Fix the flaky RunAsyncFailureTest

Review by: zun...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=10389

Modified:
 /trunk/user/test/com/google/gwt/dev/jjs/test/RunAsyncFailureTest.java
/trunk/user/test/com/google/gwt/user/server/runasync/RunAsyncFailureServlet.java

=======================================
--- /trunk/user/test/com/google/gwt/dev/jjs/test/RunAsyncFailureTest.java Thu Jun 23 07:36:26 2011 +++ /trunk/user/test/com/google/gwt/dev/jjs/test/RunAsyncFailureTest.java Thu Jun 23 20:53:02 2011
@@ -47,13 +47,16 @@
       return token;
     }

-    public boolean onSuccessHelper() {
+    public boolean onSuccessHelper(String test) {
       int token = getToken();
       log("onSuccess: attempt = " + attempt + ", token = " + token);
       if (attempt == expectedSuccessfulAttempt) {
         return true;
       } else {
-        fail("Succeeded on attempt: " + attempt +
+ // We don't really care about the test string, but we need to use it
+        // somewhere so it doesn't get dead stripped out.  Each test passes
+        // in a unique string so it ends up in it's fragment.
+        fail(test + " - Succeeded on attempt: " + attempt +
" but should have succeeded on attempt: " + expectedSuccessfulAttempt);
       }
       return false;
@@ -111,7 +114,7 @@
       }

       public void onSuccess() {
-        if (onSuccessHelper()) { finishTest(); }
+        if (onSuccessHelper("DOWNLOAD_FAILURE_TEST_1")) { finishTest(); }
       }
     });
   }
@@ -130,7 +133,7 @@
       }

       public void onSuccess() {
-        if (onSuccessHelper()) { finishTest(); }
+        if (onSuccessHelper("DOWNLOAD_FAILURE_TEST_2")) { finishTest(); }
       }
     });
   }
@@ -149,7 +152,7 @@
       }

       public void onSuccess() {
-        if (onSuccessHelper()) { finishTest(); }
+        if (onSuccessHelper("DOWNLOAD_FAILURE_TEST_3")) { finishTest(); }
       }
     });
   }
@@ -164,7 +167,9 @@
         finishTest();
       }
       public void onSuccess() {
-        fail("Code should have failed to install!");
+        // Use the string "INSTALL_FAILURE_TEST" so we can identify this
+        // fragment on the server.  In the fail message is good enough.
+        fail("INSTALL_FAILURE_TEST - Code should have failed to install!");
       }
     });
   }
@@ -176,7 +181,7 @@
   public void testHttpFailureRetries() {
     delayTestFinish(RUNASYNC_TIMEOUT);
// Default is 3, but we set it explicitly, since other tests may run first
-    LoadingStrategyBase.MAX_RETRY_COUNT = 3;
+    LoadingStrategyBase.MAX_RETRY_COUNT = 2;
     // In RunAsyncFailureServlet, the 5th time is the charm, but the code
     // by default retries 3 times every time we call runAsync, so this
     // should succeed on the second runAsync call, which is attempt #1.
=======================================
--- /trunk/user/test/com/google/gwt/user/server/runasync/RunAsyncFailureServlet.java Thu Jun 23 07:36:26 2011 +++ /trunk/user/test/com/google/gwt/user/server/runasync/RunAsyncFailureServlet.java Thu Jun 23 20:53:02 2011
@@ -20,7 +20,6 @@
 import java.io.InputStream;
 import java.net.URL;
 import java.util.HashMap;
-import java.util.HashSet;

 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -32,10 +31,17 @@
  */
 public class RunAsyncFailureServlet extends HttpServlet {

-  private static final boolean DEBUG = true;
- private static final HashSet<String> downloadErrorFragments = new HashSet<String>(); - private static final HashSet<String> installErrorFragments = new HashSet<String>();
-
+  private static final boolean DEBUG = false;
+ private static final HashMap<String, RealContents> realContentsCache = new HashMap<String, RealContents>();
+
+  private static class RealContents {
+    public RealContents(int numBytes, byte[] bytes) {
+      this.numBytes = numBytes;
+      this.bytes = bytes;
+    }
+    public int numBytes;
+    public byte[] bytes;
+  }
   /**
    * Sequence of response codes to send back. SC_OK must be last.
    */
@@ -47,13 +53,6 @@
       HttpServletResponse.SC_OK,
   };

-  static {
-    downloadErrorFragments.add("2.cache.js");
-    downloadErrorFragments.add("3.cache.js");
-    downloadErrorFragments.add("5.cache.js");
-    installErrorFragments.add("4.cache.js");
-  }
-
   private static void debug(String message) {
     if (DEBUG) {
       System.out.println(message);
@@ -62,46 +61,28 @@

   HashMap<String, Integer> triesMap = new HashMap<String, Integer>();

-  private int sSerial = 0;
-
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
     String originalUri = req.getRequestURI();
-    debug("doGet: " + originalUri);
+    debug("doGet Original: " + originalUri);
     String uri = originalUri.replace("/runAsyncFailure", "");

     int response = getDesiredResponse(uri);
+    RealContents rc = this.getRealContents(req, uri);
+    String realContentsAsString = new String(rc.bytes);
+
     String fragment = uri.substring(uri.lastIndexOf('/') + 1);
-    if (!downloadErrorFragments.contains(fragment)
-        || response == HttpServletResponse.SC_OK) {
+      if (!realContentsAsString.contains("DOWNLOAD_FAILURE_TEST")
+          || response == HttpServletResponse.SC_OK) {
       int bytes = 0;
-      if (!installErrorFragments.contains(fragment)) {
-        // Delegate the actual data fetch to the main servlet
-        String host = req.getLocalName();
-        int port = req.getLocalPort();
-        String realUrl = "http://"; + host + ":" + port + uri;
-        debug("Fetching: " + realUrl);
-
-        try {
-          URL url = new URL(realUrl);
-          InputStream is = url.openStream();
-          OutputStream os = resp.getOutputStream();
-
-          byte[] data = new byte[8192];
-          int nbytes;
-          while ((nbytes = is.read(data)) != -1) {
-            os.write(data, 0, nbytes);
-            bytes += nbytes;
-          }
-          is.close();
-          os.close();
-        } catch (IOException e) {
-          debug("IOException fetching real data: " + e);
-          throw e;
-        }
-      }
-
+      if (!realContentsAsString.contains("INSTALL_FAILURE_TEST")) {
+        OutputStream os = resp.getOutputStream();
+        os.write(rc.bytes, 0, rc.numBytes);
+        bytes = rc.numBytes;
+        os.close();
+      }
+
       resp.setContentType("text/javascript");
       resp.setHeader("Cache-Control", "no-cache");
       resp.setContentLength(bytes);
@@ -110,7 +91,7 @@
       debug("doGet: served " + uri + " (" + bytes + " bytes)");
     } else {
       resp.setHeader("Cache-Control", "no-cache");
-      resp.sendError(response, "serial=" + getNextSerial());
+      resp.sendError(response);

       debug("doGet: sent error " + response + " for " + uri);
     }
@@ -124,7 +105,31 @@
     return responses[tries % responses.length];
   }

-  private synchronized int getNextSerial() {
-    return sSerial++;
+ private RealContents getRealContents(HttpServletRequest req, String uri) throws IOException {
+    if (realContentsCache.containsKey(uri)) {
+      return realContentsCache.get(uri);
+    }
+
+    // Delegate the actual data fetch to the main servlet
+    String host = req.getLocalName();
+    int port = req.getLocalPort();
+    String realUrl = "http://"; + host + ":" + port + uri;
+    debug("Fetching: " + realUrl);
+    int bytes = 0;
+    byte[] data = new byte[32768];
+    try {
+      URL url = new URL(realUrl);
+      InputStream is = url.openStream();
+
+      bytes = is.read(data);
+      is.close();
+    } catch (IOException e) {
+      debug("IOException fetching real data: " + e);
+      throw e;
+    }
+
+    RealContents rc = new RealContents(bytes, data);
+    realContentsCache.put(uri, rc);
+    return rc;
   }
 }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to