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
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
34 using System.Threading;
38 using NUnit.Framework;
39 using NUnit.Framework.Constraints;
41 namespace MonoTests.System.Collections.Concurrent
44 public class ConcurrentBagTests
46 ConcurrentBag<int> bag;
51 bag = new ConcurrentBag<int> ();
55 public void BasicAddTakeTest ()
58 Assert.IsFalse (bag.IsEmpty);
59 Assert.AreEqual (1, bag.Count);
61 var array = bag.ToArray ();
62 Assert.AreEqual (1, array.Length);
63 Assert.AreEqual (1, array[0]);
66 Assert.IsTrue (bag.TryTake (out result));
67 Assert.AreEqual (1, result);
68 Assert.IsTrue (bag.IsEmpty);
72 public void BasicAddTakeFromOtherThread ()
74 var t = new Thread (() => bag.Add (1));
76 Assert.IsTrue (t.Join (300));
78 Assert.IsFalse (bag.IsEmpty);
79 Assert.AreEqual (1, bag.Count);
81 var array = bag.ToArray ();
82 Assert.AreEqual (1, array.Length);
83 Assert.AreEqual (1, array[0]);
86 Assert.IsTrue (bag.TryTake (out result));
87 Assert.AreEqual (1, result);
88 Assert.IsTrue (bag.IsEmpty);
92 public void AddFromMultipleThreadTakeFromOneThread ()
94 var threads = new Thread[10];
95 for (int i = 0; i < threads.Length; i++) {
96 threads[i] = new Thread (() => bag.Add (1));
99 foreach (var t in threads)
100 Assert.IsTrue (t.Join (200));
102 Assert.IsFalse (bag.IsEmpty);
103 Assert.AreEqual (threads.Length, bag.Count);
105 var array = bag.ToArray ();
106 Assert.AreEqual (threads.Length, array.Length);
108 Assert.That (array, new CollectionEquivalentConstraint (Enumerable.Repeat (1, 10).ToArray ()), "#1, same");
111 for (int i = 0; i < threads.Length; i++) {
112 Assert.IsTrue (bag.TryTake (out result));
113 Assert.AreEqual (1, result);
115 Assert.IsTrue (bag.IsEmpty);
119 public void AddFromOneThreadTakeFromMultiple ()
121 var threads = new Thread[10];
122 for (int i = 0; i < threads.Length; i++)
125 Assert.IsFalse (bag.IsEmpty);
126 Assert.AreEqual (threads.Length, bag.Count);
130 for (int i = 0; i < threads.Length; i++) {
132 threads[i] = new Thread (() => valid &= bag.TryTake (out result) && result == 1);
136 foreach (var t in threads)
137 Assert.IsTrue (t.Join (200));
139 Assert.IsTrue (valid, "Aggregate test");
143 public void BasicAddPeekTest ()
146 Assert.IsFalse (bag.IsEmpty);
147 Assert.AreEqual (1, bag.Count);
150 Assert.IsTrue (bag.TryPeek (out result));
151 Assert.AreEqual (1, result);
152 Assert.IsFalse (bag.IsEmpty);
156 public void BasicAddPeekFromOtherThread ()
158 var t = new Thread (() => bag.Add (1));
160 Assert.IsTrue (t.Join (300));
162 Assert.IsFalse (bag.IsEmpty);
163 Assert.AreEqual (1, bag.Count);
166 Assert.IsTrue (bag.TryPeek (out result));
167 Assert.AreEqual (1, result);
168 Assert.IsFalse (bag.IsEmpty);
172 public void AddFromOneThreadPeekFromMultiple ()
174 var threads = new Thread[10];
175 for (int i = 0; i < threads.Length; i++)
178 Assert.IsFalse (bag.IsEmpty);
179 Assert.AreEqual (threads.Length, bag.Count);
183 for (int i = 0; i < threads.Length; i++) {
185 threads[i] = new Thread (() => valid &= bag.TryPeek (out result) && result == 1);
189 foreach (var t in threads)
190 Assert.IsTrue (t.Join (200));
192 Assert.IsTrue (valid, "Aggregate test");
196 public void AddStressTest ()
198 CollectionStressTestHelper.AddStressTest (bag);
202 public void RemoveStressTest ()
204 CollectionStressTestHelper.RemoveStressTest (bag, CheckOrderingType.DontCare);