CollectionStressTestHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if NET_4_0
  2. //
  3. // CollectionStressTestHelper.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  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
  26. // THE SOFTWARE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. using System.Threading;
  32. using System.Linq;
  33. using MonoTests.System.Threading.Tasks;
  34. using NUnit;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Collections.Concurrent
  37. {
  38. public enum CheckOrderingType {
  39. InOrder,
  40. Reversed,
  41. DontCare
  42. }
  43. public static class CollectionStressTestHelper
  44. {
  45. public static void AddStressTest (IProducerConsumerCollection<int> coll)
  46. {
  47. ParallelTestHelper.Repeat (delegate {
  48. int amount = -1;
  49. const int count = 10;
  50. const int threads = 5;
  51. ParallelTestHelper.ParallelStressTest (coll, (q) => {
  52. int t = Interlocked.Increment (ref amount);
  53. for (int i = 0; i < count; i++)
  54. coll.TryAdd (t);
  55. }, threads);
  56. Assert.AreEqual (threads * count, coll.Count, "#-1");
  57. int[] values = new int[threads];
  58. int temp;
  59. while (coll.TryTake (out temp)) {
  60. values[temp]++;
  61. }
  62. for (int i = 0; i < threads; i++)
  63. Assert.AreEqual (count, values[i], "#" + i);
  64. });
  65. }
  66. public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
  67. {
  68. ParallelTestHelper.Repeat (delegate {
  69. const int count = 10;
  70. const int threads = 5;
  71. const int delta = 5;
  72. for (int i = 0; i < (count + delta) * threads; i++)
  73. coll.TryAdd (i);
  74. bool state = true;
  75. ParallelTestHelper.ParallelStressTest (coll, (q) => {
  76. int t;
  77. for (int i = 0; i < count; i++)
  78. state &= coll.TryTake (out t);
  79. }, threads);
  80. Assert.IsTrue (state, "#1");
  81. Assert.AreEqual (delta * threads, coll.Count, "#2");
  82. string actual = string.Empty;
  83. int temp;
  84. while (coll.TryTake (out temp)) {
  85. actual += temp.ToString ();;
  86. }
  87. IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
  88. if (order == CheckOrderingType.Reversed)
  89. range = range.Reverse ();
  90. string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
  91. if (order == CheckOrderingType.DontCare)
  92. CollectionAssert.AreEquivalent (expected, actual, "#3");
  93. else
  94. Assert.AreEqual (expected, actual, "#3");
  95. });
  96. }
  97. }
  98. }
  99. #endif