Merge pull request #1426 from ignacionr/webclient-delay-file-creation
[mono.git] / mcs / class / corlib / Test / System.Threading / TimerTest.cs
index 2749d99b759b24b4635c28316090ad48fc15516f..ee60eb53aa23b42e028e1c2d69ca64e41bb92602 100644 (file)
@@ -29,9 +29,13 @@ namespace MonoTests.System.Threading {
                        // this makes fair for the "DueTime" test since it 
                        // doesn't have to wait for the scheduler thread to be 
                        // created. 
-                       new Timer (null, null, Timeout.Infinite, 0);
+                       new Timer (new TimerCallback (DoNothing), null, Timeout.Infinite, 0);
                }
-               
+
+               void DoNothing (object foo)
+               {
+               }
+
                [Test]
                public void TestDueTime ()
                {
@@ -53,13 +57,14 @@ namespace MonoTests.System.Threading {
                public void TestChange ()
                {
                        Bucket bucket = new Bucket();
-                       Timer t = new Timer (new TimerCallback (Callback), bucket, 1, 1);
+                       Timer t = new Timer (new TimerCallback (Callback), bucket, 10, 10);
                        Thread.Sleep (500);
                        int c = bucket.count;
-                       Assert.IsTrue(c > 20, "#1");
+                       Assert.IsTrue (c > 20, "#1 " + c.ToString ());
                        t.Change (100, 100);
+                       c = bucket.count;
                        Thread.Sleep (500);
-                       Assert.IsTrue(bucket.count <= c + 20, "#2");
+                       Assert.IsTrue (bucket.count <= c + 20, "#2 " + c.ToString ());
                        t.Dispose ();
                }
 
@@ -188,13 +193,16 @@ namespace MonoTests.System.Threading {
                        Thread.Sleep(100);
                        t.Change (int.MaxValue, Timeout.Infinite);
                        // since period is 0 the callback should happen once (bug #340212)
-                       Assert.IsTrue(b.count == 1);
-                       
+                       Assert.AreEqual (1, b.count, "only once");
                }
 
                [Test]
+               [Ignore ()]
                public void TestDisposeOnCallback ()
                {
+                       // this test is bad, as the provided `state` (t1) is null and will throw an NRE inside the callback
+                       // that was ignored before 238785a3e3d510528228fc551625975bc508c2f3 and most unit test runner won't
+                       // report it since the NRE will not happen on the main thread (but Touch.Unit will)
                        Timer t1 = null;
                        t1 = new Timer (new TimerCallback (CallbackTestDisposeOnCallback), t1, 0, 10);
                        Thread.Sleep (200);
@@ -206,11 +214,114 @@ namespace MonoTests.System.Threading {
                {
                        ((Timer) foo).Dispose ();
                }
-               
+
                private void Callback (object foo)
                {
                        Bucket b = foo as Bucket;
                        Interlocked.Increment (ref b.count);
                }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void DisposeNullWaitHandle ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Dispose (null);
+                       }
+               }
+
+               [Test]
+               public void Change_IntInt_Infinite ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change ((int)Timeout.Infinite, (int)Timeout.Infinite);
+                       }
+               }
+
+               [Test]
+               public void Change_IntInt_MaxValue ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change (Int32.MaxValue, Int32.MaxValue);
+                       }
+               }
+
+               [Test]
+               public void Change_UIntUInt_Infinite ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change (unchecked ((uint) Timeout.Infinite), unchecked ((uint) Timeout.Infinite));
+                       }
+               }
+
+               [Test]
+               public void Change_UIntUInt_MaxValue ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               // UInt32.MaxValue == Timeout.Infinite == 0xffffffff
+                               t.Change (UInt32.MaxValue, UInt32.MaxValue);
+                       }
+               }
+
+               [Test]
+               public void Change_LongLong_Infinite ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change ((long) Timeout.Infinite, (long) Timeout.Infinite);
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void Change_LongLong_MaxValue ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change (Int64.MaxValue, Int64.MaxValue);
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void Change_LongLong_UInt32MaxValue ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               // not identical to (long)-1
+                               t.Change ((long)UInt32.MaxValue, (long)UInt32.MaxValue);
+                       }
+               }
+
+               [Test]
+               public void Change_LongLong_UInt32MaxValueMinusOne ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               // not identical to (long)-1
+                               t.Change ((long) UInt32.MaxValue - 1, (long) UInt32.MaxValue -1);
+                       }
+               }
+
+               [Test]
+               public void Change_TimeSpanTimeSpan_Infinite ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change (new TimeSpan (-1), new TimeSpan (-1));
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void Change_TimeSpanTimeSpan_MaxValue ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change (TimeSpan.MaxValue, TimeSpan.MaxValue);
+                       }
+               }
+
+               [Test]
+               public void Change_TimeSpanTimeSpan_UInt32MaxValue ()
+               {
+                       using (Timer t = new Timer (DoNothing, null, 0, 0)) {
+                               t.Change (new TimeSpan (UInt32.MaxValue), new TimeSpan (UInt32.MaxValue));
+                       }
+               }
        }
 }