[sgen] Make sure we don't sweep a block if we're not supposed to
[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         public class ConcurrentExclusiveSchedulerPairTest
39         {
40                 ConcurrentExclusiveSchedulerPair schedPair;
41                 TaskFactory factory;
42
43                 [Test]
44                 public void BasicExclusiveUsageTest ()
45                 {
46                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
47                         factory = new TaskFactory (schedPair.ExclusiveScheduler);
48
49                         bool launched = false;
50                         factory.StartNew (() => launched = true);
51                         Thread.Sleep (600);
52
53                         Assert.IsTrue (launched);
54                 }
55
56                 [Test]
57                 public void BasicConcurrentUsageTest ()
58                 {
59                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
60                         factory = new TaskFactory (schedPair.ConcurrentScheduler);
61
62                         bool launched = false;
63                         factory.StartNew (() => launched = true);
64                         Thread.Sleep (600);
65
66                         Assert.IsTrue (launched);
67                 }
68
69                 [Test]
70                 public void ExclusiveUsageTest ()
71                 {
72                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
73                         factory = new TaskFactory (schedPair.ExclusiveScheduler);
74
75                         int count = 0;
76                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
77                         ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
78
79                         factory.StartNew (() => {
80                                         mreStart.Set ();
81                                         Interlocked.Increment (ref count);
82                                         mreFinish.Wait ();
83                                 });
84                         mreStart.Wait ();
85                         factory.StartNew (() => Interlocked.Increment (ref count));
86                         Thread.Sleep (100);
87
88                         Assert.AreEqual (1, count);
89                         mreFinish.Set ();
90                 }
91
92                 [Test]
93                 public void ConcurrentUsageTest ()
94                 {
95                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
96                         factory = new TaskFactory (schedPair.ConcurrentScheduler);
97
98                         int count = 0;
99                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
100                         CountdownEvent cntd = new CountdownEvent (2);
101
102                         factory.StartNew (() => {
103                                         Interlocked.Increment (ref count);
104                                         cntd.Signal ();
105                                         mreFinish.Wait ();
106                                 });
107                         factory.StartNew (() => {
108                                         Interlocked.Increment (ref count);
109                                         cntd.Signal ();
110                                         mreFinish.Wait ();
111                                 });
112
113                         cntd.Wait ();
114                         Assert.AreEqual (2, count);
115                         mreFinish.Set ();
116                 }
117
118                 [Test]
119                 public void ConcurrentUsageWithExclusiveExecutingTest ()
120                 {
121                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
122                         TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
123                         TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
124
125                         int count = 0;
126                         bool exclStarted = false;
127                         ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
128                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
129
130                         exclFact.StartNew (() => {
131                                 exclStarted = true;
132                                 mreStart.Set ();
133                                 mreFinish.Wait ();
134                                 exclStarted = false;
135                         });
136
137                         mreStart.Wait ();
138
139                         concFact.StartNew (() => Interlocked.Increment (ref count));
140                         concFact.StartNew (() => Interlocked.Increment (ref count));
141                         Thread.Sleep (100);
142
143                         Assert.IsTrue (exclStarted);
144                         Assert.AreEqual (0, count);
145                         mreFinish.Set ();
146                 }
147
148                 [Test]
149                 public void ExclusiveUsageWithConcurrentExecutingTest ()
150                 {
151                         schedPair = new ConcurrentExclusiveSchedulerPair (TaskScheduler.Default, 4);
152                         TaskFactory exclFact = new TaskFactory (schedPair.ExclusiveScheduler);
153                         TaskFactory concFact = new TaskFactory (schedPair.ConcurrentScheduler);
154
155                         int count = 0;
156                         bool started = false;
157                         ManualResetEventSlim mreStart = new ManualResetEventSlim (false);
158                         ManualResetEventSlim mreFinish = new ManualResetEventSlim (false);
159
160                         concFact.StartNew (() => {
161                                 started = true;
162                                 mreStart.Set ();
163                                 mreFinish.Wait ();
164                                 started = false;
165                         });
166
167                         mreStart.Wait ();
168
169                         exclFact.StartNew (() => Interlocked.Increment (ref count));
170                         Thread.Sleep (100);
171
172                         Assert.IsTrue (started);
173                         Assert.AreEqual (0, count);
174                         mreFinish.Set ();
175                 }
176         }
177 }
178
179 #endif