5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
30 using System.Threading;
31 using NUnit.Framework;
33 namespace MonoTests.System.Threading
36 public class ThreadPoolTests
39 int minCompletionPortThreads;
41 int maxCompletionPortThreads;
46 ThreadPool.GetMinThreads (out minWorkerThreads, out minCompletionPortThreads);
47 ThreadPool.GetMaxThreads (out maxWorkerThreads, out maxCompletionPortThreads);
51 public void TearDown ()
53 ThreadPool.SetMinThreads (minWorkerThreads, minCompletionPortThreads);
54 ThreadPool.SetMaxThreads (maxWorkerThreads, maxCompletionPortThreads);
58 public void RegisterWaitForSingleObject_InvalidArguments ()
61 ThreadPool.RegisterWaitForSingleObject (null, delegate {}, new object (), 100, false);
63 } catch (ArgumentNullException) {
67 ThreadPool.RegisterWaitForSingleObject (new Mutex (), null, new object (), 100, false);
69 } catch (ArgumentNullException) {
74 public void UnsafeQueueUserWorkItem_InvalidArguments ()
77 ThreadPool.UnsafeQueueUserWorkItem (null, 1);
79 } catch (ArgumentNullException) {
84 public void QueueUserWorkItem ()
87 int total = 0, sum = 0;
88 for (int i = 0; i < n; ++i) {
90 ThreadPool.QueueUserWorkItem (_ => { Interlocked.Decrement (ref sum); Interlocked.Increment (ref total); });
92 ThreadPool.QueueUserWorkItem (_ => { Interlocked.Increment (ref sum); Interlocked.Increment (ref total); });
94 var start = DateTime.Now;
95 while ((total != n || sum != 0) && (DateTime.Now - start).TotalSeconds < 60)
97 Assert.IsTrue (total == n, "#1");
98 Assert.IsTrue (sum == 0, "#2");
101 event WaitCallback e;
104 public void UnsafeQueueUserWorkItem_MulticastDelegate ()
106 CountdownEvent ev = new CountdownEvent (2);
116 ThreadPool.UnsafeQueueUserWorkItem (e, null);
117 Assert.IsTrue (ev.Wait (3000));
121 public void SetAndGetMinThreads ()
123 int workerThreads, completionPortThreads;
124 int workerThreads_new, completionPortThreads_new;
126 ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
127 Assert.IsTrue (workerThreads > 0, "#1");
128 Assert.IsTrue (completionPortThreads > 0, "#2");
130 workerThreads_new = workerThreads == 1 ? 2 : 1;
131 completionPortThreads_new = completionPortThreads == 1 ? 2 : 1;
132 ThreadPool.SetMinThreads (workerThreads_new, completionPortThreads_new);
134 ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
135 Assert.IsTrue (workerThreads == workerThreads_new, "#3");
136 Assert.IsTrue (completionPortThreads == completionPortThreads_new, "#4");
140 public void SetAndGetMaxThreads ()
142 int cpuCount = Environment.ProcessorCount;
143 int workerThreads, completionPortThreads;
144 int workerThreads_new, completionPortThreads_new;
146 ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
147 Assert.IsTrue (workerThreads > 0, "#1");
148 Assert.IsTrue (completionPortThreads > 0, "#2");
150 workerThreads_new = workerThreads == cpuCount ? cpuCount + 1 : cpuCount;
151 completionPortThreads_new = completionPortThreads == cpuCount ? cpuCount + 1 : cpuCount;
152 ThreadPool.SetMaxThreads (workerThreads_new, completionPortThreads_new);
154 ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
155 Assert.IsTrue (workerThreads == workerThreads_new, "#3");
156 Assert.IsTrue (completionPortThreads == completionPortThreads_new, "#4");
160 public void GetAvailableThreads ()
162 ManualResetEvent mre = new ManualResetEvent (false);
163 DateTime start = DateTime.Now;
164 int i, workerThreads, completionPortThreads;
167 Assert.IsTrue (ThreadPool.SetMaxThreads (Environment.ProcessorCount, Environment.ProcessorCount));
170 ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
171 if (workerThreads == 0)
174 Console.WriteLine ("workerThreads = {0}, completionPortThreads = {1}", workerThreads, completionPortThreads);
176 if ((DateTime.Now - start).TotalSeconds >= 10)
177 Assert.Fail ("did not reach 0 available threads");
179 ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
187 void GetAvailableThreads_Callback (object state)
189 ManualResetEvent mre = (ManualResetEvent) state;
194 ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
195 ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
196 ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
197 ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);