summaryrefslogtreecommitdiff
path: root/mcs/class/corlib/Test/System.Threading/TimerTest.cs
blob: 2c05538c722a2f1d52986cafb13af6804b6dcc4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// TimerTest.cs - NUnit test cases for System.Threading.Timer
//
// Author:
//   Zoltan Varga (vargaz@freemail.hu)
//   Rafael Ferreira (raf@ophion.org)
//
// (C) 2004 Novell, Inc (http://www.novell.com)
//

using NUnit.Framework;
using System;
using System.Threading;
using System.Collections;

namespace MonoTests.System.Threading {
	[TestFixture]
	public class TimerTest {
		// this bucket is used to avoid non-theadlocal issues
		class Bucket {
			public int count;
		}

		[SetUp]
		public void Setup ()
		{
			//creating a timer that will never run just to make sure the
			// scheduler is warm for the unit tests
			// this makes fair for the "DueTime" test since it 
			// doesn't have to wait for the scheduler thread to be 
			// created. 
			new Timer (new TimerCallback (DoNothing), null, Timeout.Infinite, 0);
		}

		void DoNothing (object foo)
		{
		}

		[Test]
		public void TestDueTime ()
		{
			Bucket bucket = new Bucket();
			Timer t = new Timer (new TimerCallback (Callback), 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");
			t.Dispose ();
		}

		[Test]
		public void TestChange ()
		{
			Bucket bucket = new Bucket();
			Timer t = new Timer (new TimerCallback (Callback), bucket, 10, 10);
			Thread.Sleep (500);
			int c = bucket.count;
			Assert.IsTrue(c > 20, "#1");
			t.Change (100, 100);
			c = bucket.count;
			Thread.Sleep (500);
			Assert.IsTrue(bucket.count <= c + 20, "#2");
			t.Dispose ();
		}

		[Test]
		public void TestZeroDueTime ()
		{
			Bucket bucket = new Bucket();

			Timer t = new Timer (new TimerCallback (Callback), bucket, 0, Timeout.Infinite);
			Thread.Sleep (100);
			Assert.AreEqual (1, bucket.count, "#1");
			t.Change (0, Timeout.Infinite);
			Thread.Sleep (100);
			Assert.AreEqual (2, bucket.count, "#2");
			t.Dispose ();
		}

		[Test]
		public void TestDispose ()
		{	
			Bucket bucket = new Bucket();
			Timer t = new Timer (new TimerCallback (Callback), bucket, 10, 10);
			Thread.Sleep (200);
			t.Dispose ();
			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 ()
		{
			Timer t = new Timer (new TimerCallback (Callback), null, 10, 10);
			t.Dispose ();
			t.Dispose ();
		}
		
		[Test]
		public void TestHeavyCreationLoad ()
		{
			Bucket b = new Bucket ();
			for (int i = 0; i < 500; ++i)
				new Timer (new TimerCallback (Callback), b, 10,
					Timeout.Infinite);
			// 1000 * 10 msec = 10,000 msec or 10 sec - if everything goes well
			// we add some slack to cope with timing issues caused by system load etc.
			for (int i = 0; i < 20; ++i) {
				if (b.count == 500)
					break;
				Thread.Sleep (1000);
			}
			Assert.AreEqual (500, b.count);
			
		}

		[Test]
		public void TestQuickDisposeDeadlockBug ()
		{
			int i = 0;
			Bucket b = new Bucket ();
			ArrayList timers = new ArrayList();
			while (i < 500) {
				Timer t = new Timer (new TimerCallback (Callback),
					b, 10, Timeout.Infinite);
				timers.Add (t);
				i++;
				t.Dispose ();
			}
			Thread.Sleep (11 * 500);
		}

		[Test]
		public void TestInt32MaxDelay ()
		{
			Bucket b = new Bucket ();
			new Timer (new TimerCallback (Callback), b, Int32.MaxValue,
				Timeout.Infinite);
			Thread.Sleep (50);
			Assert.AreEqual (0, b.count);
		}

		[Test]
		public void TestInt32MaxPeriod ()
		{
			Bucket b = new Bucket ();
			new Timer (new TimerCallback (Callback), b, 0,
				Int32.MaxValue);
			Thread.Sleep (50);
			Assert.AreEqual (1, b.count);
			
		}

		[Test]
		public void TestNegativeDelay ()
		{
			Bucket b = new Bucket ();
			try {
				new Timer (new TimerCallback (Callback), b, -10,
					Timeout.Infinite);
				Assert.Fail ();
			} catch (ArgumentOutOfRangeException) {
				return;
			}
		}

		[Test]
		public void TestNegativePeriod ()
		{
			Bucket b = new Bucket ();
			try {
				new Timer (new TimerCallback (Callback), b, 0,
					-10);
				Assert.Fail ();
			} catch (ArgumentOutOfRangeException) {
				return;
			}
		}

		[Test]
		public void TestDelayZeroPeriodZero()
		{
			Bucket b = new Bucket();
			Timer t = new Timer(new TimerCallback(Callback),b,0,0);
			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);
			
		}

		[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);
			Assert.IsNotNull (t1);
			
		}

		private void CallbackTestDisposeOnCallback (object foo)
		{
			((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));
			}
		}
	}
}