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