2 // ConcurrentExclusiveSchedulerPairTest.cs
5 // Jérémie "garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2011 Jérémie "garuma" Laval
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
29 using System.Threading;
30 using System.Threading.Tasks;
32 using NUnit.Framework;
34 namespace MonoTests.System.Threading.Tasks
37 public class ConcurrentExclusiveSchedulerPairTest
39 ConcurrentExclusiveSchedulerPair schedPair;
43 public void BasicExclusiveUsageTest ()
45 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
46 factory = new TaskFactory (schedPair.ExclusiveScheduler);
48 bool launched = false;
49 factory.StartNew (() => launched = true);
52 Assert.IsTrue (launched);
56 public void BasicConcurrentUsageTest ()
58 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
59 factory = new TaskFactory (schedPair.ConcurrentScheduler);
61 bool launched = false;
62 factory.StartNew (() => launched = true);
65 Assert.IsTrue (launched);
69 public void ExclusiveUsageTest ()
71 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
72 factory = new TaskFactory (schedPair.ExclusiveScheduler);
75 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
76 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
78 factory.StartNew (() => {
80 Interlocked.Increment (ref count);
84 factory.StartNew (() => Interlocked.Increment (ref count));
87 Assert.AreEqual (1, count);
92 public void ConcurrentUsageTest ()
94 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
95 factory = new TaskFactory (schedPair.ConcurrentScheduler);
98 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
99 CountdownEvent cntd = new CountdownEvent (2);
101 factory.StartNew (() => {
102 Interlocked.Increment (ref count);
106 factory.StartNew (() => {
107 Interlocked.Increment (ref count);
113 Assert.AreEqual (2, count);
118 public void ConcurrentUsageWithExclusiveExecutingTest ()
120 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
121 TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
122 TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
125 bool exclStarted = false;
126 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
127 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
129 exclFact.StartNew (() => {
138 concFact.StartNew (() => Interlocked.Increment (ref count));
139 concFact.StartNew (() => Interlocked.Increment (ref count));
142 Assert.IsTrue (exclStarted);
143 Assert.AreEqual (0, count);
148 public void ExclusiveUsageWithConcurrentExecutingTest ()
150 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
151 TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
152 TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
155 bool started = false;
156 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
157 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
159 concFact.StartNew (() => {
168 exclFact.StartNew (() => Interlocked.Increment (ref count));
171 Assert.IsTrue (started);
172 Assert.AreEqual (0, count);