Reviewers: Ray Ryan, Description: CookieTest expects a cookie to expire in 5 seconds based on the host time with a 1000ms window of error. If the client time is more than 1000ms behind the host time, the cookie will not expire and the test fails. In our test infrastructure, this is sometimes true.
We raised the error window from 10ms to 1000ms last week, but it isn't always enough and the test is still non-deterministic. Fix: === We now use a native method to get the current time, ensuring that we base the expiration on the client time instead of the host time. The test is now deterministic. Testing: ======== Verified that the test now passes even with a 10ms error window. (I left the error window at 1000ms). Please review this at http://gwt-code-reviews.appspot.com/105807 Affected files: user/test/com/google/gwt/user/client/CookieTest.java Index: user/test/com/google/gwt/user/client/CookieTest.java =================================================================== --- user/test/com/google/gwt/user/client/CookieTest.java (revision 7030) +++ user/test/com/google/gwt/user/client/CookieTest.java (working copy) @@ -15,6 +15,7 @@ */ package com.google.gwt.user.client; +import com.google.gwt.core.client.GWT; import com.google.gwt.junit.client.GWTTestCase; import java.util.Collection; @@ -33,7 +34,7 @@ public void test() { // Make the cookie expire in one minute, so that they don't hang around // past the end of this test. - Date expires = new Date(new Date().getTime() + (60 * 1000)); + Date expires = new Date(getClientTime() + (60 * 1000)); // Test setting a simple cookie. Cookies.setCookie("foo", "bar", expires); @@ -70,8 +71,8 @@ final String sessionCookie = "shouldNotExpire" + uniqueId; // Test that the cookie expires in 5 seconds - Date expiresEarly = new Date(new Date().getTime() + (5 * 1000)); - Date expiresLate = new Date(new Date().getTime() + (60 * 1000)); + Date expiresEarly = new Date(getClientTime() + (5 * 1000)); + Date expiresLate = new Date(getClientTime() + (60 * 1000)); Cookies.setCookie(earlyCookie, "early", expiresEarly); Cookies.setCookie(lateCookie, "late", expiresLate); Cookies.setCookie(sessionCookie, "forever", null); @@ -279,4 +280,22 @@ cookies = Cookies.getCookieNames(); assertEquals(curCount, cookies.size()); } + + /** + * Get the current time in milliseconds from the client. Some tests rely on + * exact timings that will fail in development mode if the current time on the + * host and client are off by more than 1000ms. + * + * @return the time on the client + */ + private long getClientTime() { + if (GWT.isScript()) { + return (new Date()).getTime(); + } + return (long) getClientTimeImpl(); + } + + private native double getClientTimeImpl() /*-{ + return (new Date()).getTime(); + }-*/; } -- http://groups.google.com/group/Google-Web-Toolkit-Contributors
