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 System.Threading.Tasks;
38 using NUnit.Framework;
39 using MonoTests.Mono.Threading.Tasks;
41 namespace MonoTests.Mono.Collections.Concurrent
43 public enum CheckOrderingType {
49 public static class CollectionStressTestHelper
51 public static void AddStressTest (IProducerConsumerCollection<int> coll)
53 ParallelTestHelper.Repeat (delegate {
56 const int threads = 5;
58 ParallelTestHelper.ParallelStressTest (coll, (q) => {
59 int t = Interlocked.Increment (ref amount);
60 for (int i = 0; i < count; i++)
64 Assert.AreEqual (threads * count, coll.Count, "#-1");
65 int[] values = new int[threads];
67 while (coll.TryTake (out temp)) {
71 for (int i = 0; i < threads; i++)
72 Assert.AreEqual (count, values[i], "#" + i);
76 public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
78 ParallelTestHelper.Repeat (delegate {
81 const int threads = 5;
84 for (int i = 0; i < (count + delta) * threads; i++)
85 while (!coll.TryAdd (i));
89 Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
91 ParallelTestHelper.ParallelStressTest (coll, (q) => {
95 for (int i = 0; i < count; i++) {
96 s &= coll.TryTake (out t);
97 // try again in case it was a transient failure
98 if (!s && coll.TryTake (out t))
106 Assert.IsTrue (state, "#1");
107 Assert.AreEqual (delta * threads, coll.Count, "#2");
109 string actual = string.Empty;
111 while (coll.TryTake (out temp)) {
112 actual += temp.ToString ();;
115 IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
116 if (order == CheckOrderingType.Reversed)
117 range = range.Reverse ();
119 string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
121 if (order == CheckOrderingType.DontCare)
122 CollectionAssert.AreEquivalent (expected, actual, "#3");
124 Assert.AreEqual (expected, actual, "#3");