TestScheduler.cs 9.1 KB

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