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
30 using System.Threading;
31 using System.Threading.Tasks;
33 using NUnit.Framework;
35 namespace MonoTests.System.Threading.Tasks
38 public class ConcurrentExclusiveSchedulerPairTest
40 ConcurrentExclusiveSchedulerPair schedPair;
44 public void BasicExclusiveUsageTest ()
46 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
47 factory = new TaskFactory (schedPair.ExclusiveScheduler);
49 bool launched = false;
50 factory.StartNew (() => launched = true);
53 Assert.IsTrue (launched);
57 public void BasicConcurrentUsageTest ()
59 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
60 factory = new TaskFactory (schedPair.ConcurrentScheduler);
62 bool launched = false;
63 factory.StartNew (() => launched = true);
66 Assert.IsTrue (launched);
70 public void ExclusiveUsageTest ()
72 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
73 factory = new TaskFactory (schedPair.ExclusiveScheduler);
76 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
77 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
79 factory.StartNew (() => {
81 Interlocked.Increment (ref count);
85 factory.StartNew (() => Interlocked.Increment (ref count));
88 Assert.AreEqual (1, count);
93 public void ConcurrentUsageTest ()
95 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
96 factory = new TaskFactory (schedPair.ConcurrentScheduler);
99 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
100 CountdownEvent cntd = new CountdownEvent (2);
102 factory.StartNew (() => {
103 Interlocked.Increment (ref count);
107 factory.StartNew (() => {
108 Interlocked.Increment (ref count);
114 Assert.AreEqual (2, count);
119 public void ConcurrentUsageWithExclusiveExecutingTest ()
121 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
122 TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
123 TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
126 bool exclStarted = false;
127 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
128 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
130 exclFact.StartNew (() => {
139 concFact.StartNew (() => Interlocked.Increment (ref count));
140 concFact.StartNew (() => Interlocked.Increment (ref count));
143 Assert.IsTrue (exclStarted);
144 Assert.AreEqual (0, count);
149 public void ExclusiveUsageWithConcurrentExecutingTest ()
151 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
152 TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
153 TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
156 bool started = false;
157 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
158 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
160 concFact.StartNew (() => {
169 exclFact.StartNew (() => Interlocked.Increment (ref count));
172 Assert.IsTrue (started);
173 Assert.AreEqual (0, count);