[Profiler] Use the correct function to increment the refcount
[mono.git] / mcs / class / System / Test / System.Collections.Concurrent / BlockingCollectionTests.cs
1 // BlockingCollectionTests.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 //
24
25 using System;
26 using System.Threading;
27 using System.Collections.Concurrent;
28 using System.Collections.Generic;
29 using System.Threading.Tasks;
30
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Collections.Concurrent
34 {
35         [TestFixture()]
36         public class BlockingCollectionTests
37         {
38                 BlockingCollection<int> defaultCollection;
39                 BlockingCollection<int> boundedCollection;
40                 
41                 [SetUpAttribute]
42                 public void Setup()
43                 {
44                         defaultCollection = new BlockingCollection<int>();
45                         boundedCollection = new BlockingCollection<int>(10);
46                 }
47                 
48                 [TestAttribute]
49                 public void DefaultAddTestCase()
50                 {
51                         defaultCollection.Add(1);
52                         defaultCollection.Add(2);
53                         Assert.AreEqual(2, defaultCollection.Count, "#1");
54
55                 }
56                 
57                 [TestAttribute]
58                 public void BoundedAddTestCase()
59                 {
60                         boundedCollection.Add(1);
61                         boundedCollection.Add(2);
62                         Assert.AreEqual(2, boundedCollection.Count, "#1");
63                 }
64                 
65                 [TestAttribute]
66                 public void BoundedIsFullTestCase()
67                 {
68                         boundedCollection.Add(1);
69                         boundedCollection.Add(2);
70                         boundedCollection.Add(3);
71                         boundedCollection.Add(4);
72                         boundedCollection.Add(5);
73                         boundedCollection.Add(6);
74                         boundedCollection.Add(7);
75                         boundedCollection.Add(8);
76                         boundedCollection.Add(9);
77                         boundedCollection.Add(10);
78                         Assert.AreEqual(boundedCollection.BoundedCapacity, boundedCollection.Count, "#1");
79                 }
80                 
81                 [TestAttribute]
82                 public void TakeTestCase()
83                 {
84                         defaultCollection.Add(1);
85                         defaultCollection.Add(2);
86                         boundedCollection.Add(1);
87                         boundedCollection.Add(2);
88                         
89                         int value = defaultCollection.Take();
90                         Assert.AreEqual(1, value, "#1");
91                         value = boundedCollection.Take();
92                         Assert.AreEqual(1, value, "#2");
93                 }
94                 
95                 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
96                 public void DefaultAddCompletedTestCase()
97                 {
98                         defaultCollection.Add(1);
99                         defaultCollection.Add(2);
100                         defaultCollection.CompleteAdding();
101                         Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
102                         
103                         defaultCollection.Add(3);
104                 }
105                 
106                 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
107                 public void BoundedAddCompletedTestCase()
108                 {
109                         boundedCollection.Add(1);
110                         boundedCollection.Add(2);
111                         boundedCollection.Add(3);
112                         boundedCollection.Add(4);
113                         boundedCollection.Add(5);
114                         boundedCollection.Add(6);
115                         boundedCollection.Add(7);
116                         boundedCollection.Add(8);
117                         boundedCollection.Add(9);
118                         boundedCollection.Add(10);
119                         boundedCollection.CompleteAdding();
120                         Assert.IsTrue(boundedCollection.IsAddingCompleted, "#1");
121                         
122                         boundedCollection.Add(3);
123                 }
124                 
125                 [TestAttribute]
126                 public void IsCompletedTestCase()
127                 {
128                         defaultCollection.Add(1);
129                         defaultCollection.Add(2);
130                         
131                         defaultCollection.CompleteAdding();
132                         Assert.IsFalse(defaultCollection.IsCompleted, "#3");
133                         
134                         defaultCollection.Take();
135                         defaultCollection.Take();
136                         
137                         Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
138                         Assert.AreEqual(0, defaultCollection.Count, "#2");
139                         Assert.IsTrue(defaultCollection.IsCompleted, "#4");
140                 }
141                 
142                 [TestAttribute]
143                 public void IsCompletedEmptyTestCase ()
144                 {
145                         defaultCollection.CompleteAdding ();
146                         Assert.IsTrue (defaultCollection.IsCompleted);
147                 }
148
149                 [TestAttribute]
150                 public void ConsumingEnumerableTestCase()
151                 {
152                         defaultCollection.Add(1);
153                         defaultCollection.Add(2);
154                         defaultCollection.Add(3);
155                         defaultCollection.Add(4);
156                         defaultCollection.Add(5);
157                         defaultCollection.Add(6);
158                         defaultCollection.CompleteAdding ();
159                         
160                         IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
161                         Assert.IsNotNull(enumerable, "#1");
162                         int i = 1;
163                         foreach (int j in enumerable) {
164                                 int temp = i++;
165                                 Assert.AreEqual(temp, j, "#" + temp);
166                         }
167                         Assert.AreEqual(0, defaultCollection.Count, "#" + i);
168                 }
169
170                 [TestAttribute]
171                 public void TryTakeTestCase ()
172                 {
173                         defaultCollection.Add (1);
174
175                         int value = default (int);
176                         bool firstTake = defaultCollection.TryTake (out value);
177                         int value2 = default (int);
178                         bool secondTake = defaultCollection.TryTake (out value2);
179
180                         Assert.AreEqual (1, value);
181                         Assert.IsTrue (firstTake);
182                         Assert.AreEqual (default (int), value2);
183                         Assert.IsFalse (secondTake);
184                 }
185
186                 [Test]
187                 public void EmptyTryTakeWithTimeout ()
188                 {
189                         object o = null;
190                         var queue = new BlockingCollection<object> ();
191                         bool success = queue.TryTake (out o, 500);
192                         Assert.IsNull (o);
193                         Assert.IsFalse (success);
194                 }
195
196                 [Test]
197                 public void TakeAnyFromSecondCollection ()
198                 {
199                         var a = new BlockingCollection<string> ();
200                         var b = new BlockingCollection<string> ();
201                         var arr = new [] { a, b };
202                         string res = null;
203
204                         Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
205                         a.Add ("foo");
206                         Assert.AreEqual (0, t.Result, "#1");
207                         Assert.AreEqual ("foo", res, "#2");
208
209                         t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
210                         b.Add ("bar");
211                         Assert.AreEqual (1, t.Result, "#3");
212                         Assert.AreEqual ("bar", res, "#4");
213                 }
214
215                 [Test]
216                 public void TakeAnyCancellable ()
217                 {
218                         var a = new BlockingCollection<string> ();
219                         var b = new BlockingCollection<string> ();
220                         var arr = new [] { a, b };
221                         var cts = new CancellationTokenSource ();
222                         string res = null;
223
224                         Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
225                         Thread.Sleep (100);
226                         a.Add ("foo");
227                         Assert.AreEqual (0, t.Result, "#1");
228                         Assert.AreEqual ("foo", res, "#2");
229
230                         t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
231                         Thread.Sleep (100);
232                         b.Add ("bar");
233                         Assert.AreEqual (1, t.Result, "#3");
234                         Assert.AreEqual ("bar", res, "#4");
235
236                         t = Task.Factory.StartNew (() => {
237                                 try {
238                                         return BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token);
239                                 } catch (OperationCanceledException) {
240                                         res = "canceled";
241                                         return -10;
242                                 }
243                         });
244
245                         cts.Cancel ();
246                         Assert.AreEqual (-10, t.Result, "#5");
247                         Assert.AreEqual ("canceled", res, "#6");
248                 }
249
250                 [Test, ExpectedException (typeof(OperationCanceledException))]
251                 public void BoundedAddLimit ()
252                 {
253                         const int elNumber = 5;
254
255                         var c = new BlockingCollection <int> (elNumber);
256                         var token = new CancellationTokenSource (100);
257
258                         for (var i = 0; i < elNumber + 1; i++) {
259                                 c.Add (1, token.Token);
260                         }
261                 }
262
263                 [Test]
264                 public void AddAnyCancellable ()
265                 {
266                         const int elNumber = 5;
267                         const int colNumber = 5;
268
269                         var cols = new BlockingCollection <int> [colNumber];
270                         for (var i = 0; i < colNumber; i++) {
271                                 cols[i] = new BlockingCollection <int> (elNumber);
272                         }
273
274                         var token = new CancellationTokenSource (100);
275                         for (var i = 0; i < colNumber * elNumber; i++) {
276                                 BlockingCollection <int>.AddToAny (cols, 1, token.Token);
277                         }
278
279                         foreach (var col in cols) {
280                                 Assert.AreEqual (elNumber, col.Count);
281                         }
282                 }
283         }
284 }