public inbox for mauve-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: mauve-discuss@sourceware.org
Subject: Thread.sleep() tests updated
Date: Wed, 12 Oct 2005 12:42:00 -0000	[thread overview]
Message-ID: <1129120935.5922.7.camel@localhost.localdomain> (raw)


[-- 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 --]

                 reply	other threads:[~2005-10-12 12:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1129120935.5922.7.camel@localhost.localdomain \
    --to=mark@klomp.org \
    --cc=mauve-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).