Replay.cs 2.0 KB

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