2
0

ConcurrentExclusiveSchedulerPairTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // ConcurrentExclusiveSchedulerPairTest.cs
  3. //
  4. // Author:
  5. // Jérémie "garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2011 Jérémie "garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Threading;
  28. using System.Threading.Tasks;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Threading.Tasks
  31. {
  32. [TestFixture]
  33. public class ConcurrentExclusiveSchedulerPairTest
  34. {
  35. ConcurrentExclusiveSchedulerPair schedPair;
  36. TaskFactory factory;
  37. [Test]
  38. public void BasicExclusiveUsageTest ()
  39. {
  40. schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
  41. factory = new TaskFactory (schedPair.ExclusiveScheduler);
  42. bool launched = false;
  43. factory.StartNew (() => launched = true);
  44. Thread.Sleep (600);
  45. Assert.IsTrue (launched);
  46. }
  47. [Test]
  48. public void BasicConcurrentUsageTest ()
  49. {
  50. schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
  51. factory = new TaskFactory (schedPair.ConcurrentScheduler);
  52. bool launched = false;
  53. factory.StartNew (() => launched = true);
  54. Thread.Sleep (600);
  55. Assert.IsTrue (launched);
  56. }
  57. [Test]
  58. public void ExclusiveUsageTest ()
  59. {
  60. schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
  61. factory = new TaskFactory (schedPair.ExclusiveScheduler);
  62. int count = 0;
  63. ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
  64. ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
  65. factory.StartNew (() => {
  66. mreStart.Set ();
  67. Interlocked.Increment (ref count);
  68. mreFinish.Wait ();
  69. });
  70. mreStart.Wait ();
  71. factory.StartNew (() => Interlocked.Increment (ref count));
  72. Thread.Sleep (100);
  73. Assert.AreEqual (1, count);
  74. mreFinish.Set ();
  75. }
  76. [Test]
  77. public void ConcurrentUsageTest ()
  78. {
  79. schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
  80. factory = new TaskFactory (schedPair.ConcurrentScheduler);
  81. int count = 0;
  82. ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
  83. CountdownEvent cntd = new CountdownEvent (2);
  84. factory.StartNew (() => {
  85. Interlocked.Increment (ref count);
  86. cntd.Signal ();
  87. mreFinish.Wait ();
  88. });
  89. factory.StartNew (() => {
  90. Interlocked.Increment (ref count);
  91. cntd.Signal ();
  92. mreFinish.Wait ();
  93. });
  94. cntd.Wait ();
  95. Assert.AreEqual (2, count);
  96. mreFinish.Set ();
  97. }
  98. [Test]
  99. public void ConcurrentUsageWithExclusiveExecutingTest ()
  100. {
  101. schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
  102. TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
  103. TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
  104. int count = 0;
  105. bool exclStarted = false;
  106. ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
  107. ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
  108. exclFact.StartNew (() => {
  109. exclStarted = true;
  110. mreStart.Set ();
  111. mreFinish.Wait ();
  112. exclStarted = false;
  113. });
  114. mreStart.Wait ();
  115. concFact.StartNew (() => Interlocked.Increment (ref count));
  116. concFact.StartNew (() => Interlocked.Increment (ref count));
  117. Thread.Sleep (100);
  118. Assert.IsTrue (exclStarted);
  119. Assert.AreEqual (0, count);
  120. mreFinish.Set ();
  121. }
  122. [Test]
  123. public void ExclusiveUsageWithConcurrentExecutingTest ()
  124. {
  125. schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
  126. TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
  127. TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
  128. int count = 0;
  129. bool started = false;
  130. ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
  131. ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
  132. concFact.StartNew (() => {
  133. started = true;
  134. mreStart.Set ();
  135. mreFinish.Wait ();
  136. started = false;
  137. });
  138. mreStart.Wait ();
  139. exclFact.StartNew (() => Interlocked.Increment (ref count));
  140. Thread.Sleep (100);
  141. Assert.IsTrue (started);
  142. Assert.AreEqual (0, count);
  143. mreFinish.Set ();
  144. }
  145. }
  146. }