forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedulerExtensions.cs
More file actions
228 lines (206 loc) · 9.68 KB
/
SchedulerExtensions.cs
File metadata and controls
228 lines (206 loc) · 9.68 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
// Copyright (c) 2020 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Reactive.Testing;
namespace ReactiveUI.Testing
{
#pragma warning disable SA1600 // Elements should be documented
public static class SchedulerExtensions
#pragma warning restore SA1600 // Elements should be documented
{
private static readonly AutoResetEvent schedulerGate = new(true);
/// <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="scheduler">The scheduler to use.</param>
/// <returns>An object that when disposed, restores the previous default
/// schedulers.</returns>
public static IDisposable WithScheduler(IScheduler scheduler)
{
schedulerGate.WaitOne();
var prevDef = RxApp.MainThreadScheduler;
var prevTask = RxApp.TaskpoolScheduler;
RxApp.MainThreadScheduler = scheduler;
RxApp.TaskpoolScheduler = scheduler;
return Disposable.Create(() =>
{
RxApp.MainThreadScheduler = prevDef;
RxApp.TaskpoolScheduler = prevTask;
schedulerGate.Set();
});
}
/// <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>
/// <typeparam name="T">The scheduler type.</typeparam>
/// <typeparam name="TRet">The return type.</typeparam>
/// <param name="scheduler">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 scheduler, Func<T, TRet> block)
where T : IScheduler
{
if (block is null)
{
throw new ArgumentNullException(nameof(block));
}
TRet ret;
using (WithScheduler(scheduler))
{
ret = block(scheduler);
}
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>
/// <typeparam name="T">The type.</typeparam>
/// <typeparam name="TRet">The return type.</typeparam>
/// <param name="scheduler">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 scheduler, Func<T, Task<TRet>> block)
where T : IScheduler
{
if (block is null)
{
throw new ArgumentNullException(nameof(block));
}
TRet ret;
using (WithScheduler(scheduler))
{
ret = await block(scheduler).ConfigureAwait(false);
}
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>
/// <typeparam name="T">The type.</typeparam>
/// <param name="scheduler">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
public static void With<T>(this T scheduler, Action<T> block)
where T : IScheduler =>
scheduler.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>
/// <typeparam name="T">The type.</typeparam>
/// <param name="scheduler">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task WithAsync<T>(this T scheduler, Func<T, Task> block)
where T : IScheduler =>
scheduler.WithAsync(async x =>
{
await block(x).ConfigureAwait(false);
return 0;
});
/// <summary>
/// AdvanceToMs moves the TestScheduler to the specified time in
/// milliseconds.
/// </summary>
/// <param name="scheduler">The scheduler to advance.</param>
/// <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 scheduler, double milliseconds)
{
if (scheduler is null)
{
throw new ArgumentNullException(nameof(scheduler));
}
scheduler.AdvanceTo(scheduler.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)));
}
/// <summary>
/// AdvanceByMs moves the TestScheduler along by the specified time in
/// milliseconds.
/// </summary>
/// <param name="scheduler">The scheduler to advance.</param>
/// <param name="milliseconds">The relative time to advance the TestScheduler
/// by, in milliseconds.</param>
public static void AdvanceByMs(this TestScheduler scheduler, double milliseconds)
{
if (scheduler is null)
{
throw new ArgumentNullException(nameof(scheduler));
}
scheduler.AdvanceBy(scheduler.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)));
}
/// <summary>
/// OnNextAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="scheduler">The scheduler to fire from.</param>
/// <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 scheduler, double milliseconds, T value) =>
new(
scheduler.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnNext(value));
/// <summary>
/// OnErrorAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="scheduler">The scheduler to fire from.</param>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <param name="ex">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 scheduler, double milliseconds, Exception ex) =>
new(
scheduler.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>
/// <typeparam name="T">The type.</typeparam>
/// <param name="scheduler">The scheduler to fire from.</param>
/// <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 scheduler, double milliseconds) =>
new(
scheduler.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnCompleted<T>());
/// <summary>
/// Converts a timespan to a virtual time for testing.
/// </summary>
/// <param name="scheduler">The scheduler.</param>
/// <param name="span">Timespan to convert.</param>
/// <returns>Timespan for virtual scheduler to use.</returns>
public static long FromTimeSpan(this TestScheduler scheduler, TimeSpan span) => span.Ticks;
}
}
// vim: tw=120 ts=4 sw=4 et :