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 [Ignore ("Not implemented yet")]
39 public class ConcurrentExclusiveSchedulerPairTest
41 ConcurrentExclusiveSchedulerPair schedPair;
45 public void BasicExclusiveUsageTest ()
47 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
48 factory = new TaskFactory (schedPair.ExclusiveScheduler);
50 bool launched = false;
51 factory.StartNew (() => launched = true);
54 Assert.IsTrue (launched);
58 public void BasicConcurrentUsageTest ()
60 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
61 factory = new TaskFactory (schedPair.ConcurrentScheduler);
63 bool launched = false;
64 factory.StartNew (() => launched = true);
67 Assert.IsTrue (launched);
71 public void ExclusiveUsageTest ()
73 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
74 factory = new TaskFactory (schedPair.ExclusiveScheduler);
77 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
78 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
80 factory.StartNew (() => {
82 Interlocked.Increment (ref count);
86 factory.StartNew (() => Interlocked.Increment (ref count));
89 Assert.AreEqual (1, count);
94 public void ConcurrentUsageTest ()
96 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
97 factory = new TaskFactory (schedPair.ConcurrentScheduler);
100 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
101 CountdownEvent cntd = new CountdownEvent (2);
103 factory.StartNew (() => {
104 Interlocked.Increment (ref count);
108 factory.StartNew (() => {
109 Interlocked.Increment (ref count);
115 Assert.AreEqual (2, count);
120 public void ConcurrentUsageWithExclusiveExecutingTest ()
122 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
123 TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
124 TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
127 bool exclStarted = false;
128 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
129 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
131 exclFact.StartNew (() => {
140 concFact.StartNew (() => Interlocked.Increment (ref count));
141 concFact.StartNew (() => Interlocked.Increment (ref count));
144 Assert.IsTrue (exclStarted);
145 Assert.AreEqual (0, count);
150 public void ExclusiveUsageWithConcurrentExecutingTest ()
152 schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
153 TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
154 TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
157 bool started = false;
158 ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
159 ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
161 concFact.StartNew (() => {
170 exclFact.StartNew (() => Interlocked.Increment (ref count));
173 Assert.IsTrue (started);
174 Assert.AreEqual (0, count);