New test.
[mono.git] / mcs / class / corlib / Test / System.Threading / TimerTest.cs
1 //
2 // TimerTest.cs - NUnit test cases for System.Threading.Timer
3 //
4 // Author:
5 //   Zoltan Varga (vargaz@freemail.hu)
6 //
7 // (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Threading;
13
14 namespace MonoTests.System.Threading {
15
16         [TestFixture]
17         //
18         // This whole test seems to fail randomly. Either
19         // - It is relying on a race it might not win (that the timer code runs)
20         // - We have a very obscure bug with appdomains.
21         //
22         // Am going with door #1, but it would be nice to investigate this.
23         // -- Ben
24         //
25         public class TimerTest : Assertion {
26                 [Test]
27                 [Category ("NotWorking")]
28                 public void TestDueTime ()
29                 {
30                         counter = 0;
31                         Timer t = new Timer (new TimerCallback (Callback), null, 200, Timeout.Infinite);
32                         Thread.Sleep (50);
33                         AssertEquals ("t0", 0, counter);
34                         Thread.Sleep (200);
35                         AssertEquals ("t1", 1, counter);
36                         Thread.Sleep (500);
37                         AssertEquals ("t2", 1, counter);
38                         
39                         t.Change (10, 10);
40                         Thread.Sleep (500);
41                         Assert ("t3", counter > 20);
42                         t.Dispose ();
43                 }
44
45                 [Test]
46                 [Category ("NotWorking")]
47                 public void TestChange ()
48                 {
49                         counter = 0;
50                         Timer t = new Timer (new TimerCallback (Callback), null, 1, 1);
51                         Thread.Sleep (500);
52                         int c = counter;
53                         Assert ("t1", c > 20);
54                         t.Change (100, 100);
55                         Thread.Sleep (500);
56                         Assert ("t2", counter <= c + 6);
57                         t.Dispose ();
58                 }
59
60                 [Test]
61                 [Category ("NotWorking")]
62                 public void TestZeroDueTime () {
63                         counter = 0;
64
65                         Timer t = new Timer (new TimerCallback (Callback), null, 0, Timeout.Infinite);
66                         Thread.Sleep (100);
67                         AssertEquals (1, counter);
68                         t.Change (0, Timeout.Infinite);
69                         Thread.Sleep (100);
70                         AssertEquals (2, counter);
71                         t.Dispose ();
72                 }
73
74                 [Test]
75                 [Category ("NotWorking")]
76                 public void TestDispose ()
77                 {
78                         counter = 0;
79                         Timer t = new Timer (new TimerCallback (CallbackTestDispose), null, 10, 10);
80                         Thread.Sleep (200);
81                         t.Dispose ();
82                         Thread.Sleep (20);
83                         int c = counter;
84                         Assert (counter > 5);
85                         Thread.Sleep (200);
86                         AssertEquals (c, counter);
87                 }
88
89                 [Test] // bug #78208
90                 public void TestDispose2 ()
91                 {
92                         Timer t = new Timer (new TimerCallback (CallbackTestDispose), null, 10, 10);
93                         t.Dispose ();
94                         t.Dispose ();
95                 }
96                 
97                 [Test]
98                 [Category ("NotWorking")]
99                 public void TestDisposeOnCallback () {
100                         counter = 0;
101                         t1 = new Timer (new TimerCallback (CallbackTestDisposeOnCallback), null, 0, 10);
102                         Thread.Sleep (200);
103                         AssertNull (t1);
104                         
105                         counter = 2;
106                         t1 = new Timer (new TimerCallback (CallbackTestDisposeOnCallback), null, 50, 0);
107                         Thread.Sleep (200);
108                         AssertNull (t1);
109                 }
110                 
111                 private void CallbackTestDisposeOnCallback (object foo)
112                 {
113                         if (++counter == 3) {
114                                 t1.Dispose ();
115                                 t1 = null;
116                         }
117                 }
118
119                 private void CallbackTestDispose (object foo)
120                 {
121                         counter++;
122                 }
123
124                 private void Callback (object foo)
125                 {
126                         counter++;
127                 }
128
129                 Timer t1;
130                 int counter;
131         }
132 }