RefCount.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if STRESS
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Reactive.Disposables;
  8. using System.Reflection;
  9. using System.Threading;
  10. namespace ReactiveTests.Stress.Disposables
  11. {
  12. public class RefCount
  13. {
  14. /// <summary>
  15. /// Disposes the primary disposable first, allocates a number of dependents on different threads, and disposes them on different threads.
  16. /// Ref count should reach zero, and the inner disposable should be called.
  17. /// </summary>
  18. public static void PrimaryFirst_DependentsTrigger()
  19. {
  20. Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";
  21. var rnd = new Random();
  22. for (int i = 1; i <= 100; i++)
  23. {
  24. Impl(true, false, new[] { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });
  25. Impl(true, false, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  26. Impl(true, false, Enumerable.Range(0, 10).Select(_ => rnd.Next(0, 1000)));
  27. Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
  28. }
  29. }
  30. /// <summary>
  31. /// Allocates a number of dependents on different threads, disposes them on different threads, and disposes the primary disposable last.
  32. /// Ref count should reach zero, and the inner disposable should be called.
  33. /// </summary>
  34. public static void DependentsFirst_PrimaryTrigger()
  35. {
  36. Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";
  37. var rnd = new Random();
  38. for (int i = 1; i <= 100; i++)
  39. {
  40. Impl(false, false, new[] { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });
  41. Impl(false, false, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  42. Impl(false, false, Enumerable.Range(0, 10).Select(_ => rnd.Next(0, 1000)));
  43. Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
  44. }
  45. }
  46. /// <summary>
  47. /// Allocates a number of dependents on different threads, disposes them on different threads, and disposes the primary disposable at a random time.
  48. /// Ref count should reach zero, and the inner disposable should be called.
  49. /// </summary>
  50. public static void DependentsFirst_PrimaryRandom()
  51. {
  52. Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";
  53. var rnd = new Random();
  54. for (int i = 1; i <= 100; i++)
  55. {
  56. Impl(false, true, new[] { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });
  57. Impl(false, true, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  58. Impl(false, true, Enumerable.Range(0, 10).Select(_ => rnd.Next(0, 1000)));
  59. Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
  60. }
  61. }
  62. private static void Impl(bool primaryFirst, bool primaryRandom, IEnumerable<int> nDependents)
  63. {
  64. var rand = new Random();
  65. foreach (var n in nDependents)
  66. {
  67. var e = new ManualResetEvent(false);
  68. var hasDependent = new ManualResetEvent(false);
  69. var r = new RefCountDisposable(Disposable.Create(() => { e.Set(); }));
  70. var d = default(IDisposable);
  71. if (primaryFirst)
  72. {
  73. d = r.GetDisposable();
  74. r.Dispose();
  75. }
  76. else if (primaryRandom)
  77. {
  78. var sleep = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
  79. ThreadPool.QueueUserWorkItem(_ =>
  80. {
  81. hasDependent.WaitOne();
  82. Helpers.SleepOrSpin(sleep);
  83. r.Dispose();
  84. });
  85. if (n == 0)
  86. hasDependent.Set();
  87. }
  88. Console.Write(n + " - ");
  89. var cd = new CountdownEvent(n * 2);
  90. for (int i = 0; i < n; i++)
  91. {
  92. var j = i;
  93. var sleep1 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
  94. var sleep2 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
  95. var sleep3 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
  96. ThreadPool.QueueUserWorkItem(_ =>
  97. {
  98. Helpers.SleepOrSpin(sleep1);
  99. Console.Write("+");
  100. var f = r.GetDisposable();
  101. if (j == 0)
  102. hasDependent.Set();
  103. Helpers.SleepOrSpin(sleep2);
  104. ThreadPool.QueueUserWorkItem(__ =>
  105. {
  106. Helpers.SleepOrSpin(sleep3);
  107. f.Dispose();
  108. Console.Write("-");
  109. cd.Signal();
  110. });
  111. cd.Signal();
  112. });
  113. }
  114. cd.Wait();
  115. if (primaryFirst)
  116. d.Dispose();
  117. else if (!primaryRandom)
  118. r.Dispose();
  119. e.WaitOne();
  120. Console.WriteLine(".");
  121. }
  122. }
  123. }
  124. }
  125. #endif