1 // BlockingCollectionTests.cs
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
26 using System.Threading;
27 using System.Collections.Concurrent;
28 using System.Collections.Generic;
29 using System.Threading.Tasks;
31 using NUnit.Framework;
33 namespace MonoTests.System.Collections.Concurrent
36 public class BlockingCollectionTests
38 BlockingCollection<int> defaultCollection;
39 BlockingCollection<int> boundedCollection;
44 defaultCollection = new BlockingCollection<int>();
45 boundedCollection = new BlockingCollection<int>(10);
49 public void DefaultAddTestCase()
51 defaultCollection.Add(1);
52 defaultCollection.Add(2);
53 Assert.AreEqual(2, defaultCollection.Count, "#1");
58 public void BoundedAddTestCase()
60 boundedCollection.Add(1);
61 boundedCollection.Add(2);
62 Assert.AreEqual(2, boundedCollection.Count, "#1");
66 public void BoundedIsFullTestCase()
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");
82 public void TakeTestCase()
84 defaultCollection.Add(1);
85 defaultCollection.Add(2);
86 boundedCollection.Add(1);
87 boundedCollection.Add(2);
89 int value = defaultCollection.Take();
90 Assert.AreEqual(1, value, "#1");
91 value = boundedCollection.Take();
92 Assert.AreEqual(1, value, "#2");
95 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
96 public void DefaultAddCompletedTestCase()
98 defaultCollection.Add(1);
99 defaultCollection.Add(2);
100 defaultCollection.CompleteAdding();
101 Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
103 defaultCollection.Add(3);
106 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
107 public void BoundedAddCompletedTestCase()
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");
122 boundedCollection.Add(3);
126 public void IsCompletedTestCase()
128 defaultCollection.Add(1);
129 defaultCollection.Add(2);
131 defaultCollection.CompleteAdding();
132 Assert.IsFalse(defaultCollection.IsCompleted, "#3");
134 defaultCollection.Take();
135 defaultCollection.Take();
137 Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
138 Assert.AreEqual(0, defaultCollection.Count, "#2");
139 Assert.IsTrue(defaultCollection.IsCompleted, "#4");
143 public void IsCompletedEmptyTestCase ()
145 defaultCollection.CompleteAdding ();
146 Assert.IsTrue (defaultCollection.IsCompleted);
150 public void ConsumingEnumerableTestCase()
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 ();
160 IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
161 Assert.IsNotNull(enumerable, "#1");
163 foreach (int j in enumerable) {
165 Assert.AreEqual(temp, j, "#" + temp);
167 Assert.AreEqual(0, defaultCollection.Count, "#" + i);
171 public void TryTakeTestCase ()
173 defaultCollection.Add (1);
175 int value = default (int);
176 bool firstTake = defaultCollection.TryTake (out value);
177 int value2 = default (int);
178 bool secondTake = defaultCollection.TryTake (out value2);
180 Assert.AreEqual (1, value);
181 Assert.IsTrue (firstTake);
182 Assert.AreEqual (default (int), value2);
183 Assert.IsFalse (secondTake);
187 public void EmptyTryTakeWithTimeout ()
190 var queue = new BlockingCollection<object> ();
191 bool success = queue.TryTake (out o, 500);
193 Assert.IsFalse (success);
197 public void TakeAnyFromSecondCollection ()
199 var a = new BlockingCollection<string> ();
200 var b = new BlockingCollection<string> ();
201 var arr = new [] { a, b };
204 Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
206 Assert.AreEqual (0, t.Result, "#1");
207 Assert.AreEqual ("foo", res, "#2");
209 t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
211 Assert.AreEqual (1, t.Result, "#3");
212 Assert.AreEqual ("bar", res, "#4");
216 public void TakeAnyCancellable ()
218 var a = new BlockingCollection<string> ();
219 var b = new BlockingCollection<string> ();
220 var arr = new [] { a, b };
221 var cts = new CancellationTokenSource ();
224 Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
227 Assert.AreEqual (0, t.Result, "#1");
228 Assert.AreEqual ("foo", res, "#2");
230 t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
233 Assert.AreEqual (1, t.Result, "#3");
234 Assert.AreEqual ("bar", res, "#4");
236 t = Task.Factory.StartNew (() => {
238 return BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token);
239 } catch (OperationCanceledException) {
246 Assert.AreEqual (-10, t.Result, "#5");
247 Assert.AreEqual ("canceled", res, "#6");
250 [Test, ExpectedException (typeof(OperationCanceledException))]
251 public void BoundedAddLimit ()
253 const int elNumber = 5;
255 var c = new BlockingCollection <int> (elNumber);
256 var token = new CancellationTokenSource (100);
258 for (var i = 0; i < elNumber + 1; i++) {
259 c.Add (1, token.Token);
264 public void AddAnyCancellable ()
266 const int elNumber = 5;
267 const int colNumber = 5;
269 var cols = new BlockingCollection <int> [colNumber];
270 for (var i = 0; i < colNumber; i++) {
271 cols[i] = new BlockingCollection <int> (elNumber);
274 var token = new CancellationTokenSource (100);
275 for (var i = 0; i < colNumber * elNumber; i++) {
276 BlockingCollection <int>.AddToAny (cols, 1, token.Token);
279 foreach (var col in cols) {
280 Assert.AreEqual (elNumber, col.Count);