forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.cs
More file actions
231 lines (211 loc) · 9.46 KB
/
TestUtils.cs
File metadata and controls
231 lines (211 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Reactive;
using System.Reactive.Disposables;
using System.Threading;
using ReactiveUI;
using System.Reactive.Concurrency;
using Microsoft.Reactive.Testing;
using System.Threading.Tasks;
namespace ReactiveUI.Testing
{
public static class TestUtils
{
static readonly AutoResetEvent schedGate = new AutoResetEvent(true);
static readonly object mbGate = 42;
/// <summary>
/// WithScheduler overrides the default Deferred and Taskpool schedulers
/// with the given scheduler until the return value is disposed. This
/// is useful in a unit test runner to force RxXaml objects to schedule
/// via a TestScheduler object.
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <returns>An object that when disposed, restores the previous default
/// schedulers.</returns>
public static IDisposable WithScheduler(IScheduler sched)
{
schedGate.WaitOne();
var prevDef = RxApp.MainThreadScheduler;
var prevTask = RxApp.TaskpoolScheduler;
RxApp.MainThreadScheduler = sched;
RxApp.TaskpoolScheduler = sched;
return Disposable.Create(() => {
RxApp.MainThreadScheduler = prevDef;
RxApp.TaskpoolScheduler = prevTask;
schedGate.Set();
});
}
/// <summary>
/// WithMessageBus allows you to override the default Message Bus
/// implementation until the object returned is disposed. If a
/// message bus is not specified, a default empty one is created.
/// </summary>
/// <param name="messageBus">The message bus to use, or null to create
/// a new one using the default implementation.</param>
/// <returns>An object that when disposed, restores the original
/// message bus.</returns>
public static IDisposable WithMessageBus(this IMessageBus messageBus)
{
var origMessageBus = MessageBus.Current;
Monitor.Enter(mbGate);
MessageBus.Current = messageBus ?? new MessageBus();
return Disposable.Create(() =>
{
MessageBus.Current = origMessageBus;
Monitor.Exit(mbGate);
});
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Func. Use
/// this to initialize objects that store the default scheduler (most
/// RxXaml objects).
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The function to execute.</param>
/// <returns>The return value of the function.</returns>
public static TRet With<T, TRet>(this T sched, Func<T, TRet> block)
where T : IScheduler
{
TRet ret;
using (WithScheduler(sched)) {
ret = block(sched);
}
return ret;
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Func. Use
/// this to initialize objects that store the default scheduler (most
/// RxXaml objects).
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The function to execute.</param>
/// <returns>The return value of the function.</returns>
public static async Task<TRet> WithAsync<T, TRet>(this T sched, Func<T, Task<TRet>> block)
where T : IScheduler
{
TRet ret;
using (WithScheduler(sched)) {
ret = await block(sched);
}
return ret;
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Action.
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
public static void With<T>(this T sched, Action<T> block)
where T : IScheduler
{
sched.With(x => { block(x); return 0; });
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Action.
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
public static Task WithAsync<T>(this T sched, Func<T, Task> block)
where T : IScheduler
{
return sched.WithAsync(async x => { await block(x); return 0; });
}
/// <summary>
/// Override the default Message Bus during the specified block.
/// </summary>
/// <param name="messageBus">The message bus to use for the block.</param>
/// <param name="block">The function to execute.</param>
/// <returns>The return value of the function.</returns>
public static TRet With<TRet>(this IMessageBus messageBus, Func<TRet> block)
{
using (messageBus.WithMessageBus()) {
return block();
}
}
/// <summary>
/// Override the default Message Bus during the specified block.
/// </summary>
/// <param name="messageBus">The message bus to use for the block.</param>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
public static void With(this IMessageBus messageBus, Action block)
{
using (messageBus.WithMessageBus()) {
block();
}
}
/// <summary>
/// AdvanceToMs moves the TestScheduler to the specified time in
/// milliseconds.
/// </summary>
/// <param name="milliseconds">The time offset to set the TestScheduler
/// to, in milliseconds. Note that this is *not* additive or
/// incremental, it sets the time.</param>
public static void AdvanceToMs(this TestScheduler sched, double milliseconds)
{
sched.AdvanceTo(sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)));
}
/// <summary>
/// AdvanceByMs moves the TestScheduler along by the specified time in
/// milliseconds.
/// </summary>
/// <param name="milliseconds">The relative time to advance the TestScheduler
/// by, in milliseconds.</param>
public static void AdvanceByMs(this TestScheduler sched, double milliseconds)
{
sched.AdvanceBy(sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)));
}
/// <summary>
/// OnNextAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <param name="value">The value to produce.</param>
/// <returns>A recorded notification that can be provided to
/// TestScheduler.CreateHotObservable.</returns>
public static Recorded<Notification<T>> OnNextAt<T>(this TestScheduler sched, double milliseconds, T value)
{
return new Recorded<Notification<T>>(
sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnNext<T>(value));
}
/// <summary>
/// OnErrorAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <param name="exception">The exception to terminate the Observable
/// with.</param>
/// <returns>A recorded notification that can be provided to
/// TestScheduler.CreateHotObservable.</returns>
public static Recorded<Notification<T>> OnErrorAt<T>(this TestScheduler sched, double milliseconds, Exception ex)
{
return new Recorded<Notification<T>>(
sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnError<T>(ex));
}
/// <summary>
/// OnCompletedAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <returns>A recorded notification that can be provided to
/// TestScheduler.CreateHotObservable.</returns>
public static Recorded<Notification<T>> OnCompletedAt<T>(this TestScheduler sched, double milliseconds)
{
return new Recorded<Notification<T>>(
sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnCompleted<T>());
}
public static long FromTimeSpan(this TestScheduler sched, TimeSpan span)
{
return span.Ticks;
}
}
}
// vim: tw=120 ts=4 sw=4 et :