Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / ConcurrentExclusiveSchedulerPairTest.cs
1 //
2 // ConcurrentExclusiveSchedulerPairTest.cs
3 //
4 // Author:
5 //       Jérémie "garuma" Laval <jeremie.laval@gmail.com>
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
27 #if NET_4_5
28
29 using System;
30 using System.Threading;
31 using System.Threading.Tasks;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Threading.Tasks
36 {
37         [TestFixture]
38         [Ignore ("Not implemented yet")]
39         public class ConcurrentExclusiveSchedulerPairTest
40         {
41                 ConcurrentExclusiveSchedulerPair schedPair;
42                 TaskFactory factory;
43
44                 [Test]
45                 public void BasicExclusiveUsageTest ()
46                 {
47                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
48                         factory = new TaskFactory (schedPair.ExclusiveScheduler);
49
50                         bool launched = false;
51                         factory.StartNew (() => launched = true);
52                         Thread.Sleep (600);
53
54                         Assert.IsTrue (launched);
55                 }
56
57                 [Test]
58                 public void BasicConcurrentUsageTest ()
59                 {
60                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
61                         factory = new TaskFactory (schedPair.ConcurrentScheduler);
62
63                         bool launched = false;
64                         factory.StartNew (() => launched = true);
65                         Thread.Sleep (600);
66
67                         Assert.IsTrue (launched);
68                 }
69
70                 [Test]
71                 public void ExclusiveUsageTest ()
72                 {
73                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
74                         factory = new TaskFactory (schedPair.ExclusiveScheduler);
75
76                         int count = 0;
77                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
78                         ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
79
80                         factory.StartNew (() => {
81                                         mreStart.Set ();
82                                         Interlocked.Increment (ref count);
83                                         mreFinish.Wait ();
84                                 });
85                         mreStart.Wait ();
86                         factory.StartNew (() => Interlocked.Increment (ref count));
87                         Thread.Sleep (100);
88
89                         Assert.AreEqual (1, count);
90                         mreFinish.Set ();
91                 }
92
93                 [Test]
94                 public void ConcurrentUsageTest ()
95                 {
96                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
97                         factory = new TaskFactory (schedPair.ConcurrentScheduler);
98
99                         int count = 0;
100                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
101                         CountdownEvent cntd = new CountdownEvent (2);
102
103                         factory.StartNew (() => {
104                                         Interlocked.Increment (ref count);
105                                         cntd.Signal ();
106                                         mreFinish.Wait ();
107                                 });
108                         factory.StartNew (() => {
109                                         Interlocked.Increment (ref count);
110                                         cntd.Signal ();
111                                         mreFinish.Wait ();
112                                 });
113
114                         cntd.Wait ();
115                         Assert.AreEqual (2, count);
116                         mreFinish.Set ();
117                 }
118
119                 [Test]
120                 public void ConcurrentUsageWithExclusiveExecutingTest ()
121                 {
122                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
123                         TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
124                         TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
125
126                         int count = 0;
127                         bool exclStarted = false;
128                         ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
129                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
130
131                         exclFact.StartNew (() => {
132                                 exclStarted = true;
133                                 mreStart.Set ();
134                                 mreFinish.Wait ();
135                                 exclStarted = false;
136                         });
137
138                         mreStart.Wait ();
139
140                         concFact.StartNew (() => Interlocked.Increment (ref count));
141                         concFact.StartNew (() => Interlocked.Increment (ref count));
142                         Thread.Sleep (100);
143
144                         Assert.IsTrue (exclStarted);
145                         Assert.AreEqual (0, count);
146                         mreFinish.Set ();
147                 }
148
149                 [Test]
150                 public void ExclusiveUsageWithConcurrentExecutingTest ()
151                 {
152                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
153                         TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
154                         TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
155
156                         int count = 0;
157                         bool started = false;
158                         ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
159                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
160
161                         concFact.StartNew (() => {
162                                 started = true;
163                                 mreStart.Set ();
164                                 mreFinish.Wait ();
165                                 started = false;
166                         });
167
168                         mreStart.Wait ();
169
170                         exclFact.StartNew (() => Interlocked.Increment (ref count));
171                         Thread.Sleep (100);
172
173                         Assert.IsTrue (started);
174                         Assert.AreEqual (0, count);
175                         mreFinish.Set ();
176                 }
177         }
178 }
179
180 #endif