TestScheduler.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. using System;
  5. using System.Reactive;
  6. using System.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. namespace Microsoft.Reactive.Testing
  9. {
  10. /// <summary>
  11. /// Virtual time scheduler used for testing applications and libraries built using Reactive Extensions.
  12. /// </summary>
  13. public class TestScheduler : VirtualTimeScheduler<long, long>
  14. {
  15. /// <summary>
  16. /// Schedules an action to be executed at the specified virtual time.
  17. /// </summary>
  18. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  19. /// <param name="state">State passed to the action to be executed.</param>
  20. /// <param name="action">Action to be executed.</param>
  21. /// <param name="dueTime">Absolute virtual time at which to execute the action.</param>
  22. /// <returns>Disposable object used to cancel the scheduled action (best effort).</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  24. public override IDisposable ScheduleAbsolute<TState>(TState state, long dueTime, Func<IScheduler, TState, IDisposable> action)
  25. {
  26. if (dueTime <= Clock)
  27. dueTime = Clock + 1;
  28. return base.ScheduleAbsolute<TState>(state, dueTime, action);
  29. }
  30. /// <summary>
  31. /// Adds a relative virtual time to an absolute virtual time value.
  32. /// </summary>
  33. /// <param name="absolute">Absolute virtual time value.</param>
  34. /// <param name="relative">Relative virtual time value to add.</param>
  35. /// <returns>Resulting absolute virtual time sum value.</returns>
  36. protected override long Add(long absolute, long relative)
  37. {
  38. return absolute + relative;
  39. }
  40. /// <summary>
  41. /// Converts the absolute virtual time value to a DateTimeOffset value.
  42. /// </summary>
  43. /// <param name="absolute">Absolute virtual time value to convert.</param>
  44. /// <returns>Corresponding DateTimeOffset value.</returns>
  45. protected override DateTimeOffset ToDateTimeOffset(long absolute)
  46. {
  47. return new DateTimeOffset(absolute, TimeSpan.Zero);
  48. }
  49. /// <summary>
  50. /// Converts the TimeSpan value to a relative virtual time value.
  51. /// </summary>
  52. /// <param name="timeSpan">TimeSpan value to convert.</param>
  53. /// <returns>Corresponding relative virtual time value.</returns>
  54. protected override long ToRelative(TimeSpan timeSpan)
  55. {
  56. return timeSpan.Ticks;
  57. }
  58. /// <summary>
  59. /// Starts the test scheduler and uses the specified virtual times to invoke the factory function, subscribe to the resulting sequence, and dispose the subscription.
  60. /// </summary>
  61. /// <typeparam name="T">The element type of the observable sequence being tested.</typeparam>
  62. /// <param name="create">Factory method to create an observable sequence.</param>
  63. /// <param name="created">Virtual time at which to invoke the factory to create an observable sequence.</param>
  64. /// <param name="subscribed">Virtual time at which to subscribe to the created observable sequence.</param>
  65. /// <param name="disposed">Virtual time at which to dispose the subscription.</param>
  66. /// <returns>Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.</returns>
  67. /// <exception cref="ArgumentNullException"><paramref name="create"/> is null.</exception>
  68. public ITestableObserver<T> Start<T>(Func<IObservable<T>> create, long created, long subscribed, long disposed)
  69. {
  70. if (create == null)
  71. throw new ArgumentNullException("create");
  72. var source = default(IObservable<T>);
  73. var subscription = default(IDisposable);
  74. var observer = CreateObserver<T>();
  75. ScheduleAbsolute(default(object), created, (scheduler, state) => { source = create(); return Disposable.Empty; });
  76. ScheduleAbsolute(default(object), subscribed, (scheduler, state) => { subscription = source.Subscribe(observer); return Disposable.Empty; });
  77. ScheduleAbsolute(default(object), disposed, (scheduler, state) => { subscription.Dispose(); return Disposable.Empty; });
  78. Start();
  79. return observer;
  80. }
  81. /// <summary>
  82. /// Starts the test scheduler and uses the specified virtual time to dispose the subscription to the sequence obtained through the factory function.
  83. /// Default virtual times are used for <see cref="ReactiveTest.Created">factory invocation</see> and <see cref="ReactiveTest.Subscribed">sequence subscription</see>.
  84. /// </summary>
  85. /// <typeparam name="T">The element type of the observable sequence being tested.</typeparam>
  86. /// <param name="create">Factory method to create an observable sequence.</param>
  87. /// <param name="disposed">Virtual time at which to dispose the subscription.</param>
  88. /// <returns>Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.</returns>
  89. /// <exception cref="ArgumentNullException"><paramref name="create"/> is null.</exception>
  90. public ITestableObserver<T> Start<T>(Func<IObservable<T>> create, long disposed)
  91. {
  92. if (create == null)
  93. throw new ArgumentNullException("create");
  94. return Start(create, ReactiveTest.Created, ReactiveTest.Subscribed, disposed);
  95. }
  96. /// <summary>
  97. /// Starts the test scheduler and uses default virtual times to <see cref="ReactiveTest.Created">invoke the factory function</see>, to <see cref="ReactiveTest.Subscribed">subscribe to the resulting sequence</see>, and to <see cref="ReactiveTest.Disposed">dispose the subscription</see>.
  98. /// </summary>
  99. /// <typeparam name="T">The element type of the observable sequence being tested.</typeparam>
  100. /// <param name="create">Factory method to create an observable sequence.</param>
  101. /// <returns>Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.</returns>
  102. /// <exception cref="ArgumentNullException"><paramref name="create"/> is null.</exception>
  103. public ITestableObserver<T> Start<T>(Func<IObservable<T>> create)
  104. {
  105. if (create == null)
  106. throw new ArgumentNullException("create");
  107. return Start(create, ReactiveTest.Created, ReactiveTest.Subscribed, ReactiveTest.Disposed);
  108. }
  109. /// <summary>
  110. /// Creates a hot observable using the specified timestamped notification messages.
  111. /// </summary>
  112. /// <typeparam name="T">The element type of the observable sequence being created.</typeparam>
  113. /// <param name="messages">Notifications to surface through the created sequence at their specified absolute virtual times.</param>
  114. /// <returns>Hot observable sequence that can be used to assert the timing of subscriptions and notifications.</returns>
  115. /// <exception cref="ArgumentNullException"><paramref name="messages"/> is null.</exception>
  116. public ITestableObservable<T> CreateHotObservable<T>(params Recorded<Notification<T>>[] messages)
  117. {
  118. if (messages == null)
  119. throw new ArgumentNullException("messages");
  120. return new HotObservable<T>(this, messages);
  121. }
  122. /// <summary>
  123. /// Creates a cold observable using the specified timestamped notification messages.
  124. /// </summary>
  125. /// <typeparam name="T">The element type of the observable sequence being created.</typeparam>
  126. /// <param name="messages">Notifications to surface through the created sequence at their specified virtual time offsets from the sequence subscription time.</param>
  127. /// <returns>Cold observable sequence that can be used to assert the timing of subscriptions and notifications.</returns>
  128. /// <exception cref="ArgumentNullException"><paramref name="messages"/> is null.</exception>
  129. public ITestableObservable<T> CreateColdObservable<T>(params Recorded<Notification<T>>[] messages)
  130. {
  131. if (messages == null)
  132. throw new ArgumentNullException("messages");
  133. return new ColdObservable<T>(this, messages);
  134. }
  135. /// <summary>
  136. /// Creates an observer that records received notification messages and timestamps those.
  137. /// </summary>
  138. /// <typeparam name="T">The element type of the observer being created.</typeparam>
  139. /// <returns>Observer that can be used to assert the timing of received notifications.</returns>
  140. public ITestableObserver<T> CreateObserver<T>()
  141. {
  142. return new MockObserver<T>(this);
  143. }
  144. }
  145. }