Delay.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #define DEBUG // For debug.writeline
  5. using System;
  6. using System.Diagnostics;
  7. using System.Reactive.Concurrency;
  8. using System.Reactive.Linq;
  9. namespace ReactiveTests.Stress.Linq
  10. {
  11. public class Delay
  12. {
  13. /// <summary>
  14. /// Tests OnError messages are propagated all the time.
  15. /// </summary>
  16. public static void Errors()
  17. {
  18. while (true)
  19. {
  20. foreach (var N in new[] { 1, 10, 100, 1000, 10000, 100000 })
  21. {
  22. Debug.WriteLine("N = {0}", N);
  23. foreach (var d in new[] { 1, 10, 20, 50, 100, 200, 250, 500 })
  24. {
  25. try
  26. {
  27. var ex = new Exception();
  28. Observable.Range(0, N, NewThreadScheduler.Default).Concat(Observable.Throw<int>(ex)).Delay(TimeSpan.FromMilliseconds(d), NewThreadScheduler.Default).Count().Wait();
  29. }
  30. catch (Exception)
  31. {
  32. Debug.Write(".");
  33. continue;
  34. }
  35. throw new InvalidOperationException("Didn't throw!");
  36. }
  37. Debug.WriteLine("");
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// Tests no OnNext messages are lost.
  43. /// </summary>
  44. public static void OnNextMessages()
  45. {
  46. while (true)
  47. {
  48. foreach (var N in new[] { 1, 10, 100, 1000, 10000, 100000 })
  49. {
  50. Debug.WriteLine("N = {0}", N);
  51. foreach (var d in new[] { 1, 10, 20, 50, 100, 200, 250, 500 })
  52. {
  53. var n = Observable.Range(0, N, NewThreadScheduler.Default).Delay(TimeSpan.FromMilliseconds(d), NewThreadScheduler.Default).Count().Wait();
  54. if (n != N)
  55. {
  56. throw new InvalidOperationException("Lost OnNext message!");
  57. }
  58. Debug.Write(".");
  59. }
  60. Debug.WriteLine("");
  61. }
  62. }
  63. }
  64. }
  65. }