2 // ConcurrentBagTests.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
33 using System.Threading;
37 using NUnit.Framework;
38 using NUnit.Framework.Constraints;
40 namespace MonoTests.System.Collections.Concurrent
43 public class ConcurrentBagTests
45 ConcurrentBag<int> bag;
50 bag = new ConcurrentBag<int> ();
54 public void BasicAddTakeTest ()
57 Assert.IsFalse (bag.IsEmpty);
58 Assert.AreEqual (1, bag.Count);
60 var array = bag.ToArray ();
61 Assert.AreEqual (1, array.Length);
62 Assert.AreEqual (1, array[0]);
65 Assert.IsTrue (bag.TryTake (out result));
66 Assert.AreEqual (1, result);
67 Assert.IsTrue (bag.IsEmpty);
71 public void BasicAddTakeFromOtherThread ()
73 var t = new Thread (() => bag.Add (1));
75 Assert.IsTrue (t.Join (300));
77 Assert.IsFalse (bag.IsEmpty);
78 Assert.AreEqual (1, bag.Count);
80 var array = bag.ToArray ();
81 Assert.AreEqual (1, array.Length);
82 Assert.AreEqual (1, array[0]);
85 Assert.IsTrue (bag.TryTake (out result));
86 Assert.AreEqual (1, result);
87 Assert.IsTrue (bag.IsEmpty);
91 public void AddFromMultipleThreadTakeFromOneThread ()
93 var threads = new Thread[10];
94 for (int i = 0; i < threads.Length; i++) {
95 threads[i] = new Thread (() => bag.Add (1));
98 foreach (var t in threads)
99 Assert.IsTrue (t.Join (2000));
101 Assert.IsFalse (bag.IsEmpty);
102 Assert.AreEqual (threads.Length, bag.Count);
104 var array = bag.ToArray ();
105 Assert.AreEqual (threads.Length, array.Length);
107 Assert.That (array, new CollectionEquivalentConstraint (Enumerable.Repeat (1, 10).ToArray ()), "#1, same");
110 for (int i = 0; i < threads.Length; i++) {
111 Assert.IsTrue (bag.TryTake (out result));
112 Assert.AreEqual (1, result);
114 Assert.IsTrue (bag.IsEmpty);
118 public void AddFromOneThreadTakeFromMultiple ()
120 var threads = new Thread[10];
121 for (int i = 0; i < threads.Length; i++)
124 Assert.IsFalse (bag.IsEmpty);
125 Assert.AreEqual (threads.Length, bag.Count);
129 for (int i = 0; i < threads.Length; i++) {
131 threads[i] = new Thread (() => valid &= bag.TryTake (out result) && result == 1);
135 foreach (var t in threads)
136 Assert.IsTrue (t.Join (200));
138 Assert.IsTrue (valid, "Aggregate test");
142 public void BasicAddPeekTest ()
145 Assert.IsFalse (bag.IsEmpty);
146 Assert.AreEqual (1, bag.Count);
149 Assert.IsTrue (bag.TryPeek (out result));
150 Assert.AreEqual (1, result);
151 Assert.IsFalse (bag.IsEmpty);
155 public void BasicAddPeekFromOtherThread ()
157 var t = new Thread (() => bag.Add (1));
159 Assert.IsTrue (t.Join (300));
161 Assert.IsFalse (bag.IsEmpty);
162 Assert.AreEqual (1, bag.Count);
165 Assert.IsTrue (bag.TryPeek (out result));
166 Assert.AreEqual (1, result);
167 Assert.IsFalse (bag.IsEmpty);
171 public void AddFromOneThreadPeekFromMultiple ()
173 var threads = new Thread[10];
174 for (int i = 0; i < threads.Length; i++)
177 Assert.IsFalse (bag.IsEmpty);
178 Assert.AreEqual (threads.Length, bag.Count);
182 for (int i = 0; i < threads.Length; i++) {
184 threads[i] = new Thread (() => valid &= bag.TryPeek (out result) && result == 1);
188 foreach (var t in threads)
189 Assert.IsTrue (t.Join (200));
191 Assert.IsTrue (valid, "Aggregate test");
195 public void BasicRemoveEmptyTest ()
198 Assert.IsTrue(bag.IsEmpty);
199 Assert.IsFalse(bag.TryTake(out result));
203 public void BasicRemoveTwiceTest()
206 Assert.IsFalse (bag.IsEmpty);
207 Assert.AreEqual (1, bag.Count);
210 Assert.IsTrue (bag.TryTake (out result));
211 Assert.AreEqual (1, result);
212 Assert.IsTrue (bag.IsEmpty);
213 Assert.IsFalse (bag.TryTake (out result));
214 Assert.IsFalse (bag.TryTake (out result));
218 public void AddRemoveAddTest()
221 Assert.IsFalse (bag.IsEmpty);
222 Assert.AreEqual (1, bag.Count);
225 Assert.IsTrue (bag.TryTake (out result));
226 Assert.AreEqual (1, result);
227 Assert.IsTrue (bag.IsEmpty);
230 Assert.IsFalse (bag.IsEmpty);
231 Assert.AreEqual (1, bag.Count);
233 Assert.IsTrue (bag.TryTake (out result));
234 Assert.AreEqual (1, result);
235 Assert.IsTrue (bag.IsEmpty);
239 public void AddStressTest ()
241 CollectionStressTestHelper.AddStressTest (bag);
245 public void RemoveStressTest ()
247 CollectionStressTestHelper.RemoveStressTest (bag, CheckOrderingType.DontCare);
251 public void Bug24213 ()
254 var bag = new ConcurrentBag<int> ();
255 for (int i = 0; i < size; i++)
258 var array = bag.ToArray ();
260 Assert.AreEqual (size, array.Length, "#1");
262 for (int i = 0; i < size; i++)
263 Assert.AreEqual (size - 1 - i, array [i], "#C" + i);