2 // TimerTest.cs - NUnit Test Cases for System.Timers.Timer
5 // Kornél Pál <http://www.kornelpal.hu/>
6 // Robert Jordan <robertj@gmx.net>
8 // Copyright (C) 2005 Kornél Pál
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
35 using ST = System.Threading;
37 namespace MonoTests.System.Timers
40 public class TimerTest
52 public void TearDown ()
58 public void Constructor0 ()
60 Assert.IsTrue (timer.AutoReset, "#1");
61 Assert.IsFalse (timer.Enabled, "#2");
62 Assert.AreEqual (100, timer.Interval, "#3");
63 Assert.IsNull (timer.SynchronizingObject, "#4");
67 public void Constructor1 ()
69 timer = new Timer (1);
70 Assert.IsTrue (timer.AutoReset, "#A1");
71 Assert.IsFalse (timer.Enabled, "#A2");
72 Assert.AreEqual (1, timer.Interval, "#A3");
73 Assert.IsNull (timer.SynchronizingObject, "#A4");
75 timer = new Timer (int.MaxValue);
76 Assert.IsTrue (timer.AutoReset, "#B1");
77 Assert.IsFalse (timer.Enabled, "#B2");
78 Assert.AreEqual (int.MaxValue, timer.Interval, "#B3");
79 Assert.IsNull (timer.SynchronizingObject, "#B4");
83 public void Constructor1_Interval_Negative ()
88 } catch (ArgumentException ex) {
89 // Invalid value -1 for parameter interval
90 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
91 Assert.IsNull (ex.InnerException, "#3");
92 Assert.IsNotNull (ex.Message, "#4");
97 public void Constructor1_Interval_Zero ()
102 } catch (ArgumentException ex) {
103 // Invalid value 0 for parameter interval
104 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
105 Assert.IsNull (ex.InnerException, "#3");
106 Assert.IsNotNull (ex.Message, "#4");
111 public void Constructor1_Interval_Max ()
115 new Timer (0x80000000);
117 } catch (ArgumentException ex) {
118 // Invalid value 2147483648 for parameter interval
119 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
120 Assert.IsNull (ex.InnerException, "#A3");
121 Assert.IsNotNull (ex.Message, "#A4");
125 new Timer (double.MaxValue);
127 } catch (ArgumentException ex) {
128 // Invalid value 1.79769313486232E+308 for parameter interval
129 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
130 Assert.IsNull (ex.InnerException, "#B3");
131 Assert.IsNotNull (ex.Message, "#B4");
134 timer = new Timer (0x80000000);
135 Assert.AreEqual (0x80000000, timer.Interval, "#1");
136 timer = new Timer (double.MaxValue);
137 Assert.AreEqual (double.MaxValue, timer.Interval, "#2");
142 public void AutoReset ()
144 Assert.IsTrue (timer.AutoReset, "#1");
145 timer.AutoReset = false;
146 Assert.IsFalse (timer.AutoReset, "#2");
150 public void Interval ()
153 Assert.AreEqual (1, timer.Interval, "#1");
154 timer.Interval = 500;
155 Assert.AreEqual (500, timer.Interval, "#2");
156 timer.Interval = double.MaxValue;
157 Assert.AreEqual (double.MaxValue, timer.Interval, "#3");
161 public void Interval_Negative ()
166 } catch (ArgumentException ex) {
167 // '0' is not a valid value for 'Interval'. 'Interval' must be greater than 0
168 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
169 Assert.IsNull (ex.InnerException, "#3");
170 Assert.IsNotNull (ex.Message, "#4");
175 public void Interval_Zero ()
180 } catch (ArgumentException ex) {
181 // '0' is not a valid value for 'Interval'. 'Interval' must be greater than 0
182 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
183 Assert.IsNull (ex.InnerException, "#3");
184 Assert.IsNotNull (ex.Message, "#4");
189 public void StartStopEnabled ()
192 Assert.IsTrue (timer.Enabled, "#1");
194 Assert.IsFalse (timer.Enabled, "#2");
198 public void CloseEnabled ()
200 Assert.IsFalse (timer.Enabled, "#1");
201 timer.Enabled = true;
202 Assert.IsTrue (timer.Enabled, "#2");
204 Assert.IsFalse (timer.Enabled, "#3");
207 [Test] // bug #325368
208 public void EnabledInElapsed ()
211 timer = new Timer (50);
212 timer.AutoReset = false;
213 timer.Elapsed += new ElapsedEventHandler (EnabledInElapsed_Elapsed);
216 ST.Thread.Sleep (200);
219 Assert.IsTrue (_elapsedCount == 2, "#1 loss of events");
223 public void TestRaceCondition ()
225 Assert.IsTrue (new RaceTest (true).Success, "#1");
226 Assert.IsTrue (new RaceTest (false).Success, "#2");
229 void EnabledInElapsed_Elapsed (object sender, ElapsedEventArgs e)
232 Timer t = sender as Timer;
233 if (_elapsedCount == 1)
240 const int Threads = 2;
241 const int Loops = 100;
243 object locker = new object ();
247 public bool Success {
248 get { return counter > Loops * Threads; }
251 public RaceTest (bool autoReset)
253 timer = new Timer ();
254 timer.AutoReset = autoReset;
255 timer.Interval = 100;
256 timer.Elapsed += new ElapsedEventHandler (Tick);
259 ST.Thread[] tl = new ST.Thread [Threads];
261 for (int i = 0; i < Threads; i++) {
262 tl [i] = new ST.Thread (new ST.ThreadStart (Run));
266 for (int i = 0; i < Threads; i++) {
270 ST.Thread.Sleep (1000);
279 ST.Interlocked.Increment (ref counter);
282 void Tick (object sender, ElapsedEventArgs e)
289 for (int i = 0; i < Loops; i++) {