Remove ThreadLocal nonsense.
Patch by: bobv
Review by: rjrjr

Review at http://gwt-code-reviews.appspot.com/892801


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@8807 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/test/com/google/gwt/requestfactory/client/EditorTest.java b/user/test/com/google/gwt/requestfactory/client/EditorTest.java
index 1e3581d..1631bd8 100644
--- a/user/test/com/google/gwt/requestfactory/client/EditorTest.java
+++ b/user/test/com/google/gwt/requestfactory/client/EditorTest.java
@@ -17,7 +17,7 @@
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.Scheduler;
-import com.google.gwt.core.client.Scheduler.ScheduledCommand;
+import com.google.gwt.core.client.Scheduler.RepeatingCommand;
 import com.google.gwt.editor.client.Editor;
 import com.google.gwt.editor.client.EditorDelegate;
 import com.google.gwt.editor.client.EditorError;
@@ -164,15 +164,19 @@
             request.fire(new Receiver<SimpleFooProxy>() {
               public void onSuccess(SimpleFooProxy response,
                   Set<SyncResult> syncResults) {
-                // EventBus notifications occurr after the onSuccess()
-                Scheduler.get().scheduleDeferred(new ScheduledCommand() {
-                  public void execute() {
-                    assertEquals("updated", editor.userName.getValue());
-                    assertEquals("newBar",
-                        editor.barEditor().userName.getValue());
-                    finishTestAndReset();
+                // EventBus notifications occur after the onSuccess()
+                Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
+                  public boolean execute() {
+                    if ("updated".equals(editor.userName.getValue())) {
+                      assertEquals("updated", editor.userName.getValue());
+                      assertEquals("newBar",
+                          editor.barEditor().userName.getValue());
+                      finishTestAndReset();
+                      return false;
+                    }
+                    return true;
                   }
-                });
+                }, 50);
               }
             });
           }
diff --git a/user/test/com/google/gwt/requestfactory/server/SimpleBar.java b/user/test/com/google/gwt/requestfactory/server/SimpleBar.java
index e8edad5..6794388 100644
--- a/user/test/com/google/gwt/requestfactory/server/SimpleBar.java
+++ b/user/test/com/google/gwt/requestfactory/server/SimpleBar.java
@@ -26,26 +26,10 @@
  * Domain object for SimpleFooRequest.
  */
 public class SimpleBar {
-
   /**
-   * This is an ugly hack.
+   * DO NOT USE THIS UGLY HACK DIRECTLY! Call {@link #get} instead.
    */
-  static ThreadLocal<SimpleBar> singleton = new ThreadLocal<SimpleBar>() {
-    @Override
-    protected SimpleBar initialValue() {
-      SimpleBar value = null;
-      HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
-      // May be in a JRE test case
-      if (req != null) {
-        value = (SimpleBar) req.getSession().getAttribute(
-            SimpleBar.class.getCanonicalName());
-      }
-      if (value == null) {
-        value = reset();
-      }
-      return value;
-    }
-  };
+  private static SimpleBar jreTestSingleton = new SimpleBar();
 
   private static Long nextId = 1L;
 
@@ -54,7 +38,7 @@
   }
 
   public static List<SimpleBar> findAll() {
-    return Collections.singletonList(singleton.get());
+    return Collections.singletonList(get());
   }
 
   public static SimpleBar findSimpleBar(Long id) {
@@ -62,19 +46,40 @@
   }
 
   public static SimpleBar findSimpleBarById(Long id) {
-    singleton.get().setId(id);
-    return singleton.get();
+    get().setId(id);
+    return get();
+  }
+
+  public static synchronized SimpleBar get() {
+    HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
+    if (req == null) {
+      // May be in a JRE test case, use the the singleton
+      return jreTestSingleton;
+    } else {
+      /*
+       * This will not behave entirely correctly unless we have a servlet filter
+       * that doesn't allow any requests to be processed unless they're
+       * associated with an existing session.
+       */
+      SimpleBar value = (SimpleBar) req.getSession().getAttribute(
+          SimpleBar.class.getCanonicalName());
+      if (value == null) {
+        value = reset();
+      }
+      return value;
+    }
   }
 
   public static SimpleBar getSingleton() {
-    return singleton.get();
+    return get();
   }
 
-  public static SimpleBar reset() {
+  public static synchronized SimpleBar reset() {
     SimpleBar instance = new SimpleBar();
-    singleton.set(instance);
     HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
-    if (req != null) {
+    if (req == null) {
+      jreTestSingleton = instance;
+    } else {
       req.getSession().setAttribute(SimpleBar.class.getCanonicalName(),
           instance);
     }
@@ -107,7 +112,7 @@
 
   public void persist() {
     setId(nextId++);
-    singleton.get().setUserName(userName);
+    get().setUserName(userName);
   }
 
   public SimpleBar persistAndReturnSelf() {
diff --git a/user/test/com/google/gwt/requestfactory/server/SimpleFoo.java b/user/test/com/google/gwt/requestfactory/server/SimpleFoo.java
index d06ac09..4b25282 100644
--- a/user/test/com/google/gwt/requestfactory/server/SimpleFoo.java
+++ b/user/test/com/google/gwt/requestfactory/server/SimpleFoo.java
@@ -31,26 +31,10 @@
  * Domain object for SimpleFooRequest.
  */
 public class SimpleFoo {
-
   /**
-   * This is an ugly hack.
+   * DO NOT USE THIS UGLY HACK DIRECTLY! Call {@link #get} instead.
    */
-  static ThreadLocal<SimpleFoo> singleton = new ThreadLocal<SimpleFoo>() {
-    @Override
-    protected SimpleFoo initialValue() {
-      SimpleFoo value = null;
-      HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
-      // May be in a JRE test case
-      if (req != null) {
-        value = (SimpleFoo) req.getSession().getAttribute(
-            SimpleFoo.class.getCanonicalName());
-      }
-      if (value == null) {
-        value = reset();
-      }
-      return value;
-    }
-  };
+  private static SimpleFoo jreTestSingleton = new SimpleFoo();
 
   private static Long nextId = 1L;
 
@@ -59,7 +43,7 @@
   }
 
   public static List<SimpleFoo> findAll() {
-    return Collections.singletonList(singleton.get());
+    return Collections.singletonList(get());
   }
 
   public static SimpleFoo findSimpleFoo(Long id) {
@@ -67,19 +51,40 @@
   }
 
   public static SimpleFoo findSimpleFooById(Long id) {
-    singleton.get().setId(id);
-    return singleton.get();
+    get().setId(id);
+    return get();
+  }
+
+  public static synchronized SimpleFoo get() {
+    HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
+    if (req == null) {
+      // May be in a JRE test case, use the singleton
+      return jreTestSingleton;
+    } else {
+      /*
+       * This will not behave entirely correctly unless we have a servlet filter
+       * that doesn't allow any requests to be processed unless they're
+       * associated with an existing session.
+       */
+      SimpleFoo value = (SimpleFoo) req.getSession().getAttribute(
+          SimpleFoo.class.getCanonicalName());
+      if (value == null) {
+        value = reset();
+      }
+      return value;
+    }
   }
 
   public static SimpleFoo getSingleton() {
-    return singleton.get();
+    return get();
   }
 
-  public static SimpleFoo reset() {
+  public static synchronized SimpleFoo reset() {
     SimpleFoo instance = new SimpleFoo();
-    singleton.set(instance);
     HttpServletRequest req = RequestFactoryServlet.getThreadLocalRequest();
-    if (req != null) {
+    if (req == null) {
+      jreTestSingleton = instance;
+    } else {
       req.getSession().setAttribute(SimpleFoo.class.getCanonicalName(),
           instance);
     }
@@ -141,7 +146,7 @@
   }
 
   public Long countSimpleFooWithUserNameSideEffect() {
-    singleton.get().setUserName(userName);
+    get().setUserName(userName);
     return 1L;
   }