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;
31 using NUnit.Framework;
33 namespace ParallelFxTests
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 ConsumingEnumerableTestCase()
145 defaultCollection.Add(1);
146 defaultCollection.Add(2);
147 defaultCollection.Add(3);
148 defaultCollection.Add(4);
149 defaultCollection.Add(5);
150 defaultCollection.Add(6);
151 defaultCollection.CompleteAdding ();
153 IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
154 Assert.IsNotNull(enumerable, "#1");
156 foreach (int j in enumerable) {
158 Assert.AreEqual(temp, j, "#" + temp);
160 Assert.AreEqual(0, defaultCollection.Count, "#" + i);