TimerTest.cs 2.8 KB

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