-
Notifications
You must be signed in to change notification settings - Fork 1k
/
TestScheduler.cs
339 lines (306 loc) · 11.8 KB
/
TestScheduler.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//-----------------------------------------------------------------------
// <copyright file="TestScheduler.cs" company="Akka.NET Project">
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Akka.Actor;
using Akka.Configuration;
using Akka.Dispatch;
using Akka.Event;
namespace Akka.TestKit
{
/// <summary>
/// TBD
/// </summary>
public class TestScheduler : IScheduler, IAdvancedScheduler
{
private DateTimeOffset _now;
private readonly ConcurrentDictionary<long, ConcurrentQueue<ScheduledItem>> _scheduledWork;
/// <summary>
/// TBD
/// </summary>
/// <param name="schedulerConfig">TBD</param>
/// <param name="log">TBD</param>
public TestScheduler(Config schedulerConfig, ILoggingAdapter log)
{
_now = DateTimeOffset.UtcNow;
_scheduledWork = new ConcurrentDictionary<long, ConcurrentQueue<ScheduledItem>>();
}
/// <summary>
/// TBD
/// </summary>
/// <param name="offset">TBD</param>
public void Advance(TimeSpan offset)
{
_now = _now.Add(offset);
var tickItems = _scheduledWork.Where(s => s.Key <= _now.Ticks).OrderBy(s => s.Key).ToList();
foreach (var t in tickItems)
{
foreach (var si in t.Value.Where(i => i.Cancelable == null || !i.Cancelable.IsCancellationRequested))
{
if (si.Type == ScheduledItem.ScheduledItemType.Message)
si.Receiver.Tell(si.Message, si.Sender);
else
si.Action();
si.DeliveryCount++;
}
_scheduledWork.TryRemove(t.Key, out var removed);
foreach (var i in removed.Where(r => r.Repeating && (r.Cancelable == null || !r.Cancelable.IsCancellationRequested)))
{
InternalSchedule(null, i.Delay, i.Receiver, i.Message, i.Action, i.Sender, i.Cancelable, i.DeliveryCount);
}
}
}
/// <summary>
/// TBD
/// </summary>
/// <param name="when">TBD</param>
/// <exception cref="InvalidOperationException">
/// This exception is thrown when the specified <paramref name="when"/> offset is less than the currently tracked time.
/// </exception>
public void AdvanceTo(DateTimeOffset when)
{
if (when < _now)
throw new InvalidOperationException("You can't reverse time...");
Advance(when.Subtract(_now));
}
private void InternalSchedule(TimeSpan? initialDelay, TimeSpan delay, ICanTell receiver, object message, Action action,
IActorRef sender, ICancelable cancelable, int deliveryCount = 0)
{
var scheduledTime = _now.Add(initialDelay ?? delay).UtcTicks;
ConcurrentQueue<ScheduledItem> tickItems;
while (!_scheduledWork.TryGetValue(scheduledTime, out tickItems))
{
tickItems = new ConcurrentQueue<ScheduledItem>();
if (_scheduledWork.TryAdd(scheduledTime, tickItems))
{
break;
}
}
var type = message == null ? ScheduledItem.ScheduledItemType.Action : ScheduledItem.ScheduledItemType.Message;
tickItems.Enqueue(new ScheduledItem(initialDelay ?? delay, delay, type, message, action,
initialDelay.HasValue || deliveryCount > 0, receiver, sender, cancelable));
}
/// <summary>
/// TBD
/// </summary>
/// <param name="delay">TBD</param>
/// <param name="receiver">TBD</param>
/// <param name="message">TBD</param>
/// <param name="sender">TBD</param>
public void ScheduleTellOnce(TimeSpan delay, ICanTell receiver, object message, IActorRef sender)
{
InternalSchedule(null, delay, receiver, message, null, sender, null);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="delay">TBD</param>
/// <param name="receiver">TBD</param>
/// <param name="message">TBD</param>
/// <param name="sender">TBD</param>
/// <param name="cancelable">TBD</param>
public void ScheduleTellOnce(TimeSpan delay, ICanTell receiver, object message, IActorRef sender, ICancelable cancelable)
{
InternalSchedule(null, delay, receiver, message, null, sender, cancelable);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="initialDelay">TBD</param>
/// <param name="interval">TBD</param>
/// <param name="receiver">TBD</param>
/// <param name="message">TBD</param>
/// <param name="sender">TBD</param>
public void ScheduleTellRepeatedly(TimeSpan initialDelay, TimeSpan interval, ICanTell receiver, object message,
IActorRef sender)
{
InternalSchedule(initialDelay, interval, receiver, message, null, sender, null);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="initialDelay">TBD</param>
/// <param name="interval">TBD</param>
/// <param name="receiver">TBD</param>
/// <param name="message">TBD</param>
/// <param name="sender">TBD</param>
/// <param name="cancelable">TBD</param>
public void ScheduleTellRepeatedly(TimeSpan initialDelay, TimeSpan interval, ICanTell receiver, object message,
IActorRef sender, ICancelable cancelable)
{
InternalSchedule(initialDelay, interval, receiver, message, null, sender, cancelable);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="delay">TBD</param>
/// <param name="action">TBD</param>
/// <param name="cancelable">TBD</param>
public void ScheduleOnce(TimeSpan delay, Action action, ICancelable cancelable)
{
InternalSchedule(null, delay, null, null, action, null, null);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="delay">TBD</param>
/// <param name="action">TBD</param>
public void ScheduleOnce(TimeSpan delay, Action action)
{
InternalSchedule(null, delay, null, null, action, null, null);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="initialDelay">TBD</param>
/// <param name="interval">TBD</param>
/// <param name="action">TBD</param>
/// <param name="cancelable">TBD</param>
public void ScheduleRepeatedly(TimeSpan initialDelay, TimeSpan interval, Action action, ICancelable cancelable)
{
InternalSchedule(initialDelay, interval, null, null, action, null, cancelable);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="initialDelay">TBD</param>
/// <param name="interval">TBD</param>
/// <param name="action">TBD</param>
public void ScheduleRepeatedly(TimeSpan initialDelay, TimeSpan interval, Action action)
{
InternalSchedule(initialDelay, interval, null, null, action, null, null);
}
/// <summary>
/// TBD
/// </summary>
protected DateTimeOffset TimeNow { get { return _now; } }
/// <summary>
/// TBD
/// </summary>
public DateTimeOffset Now { get { return _now; } }
/// <summary>
/// TBD
/// </summary>
public TimeSpan MonotonicClock { get { return Util.MonotonicClock.Elapsed; } }
/// <summary>
/// TBD
/// </summary>
public TimeSpan HighResMonotonicClock { get { return Util.MonotonicClock.ElapsedHighRes; } }
/// <summary>
/// TBD
/// </summary>
public IAdvancedScheduler Advanced
{
get { return this; }
}
/// <summary>
/// TBD
/// </summary>
internal class ScheduledItem
{
/// <summary>
/// TBD
/// </summary>
public TimeSpan InitialDelay { get; set; }
/// <summary>
/// TBD
/// </summary>
public TimeSpan Delay { get; set; }
/// <summary>
/// TBD
/// </summary>
public ScheduledItemType Type { get; set; }
/// <summary>
/// TBD
/// </summary>
public object Message { get; set; }
/// <summary>
/// TBD
/// </summary>
public Action Action { get; set; }
/// <summary>
/// TBD
/// </summary>
public bool Repeating { get; set; }
/// <summary>
/// TBD
/// </summary>
public ICanTell Receiver { get; set; }
/// <summary>
/// TBD
/// </summary>
public IActorRef Sender { get; set; }
/// <summary>
/// TBD
/// </summary>
public ICancelable Cancelable { get; set; }
/// <summary>
/// TBD
/// </summary>
public int DeliveryCount { get; set; }
/// <summary>
/// TBD
/// </summary>
public enum ScheduledItemType
{
/// <summary>
/// TBD
/// </summary>
Message,
/// <summary>
/// TBD
/// </summary>
Action
}
/// <summary>
/// TBD
/// </summary>
/// <param name="initialDelay">TBD</param>
/// <param name="delay">TBD</param>
/// <param name="type">TBD</param>
/// <param name="message">TBD</param>
/// <param name="action">TBD</param>
/// <param name="repeating">TBD</param>
/// <param name="receiver">TBD</param>
/// <param name="sender">TBD</param>
/// <param name="cancelable">TBD</param>
public ScheduledItem(TimeSpan initialDelay, TimeSpan delay, ScheduledItemType type, object message, Action action, bool repeating, ICanTell receiver,
IActorRef sender, ICancelable cancelable)
{
InitialDelay = initialDelay;
Delay = delay;
Type = type;
Message = message;
Action = action;
Repeating = repeating;
Receiver = receiver;
Sender = sender;
Cancelable = cancelable;
DeliveryCount = 0;
}
}
// don't need these methods - not used during testing
public void ScheduleOnce(TimeSpan delay, IRunnable action, ICancelable cancelable)
{
throw new NotImplementedException();
}
public void ScheduleOnce(TimeSpan delay, IRunnable action)
{
throw new NotImplementedException();
}
public void ScheduleRepeatedly(TimeSpan initialDelay, TimeSpan interval, IRunnable action, ICancelable cancelable)
{
throw new NotImplementedException();
}
public void ScheduleRepeatedly(TimeSpan initialDelay, TimeSpan interval, IRunnable action)
{
throw new NotImplementedException();
}
}
}