RefCount.cs 5.7 KB

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