ThreadPoolTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // ThreadPoolTest.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
  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. //
  27. //
  28. using System;
  29. using System.Threading;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Threading
  32. {
  33. [TestFixture]
  34. public class ThreadPoolTests
  35. {
  36. int minWorkerThreads;
  37. int minCompletionPortThreads;
  38. int maxWorkerThreads;
  39. int maxCompletionPortThreads;
  40. [SetUp]
  41. public void SetUp ()
  42. {
  43. ThreadPool.GetMinThreads (out minWorkerThreads, out minCompletionPortThreads);
  44. ThreadPool.GetMaxThreads (out maxWorkerThreads, out maxCompletionPortThreads);
  45. }
  46. [TearDown]
  47. public void TearDown ()
  48. {
  49. ThreadPool.SetMinThreads (minWorkerThreads, minCompletionPortThreads);
  50. ThreadPool.SetMaxThreads (maxWorkerThreads, maxCompletionPortThreads);
  51. }
  52. [Test]
  53. public void RegisterWaitForSingleObject_InvalidArguments ()
  54. {
  55. try {
  56. ThreadPool.RegisterWaitForSingleObject (null, delegate {}, new object (), 100, false);
  57. Assert.Fail ("#1");
  58. } catch (ArgumentNullException) {
  59. }
  60. try {
  61. ThreadPool.RegisterWaitForSingleObject (new Mutex (), null, new object (), 100, false);
  62. Assert.Fail ("#2");
  63. } catch (ArgumentNullException) {
  64. }
  65. }
  66. [Test]
  67. public void UnsafeQueueUserWorkItem_InvalidArguments ()
  68. {
  69. try {
  70. ThreadPool.UnsafeQueueUserWorkItem (null, 1);
  71. Assert.Fail ("#1");
  72. } catch (ArgumentNullException) {
  73. }
  74. }
  75. [Test]
  76. public void QueueUserWorkItem ()
  77. {
  78. int n = 100000;
  79. int total = 0, sum = 0;
  80. for (int i = 0; i < n; ++i) {
  81. if (i % 2 == 0)
  82. ThreadPool.QueueUserWorkItem (_ => { Interlocked.Decrement (ref sum); Interlocked.Increment (ref total); });
  83. else
  84. ThreadPool.QueueUserWorkItem (_ => { Interlocked.Increment (ref sum); Interlocked.Increment (ref total); });
  85. }
  86. var start = DateTime.Now;
  87. while ((total != n || sum != 0) && (DateTime.Now - start).TotalSeconds < 60)
  88. Thread.Sleep (1000);
  89. Assert.IsTrue (total == n, "#1");
  90. Assert.IsTrue (sum == 0, "#2");
  91. }
  92. event WaitCallback e;
  93. [Test]
  94. public void UnsafeQueueUserWorkItem_MulticastDelegate ()
  95. {
  96. CountdownEvent ev = new CountdownEvent (2);
  97. e += delegate {
  98. ev.Signal ();
  99. };
  100. e += delegate {
  101. ev.Signal ();
  102. };
  103. ThreadPool.UnsafeQueueUserWorkItem (e, null);
  104. Assert.IsTrue (ev.Wait (3000));
  105. }
  106. [Test]
  107. public void SetAndGetMinThreads ()
  108. {
  109. int workerThreads, completionPortThreads;
  110. int workerThreads_new, completionPortThreads_new;
  111. ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
  112. Assert.IsTrue (workerThreads > 0, "#1");
  113. Assert.IsTrue (completionPortThreads > 0, "#2");
  114. workerThreads_new = workerThreads == 1 ? 2 : 1;
  115. completionPortThreads_new = completionPortThreads == 1 ? 2 : 1;
  116. ThreadPool.SetMinThreads (workerThreads_new, completionPortThreads_new);
  117. ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
  118. Assert.IsTrue (workerThreads == workerThreads_new, "#3");
  119. Assert.IsTrue (completionPortThreads == completionPortThreads_new, "#4");
  120. }
  121. [Test]
  122. public void SetAndGetMaxThreads ()
  123. {
  124. int cpuCount = Environment.ProcessorCount;
  125. int workerThreads, completionPortThreads;
  126. int workerThreads_new, completionPortThreads_new;
  127. ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
  128. Assert.IsTrue (workerThreads > 0, "#1");
  129. Assert.IsTrue (completionPortThreads > 0, "#2");
  130. workerThreads_new = workerThreads == cpuCount ? cpuCount + 1 : cpuCount;
  131. completionPortThreads_new = completionPortThreads == cpuCount ? cpuCount + 1 : cpuCount;
  132. ThreadPool.SetMaxThreads (workerThreads_new, completionPortThreads_new);
  133. ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
  134. Assert.IsTrue (workerThreads == workerThreads_new, "#3");
  135. Assert.IsTrue (completionPortThreads == completionPortThreads_new, "#4");
  136. }
  137. [Test]
  138. public void GetAvailableThreads ()
  139. {
  140. ManualResetEvent mre = new ManualResetEvent (false);
  141. DateTime start = DateTime.Now;
  142. int i, workerThreads, completionPortThreads;
  143. try {
  144. Assert.IsTrue (ThreadPool.SetMaxThreads (Environment.ProcessorCount, Environment.ProcessorCount));
  145. while (true) {
  146. ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
  147. if (workerThreads == 0)
  148. break;
  149. Console.WriteLine ("workerThreads = {0}, completionPortThreads = {1}", workerThreads, completionPortThreads);
  150. if ((DateTime.Now - start).TotalSeconds >= 10)
  151. Assert.Fail ("did not reach 0 available threads");
  152. ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
  153. Thread.Sleep (1);
  154. }
  155. } finally {
  156. mre.Set ();
  157. }
  158. }
  159. void GetAvailableThreads_Callback (object state)
  160. {
  161. ManualResetEvent mre = (ManualResetEvent) state;
  162. if (mre.WaitOne (0))
  163. return;
  164. ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
  165. ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
  166. ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
  167. ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
  168. mre.WaitOne ();
  169. }
  170. }
  171. }