3 // CollectionStressTestHelper.cs
6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // 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;
35 using MonoTests.System.Threading.Tasks;
38 using NUnit.Framework;
40 namespace MonoTests.System.Collections.Concurrent
42 public enum CheckOrderingType {
48 public static class CollectionStressTestHelper
50 public static void AddStressTest (IProducerConsumerCollection<int> coll)
52 ParallelTestHelper.Repeat (delegate {
55 const int threads = 5;
57 ParallelTestHelper.ParallelStressTest (coll, (q) => {
58 int t = Interlocked.Increment (ref amount);
59 for (int i = 0; i < count; i++)
63 Assert.AreEqual (threads * count, coll.Count, "#-1");
64 int[] values = new int[threads];
66 while (coll.TryTake (out temp)) {
70 for (int i = 0; i < threads; i++)
71 Assert.AreEqual (count, values[i], "#" + i);
75 public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
77 ParallelTestHelper.Repeat (delegate {
80 const int threads = 5;
83 for (int i = 0; i < (count + delta) * threads; i++)
84 while (!coll.TryAdd (i));
88 Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
90 ParallelTestHelper.ParallelStressTest (coll, (q) => {
94 for (int i = 0; i < count; i++) {
95 s &= coll.TryTake (out t);
96 // try again in case it was a transient failure
97 if (!s && coll.TryTake (out t))
105 Assert.IsTrue (state, "#1");
106 Assert.AreEqual (delta * threads, coll.Count, "#2");
108 string actual = string.Empty;
110 while (coll.TryTake (out temp)) {
111 actual += temp.ToString ();;
114 IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
115 if (order == CheckOrderingType.Reversed)
116 range = range.Reverse ();
118 string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
120 if (order == CheckOrderingType.DontCare)
121 CollectionAssert.AreEquivalent (expected, actual, "#3");
123 Assert.AreEqual (expected, actual, "#3");