-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathTestProbe.cs
More file actions
311 lines (250 loc) · 9.97 KB
/
TestProbe.cs
File metadata and controls
311 lines (250 loc) · 9.97 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
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
// -----------------------------------------------------------------------
// <copyright file="TestProbe.cs" company="Asynkron AB">
// Copyright (C) 2015-2025 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Proto;
using Proto.Mailbox;
namespace Proto.TestKit;
/// <inheritdoc cref="ITestProbe" />
public class TestProbe : IActor, ITestProbe
{
private readonly Channel<MessageAndSender> _channel = Channel.CreateUnbounded<MessageAndSender>();
private IContext? _context;
/// <inheritdoc />
public Task ReceiveAsync(IContext context)
{
switch (context.Message)
{
case Started _:
Context = context;
break;
case Terminated _:
_channel.Writer.TryWrite(new MessageAndSender(context));
break;
case SystemMessage _:
default:
_channel.Writer.TryWrite(new MessageAndSender(context));
break;
}
return Task.CompletedTask;
}
/// <inheritdoc />
public PID? Sender { get; private set; }
/// <inheritdoc />
public IContext Context
{
get
{
if (_context is null)
{
throw new InvalidOperationException("Probe context is null");
}
return _context;
}
private set => _context = value;
}
/// <inheritdoc />
public async Task ExpectNoMessageAsync(TimeSpan? timeAllowed = null, CancellationToken cancellationToken = default)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(timeAllowed ?? TimeSpan.FromSeconds(1));
try
{
var item = await _channel.Reader.ReadAsync(cts.Token);
var seconds = (timeAllowed ?? TimeSpan.FromSeconds(1)).TotalSeconds.ToString("0.###");
throw new TestKitException($"Waited {seconds} seconds and received a message of type {item.Message.GetType()}");
}
catch (OperationCanceledException)
{
// expected - no message arrived
}
}
/// <inheritdoc />
public async Task<object?> GetNextMessageAsync(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var item = await ReceiveNextAsync(timeAllowed, cancellationToken);
return item.Message;
}
/// <inheritdoc />
public async Task<T> GetNextMessageAsync<T>(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var item = await ReceiveNextAsync(timeAllowed, cancellationToken);
if (item.Message is not T typed)
{
throw new TestKitException($"Message expected type {typeof(T)}, actual type {item.Message?.GetType()}");
}
return typed;
}
/// <inheritdoc />
public async Task<T> GetNextMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var output = await GetNextMessageAsync<T>(timeAllowed, cancellationToken);
if (!when(output))
{
throw new TestKitException("Condition not met");
}
return output;
}
/// <inheritdoc />
public Task<T> FishForMessageAsync<T>(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) =>
FishForMessageAsync<T>(_ => true, timeAllowed, cancellationToken);
/// <inheritdoc />
public async Task<T> FishForMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var endTime = DateTime.UtcNow + (timeAllowed ?? TimeSpan.FromSeconds(1));
while (DateTime.UtcNow < endTime)
{
var remaining = endTime - DateTime.UtcNow;
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(remaining);
try
{
var item = await _channel.Reader.ReadAsync(cts.Token);
if (item.Message is T typed && when(typed))
{
Sender = item.Sender;
return typed;
}
}
catch (OperationCanceledException)
{
// ignored, loop will end if time elapsed
}
}
throw new TestKitException("Message not found");
}
/// <inheritdoc />
public void Send(PID target, object message) => Context.Send(target, message);
/// <inheritdoc />
public void Request(PID target, object message) => Context.Request(target, message);
/// <inheritdoc />
public void Respond(object message)
{
if (Sender is null)
{
return;
}
Send(Sender, message);
}
/// <inheritdoc />
public Task<T> RequestAsync<T>(PID target, object message) => Context.RequestAsync<T>(target, message);
/// <inheritdoc />
public Task<T> RequestAsync<T>(PID target, object message, CancellationToken cancellationToken) =>
Context.RequestAsync<T>(target, message, cancellationToken);
/// <inheritdoc />
public Task<T> RequestAsync<T>(PID target, object message, TimeSpan timeAllowed) =>
Context.RequestAsync<T>(target, message, timeAllowed);
public static implicit operator PID?(TestProbe tp) => tp.Context.Self;
private async Task<MessageAndSender> ReceiveNextAsync(TimeSpan? timeAllowed,
CancellationToken cancellationToken)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(timeAllowed ?? TimeSpan.FromSeconds(1));
try
{
var item = await _channel.Reader.ReadAsync(cts.Token);
Sender = item.Sender;
return item;
}
catch (OperationCanceledException)
{
var seconds = (timeAllowed ?? TimeSpan.FromSeconds(1)).TotalSeconds.ToString("0.###");
throw new TestKitException($"Waited {seconds} seconds but failed to receive a message");
}
}
/// <inheritdoc />
public async Task<T> GetNextSystemMessageAsync<T>(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) where T : SystemMessage
{
var item = await ReceiveNextAsync(timeAllowed, cancellationToken);
if (item.Message is not SystemMessage)
{
throw new TestKitException(
$"Expected system message of type {typeof(T)}, but received user message of type {item.Message?.GetType()}");
}
if (item.Message is not T typed)
{
throw new TestKitException($"Message expected type {typeof(T)}, actual type {item.Message.GetType()}");
}
return typed;
}
/// <inheritdoc />
public async Task<T> GetNextUserMessageAsync<T>(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var item = await ReceiveNextAsync(timeAllowed, cancellationToken);
if (item.Message is SystemMessage sys)
{
throw new TestKitException(
$"Expected user message of type {typeof(T)}, but received system message of type {sys.GetType()}");
}
if (item.Message is not T typed)
{
throw new TestKitException($"Message expected type {typeof(T)}, actual type {item.Message?.GetType()}");
}
return typed;
}
/// <inheritdoc />
public async Task<T> GetNextSystemMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) where T : SystemMessage
{
var output = await GetNextSystemMessageAsync<T>(timeAllowed, cancellationToken);
if (!when(output))
{
throw new TestKitException("Condition not met");
}
return output;
}
/// <inheritdoc />
public async Task<T> GetNextUserMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var output = await GetNextUserMessageAsync<T>(timeAllowed, cancellationToken);
if (!when(output))
{
throw new TestKitException("Condition not met");
}
return output;
}
/// <inheritdoc />
public async Task ExpectNextSystemMessageAsync<T>(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) where T : SystemMessage =>
_ = await GetNextSystemMessageAsync<T>(timeAllowed, cancellationToken);
/// <inheritdoc />
public async Task ExpectNextUserMessageAsync<T>(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) =>
_ = await GetNextUserMessageAsync<T>(timeAllowed, cancellationToken);
/// <inheritdoc />
public async Task ExpectNextSystemMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) where T : SystemMessage =>
_ = await GetNextSystemMessageAsync(when, timeAllowed, cancellationToken);
/// <inheritdoc />
public async Task ExpectNextUserMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default) =>
_ = await GetNextUserMessageAsync(when, timeAllowed, cancellationToken);
/// <inheritdoc />
public async Task ExpectEmptyMailboxAsync(TimeSpan? timeAllowed = null,
CancellationToken cancellationToken = default)
{
var self = Context.Self;
if (timeAllowed.HasValue)
{
await RequestAsync<Touched>(self, new Touch(), timeAllowed.Value);
}
else
{
await RequestAsync<Touched>(self, new Touch(), cancellationToken);
}
await GetNextUserMessageAsync<Touch>(timeAllowed, cancellationToken);
}
}