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
51 public void TearDown ()
57 public void Constructor0 ()
59 Assert.IsTrue (timer.AutoReset, "#1");
60 Assert.IsFalse (timer.Enabled, "#2");
61 Assert.AreEqual (100, timer.Interval, "#3");
62 Assert.IsNull (timer.SynchronizingObject, "#4");
66 public void Constructor1 ()
68 timer = new Timer (1);
69 Assert.IsTrue (timer.AutoReset, "#A1");
70 Assert.IsFalse (timer.Enabled, "#A2");
71 Assert.AreEqual (1, timer.Interval, "#A3");
72 Assert.IsNull (timer.SynchronizingObject, "#A4");
74 timer = new Timer (int.MaxValue);
75 Assert.IsTrue (timer.AutoReset, "#B1");
76 Assert.IsFalse (timer.Enabled, "#B2");
77 Assert.AreEqual (int.MaxValue, timer.Interval, "#B3");
78 Assert.IsNull (timer.SynchronizingObject, "#B4");
82 public void Constructor1_Interval_Negative ()
87 } catch (ArgumentException ex) {
88 // Invalid value -1 for parameter interval
89 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
90 Assert.IsNull (ex.InnerException, "#3");
91 Assert.IsNotNull (ex.Message, "#4");
96 public void Constructor1_Interval_Zero ()
101 } catch (ArgumentException ex) {
102 // Invalid value 0 for parameter interval
103 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
104 Assert.IsNull (ex.InnerException, "#3");
105 Assert.IsNotNull (ex.Message, "#4");
110 public void Constructor1_Interval_Max ()
113 new Timer (0x80000000);
115 } catch (ArgumentException ex) {
116 // Invalid value 2147483648 for parameter interval
117 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
118 Assert.IsNull (ex.InnerException, "#A3");
119 Assert.IsNotNull (ex.Message, "#A4");
123 new Timer (double.MaxValue);
125 } catch (ArgumentException ex) {
126 // Invalid value 1.79769313486232E+308 for parameter interval
127 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
128 Assert.IsNull (ex.InnerException, "#B3");
129 Assert.IsNotNull (ex.Message, "#B4");
134 [ExpectedException (typeof (ArgumentException))]
135 public void Constructor1_Interval_Max_2 ()
137 timer = new Timer (double.MaxValue);
141 [ExpectedException (typeof (ArgumentException))]
142 public void Constructor1_Interval_Min_1 ()
144 timer = new Timer (0);
148 [ExpectedException (typeof (ArgumentException))]
149 public void Constructor1_Interval_Min_2 ()
151 timer = new Timer (-5);
155 public void Interval_TooHigh_Disabled_NoThrow ()
157 timer.Interval = double.MaxValue;
158 Assert.AreEqual (double.MaxValue, timer.Interval, "#3");
162 public void Interval_TooHigh_ThrowOnEnabled ()
164 timer.Interval = 0x80000000;
165 Assert.AreEqual (0x80000000, timer.Interval, "#1");
167 timer.Enabled = true;
169 } catch (Exception ex) {
170 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#3");
171 Assert.IsTrue (timer.Enabled);
176 [ExpectedException (typeof (ArgumentOutOfRangeException))]
177 public void Interval_TooHigh_Enabled_Throw ()
179 timer.Interval = 100;
180 timer.Enabled = true;
181 timer.Interval = double.MaxValue;
185 public void DoubleClose_NoThrow ()
187 timer.Interval = 100;
194 public void DisposedMeansDisabled_NoThrow ()
196 timer.Interval = 100;
199 Assert.IsFalse (timer.Enabled);
203 public void Disposed_ThrowOnEnabled ()
205 timer.Interval = 100;
208 timer.Enabled = false;
212 public void Elapsed_DontFireIfDisposed ()
214 timer.Interval = 500;
215 var countElapsedCalls = 0;
216 timer.Elapsed += (_, __) => { countElapsedCalls++; };
219 ST.Thread.Sleep (500);
220 Assert.AreEqual (countElapsedCalls, 0);
224 public void AutoReset ()
226 Assert.IsTrue (timer.AutoReset, "#1");
227 timer.AutoReset = false;
228 Assert.IsFalse (timer.AutoReset, "#2");
232 public void Interval ()
235 Assert.AreEqual (1, timer.Interval, "#1");
236 timer.Interval = 500;
237 Assert.AreEqual (500, timer.Interval, "#2");
238 timer.Interval = double.MaxValue;
239 Assert.AreEqual (double.MaxValue, timer.Interval, "#3");
243 public void Interval_Negative ()
248 } catch (ArgumentException ex) {
249 // '0' is not a valid value for 'Interval'. 'Interval' must be greater than 0
250 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
251 Assert.IsNull (ex.InnerException, "#3");
252 Assert.IsNotNull (ex.Message, "#4");
257 public void Interval_Zero ()
262 } catch (ArgumentException ex) {
263 // '0' is not a valid value for 'Interval'. 'Interval' must be greater than 0
264 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
265 Assert.IsNull (ex.InnerException, "#3");
266 Assert.IsNotNull (ex.Message, "#4");
271 public void StartStopEnabled ()
274 Assert.IsTrue (timer.Enabled, "#1");
276 Assert.IsFalse (timer.Enabled, "#2");
280 public void CloseEnabled ()
282 Assert.IsFalse (timer.Enabled, "#1");
283 timer.Enabled = true;
284 Assert.IsTrue (timer.Enabled, "#2");
286 Assert.IsFalse (timer.Enabled, "#3");
289 [Test] // bug https://bugzilla.novell.com/show_bug.cgi?id=325368
290 public void EnabledInElapsed ()
292 var elapsedCount = 0;
293 var mre = new ST.ManualResetEventSlim ();
294 timer = new Timer (50);
295 timer.AutoReset = false;
296 timer.Elapsed += (s, e) =>
299 if (elapsedCount == 1)
300 timer.Enabled = true;
301 else if (elapsedCount == 2)
306 Assert.IsTrue (mre.Wait (500), "#1 re-enabling timer in Elapsed didn't work");
307 Assert.AreEqual (2, elapsedCount, "#2 wrong elapsedCount");
312 public void AutoResetEventFalseStopsFiringElapsed ()
314 var elapsedCount = 0;
315 var mre = new ST.ManualResetEventSlim ();
316 timer = new Timer (50);
317 timer.AutoReset = false;
318 timer.Elapsed += (s, e) =>
321 if (elapsedCount > 1)
326 Assert.IsFalse (mre.Wait (500), "#1 AutoResetEvent=false didn't stop firing Elapsed, elapsedCount=" + elapsedCount);
327 Assert.AreEqual (1, elapsedCount, "#2 wrong elapsedCount");
332 public void TestRaceCondition ()
334 Assert.IsTrue (new RaceTest (true).Success, "#1");
335 Assert.IsTrue (new RaceTest (false).Success, "#2");
341 const int Threads = 2;
342 const int Loops = 100;
344 object locker = new object ();
348 public bool Success {
349 get { return counter > Loops * Threads; }
352 public RaceTest (bool autoReset)
354 timer = new Timer ();
355 timer.AutoReset = autoReset;
356 timer.Interval = 100;
357 timer.Elapsed += new ElapsedEventHandler (Tick);
360 ST.Thread[] tl = new ST.Thread [Threads];
362 for (int i = 0; i < Threads; i++) {
363 tl [i] = new ST.Thread (new ST.ThreadStart (Run));
367 for (int i = 0; i < Threads; i++) {
371 ST.Thread.Sleep (1000);
380 ST.Interlocked.Increment (ref counter);
383 void Tick (object sender, ElapsedEventArgs e)
390 for (int i = 0; i < Loops; i++) {