2 // BlockingCollectionTests.cs
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Threading;
28 using System.Collections.Concurrent;
29 using System.Collections.Generic;
30 using System.Threading.Tasks;
32 using NUnit.Framework;
34 namespace MonoTests.System.Collections.Concurrent
37 public class BlockingCollectionTests
39 BlockingCollection<int> defaultCollection;
40 BlockingCollection<int> boundedCollection;
45 defaultCollection = new BlockingCollection<int>();
46 boundedCollection = new BlockingCollection<int>(10);
50 public void DefaultAddTestCase()
52 defaultCollection.Add(1);
53 defaultCollection.Add(2);
54 Assert.AreEqual(2, defaultCollection.Count, "#1");
59 public void BoundedAddTestCase()
61 boundedCollection.Add(1);
62 boundedCollection.Add(2);
63 Assert.AreEqual(2, boundedCollection.Count, "#1");
67 public void BoundedIsFullTestCase()
69 boundedCollection.Add(1);
70 boundedCollection.Add(2);
71 boundedCollection.Add(3);
72 boundedCollection.Add(4);
73 boundedCollection.Add(5);
74 boundedCollection.Add(6);
75 boundedCollection.Add(7);
76 boundedCollection.Add(8);
77 boundedCollection.Add(9);
78 boundedCollection.Add(10);
79 Assert.AreEqual(boundedCollection.BoundedCapacity, boundedCollection.Count, "#1");
83 public void TakeTestCase()
85 defaultCollection.Add(1);
86 defaultCollection.Add(2);
87 boundedCollection.Add(1);
88 boundedCollection.Add(2);
90 int value = defaultCollection.Take();
91 Assert.AreEqual(1, value, "#1");
92 value = boundedCollection.Take();
93 Assert.AreEqual(1, value, "#2");
96 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
97 public void DefaultAddCompletedTestCase()
99 defaultCollection.Add(1);
100 defaultCollection.Add(2);
101 defaultCollection.CompleteAdding();
102 Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
104 defaultCollection.Add(3);
107 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
108 public void BoundedAddCompletedTestCase()
110 boundedCollection.Add(1);
111 boundedCollection.Add(2);
112 boundedCollection.Add(3);
113 boundedCollection.Add(4);
114 boundedCollection.Add(5);
115 boundedCollection.Add(6);
116 boundedCollection.Add(7);
117 boundedCollection.Add(8);
118 boundedCollection.Add(9);
119 boundedCollection.Add(10);
120 boundedCollection.CompleteAdding();
121 Assert.IsTrue(boundedCollection.IsAddingCompleted, "#1");
123 boundedCollection.Add(3);
127 public void IsCompletedTestCase()
129 defaultCollection.Add(1);
130 defaultCollection.Add(2);
132 defaultCollection.CompleteAdding();
133 Assert.IsFalse(defaultCollection.IsCompleted, "#3");
135 defaultCollection.Take();
136 defaultCollection.Take();
138 Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
139 Assert.AreEqual(0, defaultCollection.Count, "#2");
140 Assert.IsTrue(defaultCollection.IsCompleted, "#4");
144 public void IsCompletedEmptyTestCase ()
146 defaultCollection.CompleteAdding ();
147 Assert.IsTrue (defaultCollection.IsCompleted);
151 public void ConsumingEnumerableTestCase()
153 defaultCollection.Add(1);
154 defaultCollection.Add(2);
155 defaultCollection.Add(3);
156 defaultCollection.Add(4);
157 defaultCollection.Add(5);
158 defaultCollection.Add(6);
159 defaultCollection.CompleteAdding ();
161 IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
162 Assert.IsNotNull(enumerable, "#1");
164 foreach (int j in enumerable) {
166 Assert.AreEqual(temp, j, "#" + temp);
168 Assert.AreEqual(0, defaultCollection.Count, "#" + i);
172 public void TryTakeTestCase ()
174 defaultCollection.Add (1);
176 int value = default (int);
177 bool firstTake = defaultCollection.TryTake (out value);
178 int value2 = default (int);
179 bool secondTake = defaultCollection.TryTake (out value2);
181 Assert.AreEqual (1, value);
182 Assert.IsTrue (firstTake);
183 Assert.AreEqual (default (int), value2);
184 Assert.IsFalse (secondTake);
188 public void EmptyTryTakeWithTimeout ()
191 var queue = new BlockingCollection<object> ();
192 bool success = queue.TryTake (out o, 500);
194 Assert.IsFalse (success);
198 public void TakeAnyFromSecondCollection ()
200 var a = new BlockingCollection<string> ();
201 var b = new BlockingCollection<string> ();
202 var arr = new [] { a, b };
205 Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
207 Assert.AreEqual (0, t.Result, "#1");
208 Assert.AreEqual ("foo", res, "#2");
210 t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
212 Assert.AreEqual (1, t.Result, "#3");
213 Assert.AreEqual ("bar", res, "#4");
217 public void TakeAnyCancellable ()
219 var a = new BlockingCollection<string> ();
220 var b = new BlockingCollection<string> ();
221 var arr = new [] { a, b };
222 var cts = new CancellationTokenSource ();
225 Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
228 Assert.AreEqual (0, t.Result, "#1");
229 Assert.AreEqual ("foo", res, "#2");
231 t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
234 Assert.AreEqual (1, t.Result, "#3");
235 Assert.AreEqual ("bar", res, "#4");
237 t = Task.Factory.StartNew (() => {
239 return BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token);
240 } catch (OperationCanceledException WE_GOT_CANCELED) {
247 Assert.AreEqual (-10, t.Result, "#5");
248 Assert.AreEqual ("canceled", res, "#6");