File: TimerTest.cs

package info (click to toggle)
mono 1.2.2.1-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 142,728 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,304; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (132 lines) | stat: -rw-r--r-- 2,882 bytes parent folder | download | duplicates (2)
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
//
// TimerTest.cs - NUnit test cases for System.Threading.Timer
//
// Author:
//   Zoltan Varga (vargaz@freemail.hu)
//
// (C) 2004 Novell, Inc (http://www.novell.com)
//

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

namespace MonoTests.System.Threading {

	[TestFixture]
	//
	// This whole test seems to fail randomly. Either
	// - It is relying on a race it might not win (that the timer code runs)
	// - We have a very obscure bug with appdomains.
	//
	// Am going with door #1, but it would be nice to investigate this.
	// -- Ben
	//
	public class TimerTest : Assertion {
		[Test]
		[Category ("NotWorking")]
		public void TestDueTime ()
		{
			counter = 0;
			Timer t = new Timer (new TimerCallback (Callback), null, 200, Timeout.Infinite);
			Thread.Sleep (50);
			AssertEquals ("t0", 0, counter);
			Thread.Sleep (200);
			AssertEquals ("t1", 1, counter);
			Thread.Sleep (500);
			AssertEquals ("t2", 1, counter);
			
			t.Change (10, 10);
			Thread.Sleep (500);
			Assert ("t3", counter > 20);
			t.Dispose ();
		}

		[Test]
		[Category ("NotWorking")]
		public void TestChange ()
		{
			counter = 0;
			Timer t = new Timer (new TimerCallback (Callback), null, 1, 1);
			Thread.Sleep (500);
			int c = counter;
			Assert ("t1", c > 20);
			t.Change (100, 100);
			Thread.Sleep (500);
			Assert ("t2", counter <= c + 6);
			t.Dispose ();
		}

		[Test]
		[Category ("NotWorking")]
		public void TestZeroDueTime () {
			counter = 0;

			Timer t = new Timer (new TimerCallback (Callback), null, 0, Timeout.Infinite);
			Thread.Sleep (100);
			AssertEquals (1, counter);
			t.Change (0, Timeout.Infinite);
			Thread.Sleep (100);
			AssertEquals (2, counter);
			t.Dispose ();
		}

		[Test]
		[Category ("NotWorking")]
		public void TestDispose ()
		{
			counter = 0;
			Timer t = new Timer (new TimerCallback (CallbackTestDispose), null, 10, 10);
			Thread.Sleep (200);
			t.Dispose ();
			Thread.Sleep (20);
			int c = counter;
			Assert (counter > 5);
			Thread.Sleep (200);
			AssertEquals (c, counter);
		}

		[Test] // bug #78208
		public void TestDispose2 ()
		{
			Timer t = new Timer (new TimerCallback (CallbackTestDispose), null, 10, 10);
			t.Dispose ();
			t.Dispose ();
		}
		
		[Test]
		[Category ("NotWorking")]
		public void TestDisposeOnCallback () {
			counter = 0;
			t1 = new Timer (new TimerCallback (CallbackTestDisposeOnCallback), null, 0, 10);
			Thread.Sleep (200);
			AssertNull (t1);
			
			counter = 2;
			t1 = new Timer (new TimerCallback (CallbackTestDisposeOnCallback), null, 50, 0);
			Thread.Sleep (200);
			AssertNull (t1);
		}
		
		private void CallbackTestDisposeOnCallback (object foo)
		{
			if (++counter == 3) {
				t1.Dispose ();
				t1 = null;
			}
		}

		private void CallbackTestDispose (object foo)
		{
			counter++;
		}

		private void Callback (object foo)
		{
			counter++;
		}

		Timer t1;
		int counter;
	}
}