Replay.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.Linq;
  6. using System.Text;
  7. using System.Reactive.Concurrency;
  8. using System.Threading;
  9. using System.Reactive.Linq;
  10. namespace ReactiveTests.Stress.Linq
  11. {
  12. public class Replay
  13. {
  14. /// <summary>
  15. /// Tests the Replay operator with different schedulers, supporting ISchedulerLongRunning and otherwise.
  16. /// Stresses the ScheduledObserver implementation with its counting logic.
  17. /// </summary>
  18. public static void DifferentSchedulers()
  19. {
  20. while (true)
  21. {
  22. for (int i = 100; i <= 10000; i *= 10)
  23. {
  24. foreach (var s in new IScheduler[] { Scheduler.Default, TaskPoolScheduler.Default, ThreadPoolScheduler.Instance })
  25. {
  26. foreach (var b in new[] { true, false })
  27. {
  28. var t = b ? s : s.DisableOptimizations();
  29. var e = new ManualResetEvent(false);
  30. var xs = Observable.Range(0, i, TaskPoolScheduler.Default.DisableOptimizations()).Do(_ => { }, () => e.Set());
  31. var ys = xs.Replay(t);
  32. var f = new ManualResetEvent(false);
  33. var r = new List<int>();
  34. ys.Subscribe(r.Add, () => f.Set());
  35. ys.Connect();
  36. e.WaitOne();
  37. f.WaitOne();
  38. if (!r.SequenceEqual(Enumerable.Range(0, i)))
  39. throw new Exception();
  40. Console.Write(".");
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. #endif