Delay.cs 2.3 KB

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