public inbox for mauve-discuss@sourceware.org
 help / color / mirror / Atom feed
* Thread.sleep() tests updated
@ 2005-10-12 12:42 Mark Wielaard
  0 siblings, 0 replies; only message in thread
From: Mark Wielaard @ 2005-10-12 12:42 UTC (permalink / raw)
  To: mauve-discuss


[-- Attachment #1.1: Type: text/plain, Size: 539 bytes --]

Hi,

The Thread.sleep() tests were not really deterministic.
This fixes that by explicitly waiting for the helper thread to sleep and
never using "zero sleeptime". The downside is that the tests now takes
between 10 and 12 seconds because of the explicit sleep() statements.
The upside is that the results are now deterministic.

2005-10-12  Mark Wielaard  <mark@klomp.org>

    * gnu/testlet/java/lang/Thread/sleep.java: Explicitly wait for helper
    thread to start sleeping. Add extra debug info.


Committed,

Mark


[-- Attachment #1.2: sleep.diff --]
[-- Type: text/x-patch, Size: 6186 bytes --]

Index: gnu/testlet/java/lang/Thread/sleep.java
===================================================================
RCS file: /cvs/mauve/mauve/gnu/testlet/java/lang/Thread/sleep.java,v
retrieving revision 1.3
diff -u -r1.3 sleep.java
--- gnu/testlet/java/lang/Thread/sleep.java	31 Dec 2004 12:51:21 -0000	1.3
+++ gnu/testlet/java/lang/Thread/sleep.java	12 Oct 2005 12:02:36 -0000
@@ -30,6 +30,7 @@
   private TestHarness harness;
 
   private Thread thread;
+  private boolean helper_started;
   private boolean helper_done;
 
   private final static long SLEEP_TIME = 5 * 1000; // 5 seconds
@@ -56,6 +57,7 @@
 	// (It should also go to sleep)
 	synchronized(this)
 	  {
+	    helper_started = true;
 	    this.notify();
 	  }
 
@@ -70,7 +72,8 @@
       }
     catch (InterruptedException ie)
       {
-	harness.fail("Interrupted in helper thread");
+	harness.debug("Interrupted in helper thread");
+	harness.check(false);
       }
   }
 
@@ -95,11 +98,13 @@
 	// Wait for the helper to start (and sleep immediately).
 	try
 	  {
-	    this.wait(0);
+	    while (!helper_started)
+	      this.wait();
 	  }
 	catch (InterruptedException ie)
 	  {
-	    harness.fail("Interruped during wait for helper thread");
+	    harness.debug("Interrupted during helper start");
+	    harness.check(false);
 	  }
 	
 	// Go to sleep.
@@ -117,8 +122,10 @@
 	
 	// About half the time should have been spent sleeping.
 	long present = System.currentTimeMillis();
-	harness.check(present - past >= SLEEP_TIME / 2);
-	harness.check(present - past < SLEEP_TIME);
+	long diff = present - past;
+	harness.debug("diff: " + diff);
+	harness.check(diff >= SLEEP_TIME / 2);
+	harness.check(diff < SLEEP_TIME);
 
 	// Even though we are interrupted,
 	// the thread interrupted flag should be cleared.
@@ -136,7 +143,8 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("Interruped during joining the helper thread");
+	harness.debug("Interruped during joining the helper thread");
+	harness.check(false);
       }
     harness.check(helper_done);
     
@@ -177,7 +185,6 @@
     invalid(Long.MAX_VALUE, Integer.MAX_VALUE);
 
     // (Large) valid argument checks
-    harness.checkPoint("Long (interrupted) sleep");
     valid(Integer.MAX_VALUE);
     valid(Long.MAX_VALUE);
     valid(Integer.MAX_VALUE, 0);
@@ -202,8 +209,10 @@
     // The thread should have slept at least 5 miliseconds.
     // But certainly not more than 500 miliseconds.
     long present = System.currentTimeMillis();
-    harness.check(present - past > 5);
-    harness.check(present - past < 500);
+    long diff = present - past;
+    harness.debug("diff: " + diff);
+    harness.check(diff > 5);
+    harness.check(diff < 500);
 
 
     // A thread in interrupted state that goes to sleep gets
@@ -239,7 +248,8 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("InterruptedException in invalid(" + milli + ")");
+	harness.debug("InterruptedException in invalid(" + milli + ")");
+	harness.check(false);
       }
     harness.check(illegal_argument);
   }
@@ -257,8 +267,9 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("InterruptedException in invalid("
-		     + milli + ", " + nano + ")");
+	harness.debug("InterruptedException in invalid("
+		      + milli + ", " + nano + ")");
+	harness.check(false);
       }
     harness.check(illegal_argument);
 
@@ -266,13 +277,30 @@
 
   private void valid(long milli)
   {
+    harness.checkPoint("valid long:" + milli);
     Thread helper = new Thread(this);
+    helper_started = false;
     helper_done = false;
-    helper_sleep = 0;
+    helper_sleep = 1000;
     thread = Thread.currentThread();
+
+    // Wait for the helper to start (and sleep immediately).
+    helper.start();
+    synchronized(this)
+      {
+	try
+	  {
+	    while (!helper_started)
+	      this.wait();
+	  }
+	catch (InterruptedException ie)
+	  {
+	    harness.debug("Interrupted during helper start");
+	    harness.check(false);
+	  }
+      }
     
     boolean interrupted_exception = false;
-    helper.start();
     try
       {
 	Thread.sleep(milli);
@@ -289,20 +317,38 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("Interruped during joining the helper thread");
+	harness.debug("Interruped during joining the helper thread");
+	harness.check(false);
       }
     harness.check(helper_done);
   }
 
   private void valid(long milli, int nano)
   {
+    harness.checkPoint("valid long " + milli + " int " + nano);
     Thread helper = new Thread(this);
+    helper_started = false;
     helper_done = false;
-    helper_sleep = 0;
+    helper_sleep = 1000;
     thread = Thread.currentThread();
     
-    boolean interrupted_exception = false;
+    // Wait for the helper to start (and sleep immediately).
     helper.start();
+    synchronized(this)
+      {
+	try
+	  {
+	    while (!helper_started)
+	      this.wait();
+	  }
+	catch (InterruptedException ie)
+	  {
+	    harness.debug("Interrupted during helper start");
+	    harness.check(false);
+	  }
+      }
+    
+    boolean interrupted_exception = false;
     try
       {
 	Thread.sleep(milli, nano);
@@ -331,7 +377,8 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("Interruped during joining the helper thread");
+	harness.debug("Interrupted during joining the helper thread");
+	harness.check(false);
       }
     harness.check(helper_done);
   }
@@ -345,7 +392,8 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("InterruptedException in nearZero(" + milli + ")");
+	harness.debug("InterruptedException in nearZero(" + milli + ")");
+	harness.check(false);
       }
   }
 
@@ -358,8 +406,9 @@
       }
     catch(InterruptedException ie)
       {
-	harness.fail("InterruptedException in nearZero("
-		     + milli + ", " + nano + ")");
+	harness.debug("InterruptedException in nearZero("
+		      + milli + ", " + nano + ")");
+	harness.check(false);
       }
   }
 

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-10-12 12:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-12 12:42 Thread.sleep() tests updated Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).