CountdownEventTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if NET_4_0
  2. // CountdownEventTests.cs
  3. //
  4. // Copyright (c) 2008 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Threading;
  27. using NUnit.Framework;
  28. using MonoTests.System.Threading.Tasks;
  29. namespace MonoTests.System.Threading
  30. {
  31. [TestFixtureAttribute]
  32. public class CountdownEventTests
  33. {
  34. CountdownEvent evt;
  35. [SetUpAttribute]
  36. public void Setup()
  37. {
  38. evt = new CountdownEvent(5);
  39. }
  40. [Test]
  41. public void InitialTestCase()
  42. {
  43. Assert.AreEqual(5, evt.InitialCount, "#1");
  44. evt.AddCount();
  45. evt.Signal(3);
  46. Assert.AreEqual(5, evt.InitialCount, "#2");
  47. }
  48. [Test]
  49. public void CurrentCountTestCase()
  50. {
  51. Assert.AreEqual(5, evt.CurrentCount, "#1");
  52. evt.AddCount();
  53. Assert.AreEqual(6, evt.CurrentCount, "#2");
  54. evt.TryAddCount(2);
  55. Assert.AreEqual(8, evt.CurrentCount, "#3");
  56. evt.Signal(4);
  57. Assert.AreEqual(4, evt.CurrentCount, "#4");
  58. evt.Reset();
  59. Assert.AreEqual(5, evt.CurrentCount, "#5");
  60. }
  61. [Test]
  62. public void IsSetTestCase()
  63. {
  64. Assert.IsFalse(evt.IsSet, "#1");
  65. evt.Signal(5);
  66. Assert.IsTrue(evt.IsSet, "#2");
  67. evt.Reset();
  68. Assert.IsFalse(evt.IsSet, "#3");
  69. }
  70. [Test]
  71. public void TryAddCountTestCase()
  72. {
  73. Assert.IsTrue(evt.TryAddCount(2), "#1");
  74. evt.Signal(7);
  75. Assert.IsFalse(evt.TryAddCount(), "#2");
  76. }
  77. [Test]
  78. public void WaitTestCase()
  79. {
  80. int count = 0;
  81. bool s = false;
  82. ParallelTestHelper.ParallelStressTest(evt, delegate (CountdownEvent e) {
  83. if (Interlocked.Increment(ref count) % 2 == 0) {
  84. Thread.Sleep(100);
  85. while(!e.IsSet)
  86. e.Signal();
  87. } else {
  88. e.Wait();
  89. s = true;
  90. }
  91. });
  92. Assert.IsTrue(s, "#1");
  93. Assert.IsTrue(evt.IsSet, "#2");
  94. }
  95. [Test]
  96. public void AddCountSignalStressTestCase()
  97. {
  98. int count = 0;
  99. ParallelTestHelper.ParallelStressTest(evt, delegate (CountdownEvent e) {
  100. int num = Interlocked.Increment(ref count);
  101. if (num % 2 == 0)
  102. e.AddCount();
  103. else
  104. e.Signal();
  105. }, 7);
  106. Assert.AreEqual(4, evt.CurrentCount, "#1");
  107. Assert.IsFalse(evt.IsSet, "#2");
  108. }
  109. }
  110. }
  111. #endif