Merge pull request #4376 from kumpera/test-hardneing
authorRodrigo Kumpera <kumpera@users.noreply.github.com>
Thu, 16 Feb 2017 09:01:33 +0000 (01:01 -0800)
committerGitHub <noreply@github.com>
Thu, 16 Feb 2017 09:01:33 +0000 (01:01 -0800)
Disable some flaky tests and harden another one.

mcs/class/corlib/Test/System.Threading/TimerTest.cs
mcs/class/corlib/Test/System/GCTest.cs

index ce1c9793fc57daeef63df5687cbea2c505ac916d..b9ae6d1709ad07c3bd99073e4271bb4947271360 100644 (file)
@@ -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,37 +37,52 @@ namespace MonoTests.System.Threading {
                {
                }
 
+               private void Callback2 (object foo)
+               {
+                       Bucket b = foo as Bucket;
+                       Interlocked.Increment (ref b.count);
+                       b.mre.Set ();
+               }
+
+
                [Test]
                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)) {
+                               Assert.IsTrue (bucket.mre.Wait (5000), "#-1");
+                               Assert.AreEqual (1, bucket.count, "#1");
                        }
                }
 
+               [Test]
+               public void TestDispose ()
+               {       
+                       Bucket bucket = new Bucket();
+
+                       using (Timer t = new Timer (o => Callback2 (o), bucket, 10, 10)) {
+                               Assert.IsTrue (bucket.mre.Wait (5000), "#-1");
+                       }
+                       //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)) {
+                               Assert.IsTrue (bucket.mre.Wait (5000), "#-1");
                                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);
                        }
                }
 
@@ -75,31 +91,16 @@ namespace MonoTests.System.Threading {
                {
                        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)) {
+                               Assert.IsTrue (bucket.mre.Wait (5000), "#-1");
+                               bucket.mre.Reset ();
                                Assert.AreEqual (1, bucket.count, "#1");
                                t.Change (0, Timeout.Infinite);
-                               Thread.Sleep (100);
+                               Assert.IsTrue (bucket.mre.Wait (5000), "#1.5");
                                Assert.AreEqual (2, bucket.count, "#2");
                        }
                }
 
-               [Test]
-               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 ()
                {
index 6ce65b4b432018862fbbef3c5a45cb9d2d2db94a..59d50b92845a3700771805437d2708be9c4b7eff 100644 (file)
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 using NUnit.Framework;
@@ -57,7 +58,10 @@ namespace MonoTests.System {
                [Test]
                public void ReRegisterForFinalizeTest ()
                {
-                       Run_ReRegisterForFinalizeTest ();
+                       var thread =  new Thread (Run_ReRegisterForFinalizeTest);
+                       thread.Start ();
+                       thread.Join ();
+
                        var t = Task.Factory.StartNew (() => {
                                do {
                                        GC.Collect ();