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