Browse Source

[corlib] Rewrite the timer tests to something that can actually be verified.

Rodrigo Kumpera 9 years ago
parent
commit
779b5e8aed
1 changed files with 35 additions and 38 deletions
  1. 35 38
      mcs/class/corlib/Test/System.Threading/TimerTest.cs

+ 35 - 38
mcs/class/corlib/Test/System.Threading/TimerTest.cs

@@ -19,6 +19,7 @@ namespace MonoTests.System.Threading {
 		// this bucket is used to avoid non-theadlocal issues
 		class Bucket {
 			public int count;
+			public ManualResetEventSlim mre = new ManualResetEventSlim (false); 
 		}
 
 		[SetUp]
@@ -36,74 +37,70 @@ namespace MonoTests.System.Threading {
 		{
 		}
 
+		private void Callback2 (object foo)
+		{
+			Bucket b = foo as Bucket;
+			Interlocked.Increment (ref b.count);
+			b.mre.Set ();
+		}
+
+
 		[Test]
-		[Category ("NotWorking")]
 		public void TestDueTime ()
 		{
 			Bucket bucket = new Bucket();
 
-			using (Timer t = new Timer (o => Callback (o), bucket, 200, Timeout.Infinite)) {
-				Thread.Sleep (50);
-				Assert.AreEqual (0, bucket.count, "#1");
-				Thread.Sleep (200);
-				Assert.AreEqual (1, bucket.count, "#2");
-				Thread.Sleep (500);
-				Assert.AreEqual (1, bucket.count, "#3");
-				t.Change (10, 10);
-				Thread.Sleep (1000);
-				Assert.IsTrue(bucket.count > 20, "#4");
+			using (Timer t = new Timer (o => Callback2 (o), bucket, 200, Timeout.Infinite)) {
+				bucket.mre.Wait (5000);
+				Assert.AreEqual (1, bucket.count, "#1");
 			}
 		}
 
 		[Test]
-		[Category ("NotWorking")]
+		public void TestDispose ()
+		{	
+			Bucket bucket = new Bucket();
+
+			using (Timer t = new Timer (o => Callback2 (o), bucket, 10, 10)) {
+				bucket.mre.Wait (5000);
+			}
+			//If the callback is called after dispose, it will NRE and be reported
+			bucket.mre = null;
+			int c = bucket.count;
+			Assert.IsTrue (c > 0, "#1");
+		}
+
+		[Test]
 		public void TestChange ()
 		{
 			Bucket bucket = new Bucket();
 
-			using (Timer t = new Timer (o => Callback (o), bucket, 10, 10)) {
-				Thread.Sleep (500);
+			using (Timer t = new Timer (o => Callback2 (o), bucket, 10, 10)) {
+				bucket.mre.Wait (5000);
 				int c = bucket.count;
-				Assert.IsTrue (c > 20, "#1 " + c.ToString ());
-				t.Change (100, 100);
+				Assert.IsTrue (c > 0, "#1 " + c);
+				t.Change (100000, 1000000);
 				c = bucket.count;
 				Thread.Sleep (500);
-				Assert.IsTrue (bucket.count <= c + 20, "#2 " + c.ToString ());
+				Assert.IsTrue (bucket.count <= c + 1, "#2 " + c);
 			}
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		public void TestZeroDueTime ()
 		{
 			Bucket bucket = new Bucket();
 
-			using (Timer t = new Timer (o => Callback (o), bucket, 0, Timeout.Infinite)) {
-				Thread.Sleep (100);
+			using (Timer t = new Timer (o => Callback2 (o), bucket, 0, Timeout.Infinite)) {
+				bucket.mre.Wait (5000);
+				bucket.mre.Reset ();
 				Assert.AreEqual (1, bucket.count, "#1");
 				t.Change (0, Timeout.Infinite);
-				Thread.Sleep (100);
+				bucket.mre.Wait (5000);
 				Assert.AreEqual (2, bucket.count, "#2");
 			}
 		}
 
-		[Test]
-		[Category ("NotWorking")]
-		public void TestDispose ()
-		{	
-			Bucket bucket = new Bucket();
-
-			using (Timer t = new Timer (o => Callback (o), bucket, 10, 10)) {
-				Thread.Sleep (200);
-			}
-
-			Thread.Sleep (20);
-			int c = bucket.count;
-			Assert.IsTrue (bucket.count > 5, "#1");
-			Thread.Sleep (200);
-			Assert.AreEqual (c, bucket.count, "#2");
-		}
-
 		[Test] // bug #320950
 		public void TestDispose2 ()
 		{