-
Notifications
You must be signed in to change notification settings - Fork 10
/
Saga.cs
558 lines (490 loc) · 21.6 KB
/
Saga.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
namespace NServiceBus.Testing
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MessageInterfaces.MessageMapper.Reflection;
internal static class SagaConsts
{
public const string Originator = "NServiceBus.Testing.SagaOriginator";
}
/// <summary>
/// Saga unit testing framework.
/// </summary>
public partial class Saga<T> where T : Saga
{
internal Saga(T saga)
{
this.saga = saga;
testContext = new TestingContext(messageCreator);
if (saga.Entity == null)
{
var prop = typeof(T).GetProperty("Data");
if (prop == null)
{
return;
}
var sagaData = Activator.CreateInstance(prop.PropertyType) as IContainSagaData;
saga.Entity = sagaData;
}
saga.Entity.OriginalMessageId = Guid.NewGuid().ToString();
saga.Entity.Originator = SagaConsts.Originator;
}
/// <summary>
/// Set the address of the client that caused the saga to be started.
/// </summary>
public Saga<T> WhenReceivesMessageFrom(string client)
{
saga.Entity.Originator = client;
return this;
}
/// <summary>
/// Provides a way to set external dependencies on the saga under test.
/// </summary>
public Saga<T> WithExternalDependencies(Action<T> actionToSetUpExternalDependencies)
{
actionToSetUpExternalDependencies(saga);
return this;
}
/// <summary>
/// Provides a way to customize the <see cref="IMessageHandlerContext" /> instance received by the message handler.
/// </summary>
public Saga<T> ConfigureHandlerContext(Action<TestableMessageHandlerContext> contextInitializer)
{
contextInitializer(testContext);
return this;
}
/// <summary>
/// Set the headers on an incoming message that will be return
/// when code calls Bus.CurrentMessageContext.Headers
/// </summary>
public Saga<T> SetIncomingHeader(string key, string value)
{
testContext.MessageHeaders[key] = value;
return this;
}
/// <summary>
/// Check that the saga sends a message of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectSend<TMessage>(Func<TMessage, SendOptions, bool> check = null)
{
testContext.AddExpectation(new ExpectSend<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga sends a message of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectSend<TMessage>(Func<TMessage, bool> check)
{
return ExpectSend((TMessage m, SendOptions _) => check(m));
}
/// <summary>
/// Check that the saga sends a message of the given type complying with user-supplied assertions.
/// </summary>
/// <param name="check">An action containing assertions on the message.</param>
public Saga<T> ExpectSend<TMessage>(Action<TMessage> check)
{
return ExpectSend(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga does not send a message of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectNotSend<TMessage>(Func<TMessage, SendOptions, bool> check = null)
{
testContext.AddExpectation(new ExpectNotSend<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga does not send a message of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectNotSend<TMessage>(Func<TMessage, bool> check)
{
return ExpectNotSend((TMessage m, SendOptions _) => check(m));
}
/// <summary>
/// Check that the saga does not send a message of the given type complying with the given predicate.
/// </summary>
/// <param name="check">An action containing assertions on the message.</param>
public Saga<T> ExpectNotSend<TMessage>(Action<TMessage> check)
{
return ExpectNotSend(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga replies with the given message type complying with the given predicate.
/// </summary>
public Saga<T> ExpectReply<TMessage>(Func<TMessage, ReplyOptions, bool> check = null)
{
testContext.AddExpectation(new ExpectReply<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga replies with the given message type complying with the given predicate.
/// </summary>
public Saga<T> ExpectReply<TMessage>(Func<TMessage, bool> check)
{
return ExpectReply((TMessage m, ReplyOptions _) => check(m));
}
/// <summary>
/// Check that the saga does not reply with the given message type complying with the given predicate.
/// </summary>
public Saga<T> ExpectNotReply<TMessage>(Func<TMessage, ReplyOptions, bool> check = null)
{
testContext.AddExpectation(new ExpectNotReply<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga does not reply with the given message type complying with the given predicate.
/// </summary>
public Saga<T> ExpectNotReply<TMessage>(Func<TMessage, bool> check)
{
return ExpectNotReply((TMessage m, ReplyOptions _) => check(m));
}
/// <summary>
/// Check that the saga doesn't forward a message to the given destination.
/// </summary>
public Saga<T> ExpectNotForwardCurrentMessageTo(Func<string, bool> check = null)
{
testContext.AddExpectation(new ExpectNotForwardCurrentMessageTo(check));
return this;
}
/// <summary>
/// Check that the saga forwards a message to the given destination.
/// </summary>
public Saga<T> ExpectForwardCurrentMessageTo(Func<string, bool> check = null)
{
testContext.AddExpectation(new ExpectForwardCurrentMessageTo(check));
return this;
}
/// <summary>
/// Check that the saga replies to the originator with the given message type.
/// </summary>
public Saga<T> ExpectReplyToOriginator<TMessage>(Func<TMessage, bool> check = null)
{
testContext.AddExpectation(new ExpectReplyToOriginator<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga replies to the originator with the given message type.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectReplyToOriginator<TMessage>(Action<TMessage> check)
{
return ExpectReplyToOriginator(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga publishes a message of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectPublish<TMessage>(Func<TMessage, PublishOptions, bool> check = null)
{
testContext.AddExpectation(new ExpectPublish<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga publishes a message of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectPublish<TMessage>(Func<TMessage, bool> check)
{
return ExpectPublish((TMessage m, PublishOptions _) => check(m));
}
/// <summary>
/// Check that the saga publishes a message of the given type complying with the given predicate.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectPublish<TMessage>(Action<TMessage> check)
{
return ExpectPublish(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga does not publish any messages of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectNotPublish<TMessage>(Func<TMessage, PublishOptions, bool> check = null)
{
testContext.AddExpectation(new ExpectNotPublish<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga does not publish any messages of the given type complying with the given predicate.
/// </summary>
public Saga<T> ExpectNotPublish<TMessage>(Func<TMessage, bool> check)
{
return ExpectNotPublish((TMessage m, PublishOptions _) => check(m));
}
/// <summary>
/// Check that the saga does not publish any messages of the given type complying with the given predicate.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectNotPublish<TMessage>(Action<TMessage> check)
{
return ExpectNotPublish(CheckActionToFunc(check));
}
/// <summary>
/// Initializes the given message type and checks all the expectations previously set up,
/// and then clears them for continued testing.
/// </summary>
public Saga<T> WhenHandling<TMessage>(Action<TMessage> initializeMessage = null)
{
var message = messageCreator.CreateInstance(initializeMessage);
return WhenHandling(message);
}
/// <summary>
/// Checks all the expectations previously set up, and then clears them for continued testing.
/// </summary>
public Saga<T> WhenHandling<TMessage>(TMessage message)
{
var invokers = saga.GetType().CreateInvokers(typeof(TMessage), typeof(IHandleMessages<>));
return When((s, c) => invokers.InvokeSerially(saga, message, c));
}
/// <summary>
/// Initializes the given timeout message type and checks all the expectations previously set up,
/// and then clears them for continued testing.
/// </summary>
public Saga<T> WhenHandlingTimeout<TMessage>(Action<TMessage> initializeMessage = null)
{
var message = messageCreator.CreateInstance(initializeMessage);
return WhenHandlingTimeout(message);
}
/// <summary>
/// Checks all the expectations previously set up, and then clears them for continued testing.
/// </summary>
public Saga<T> WhenHandlingTimeout<TMessage>(TMessage message)
{
var invokers = saga.GetType().CreateInvokers(typeof(TMessage), typeof(IHandleTimeouts<>));
return When((s, c) => invokers.InvokeSerially(saga, message, c));
}
/// <summary>
/// Uses the given delegate to invoke the saga, checking all the expectations previously set up,
/// and then clearing them for continued testing.
/// example: <c>When((saga, context) => s.Handle(new MyMessage(), context))</c>
/// </summary>
public Saga<T> When(Func<T, IMessageHandlerContext, Task> sagaIsInvoked)
{
sagaIsInvoked(saga, testContext).GetAwaiter().GetResult();
testContext.Validate();
testContext = new TestingContext(messageCreator, testContext.previousTimeouts.Concat(testContext.TimeoutMessages).ToArray());
return this;
}
/// <summary>
/// Uses the given delegate to select the message handler and invoking it with the given message. Checks all the expectations previously, and then clearing them for continued testing.
/// example: <c>When(s => s.Handle, new MyMessage())</c>
/// </summary>
public Saga<T> When<TMessage>(Func<T, Func<TMessage, IMessageHandlerContext, Task>> handlerSelector, TMessage message)
{
return When((s, context) => handlerSelector(s)(message, context));
}
/// <summary>
/// Uses the given delegate to select the message handler and invoking it with the specified message. Checks all the expectations previously, and then clearing them for continued testing.
/// example: <c>When<MyMessage>(s => s.Handle, m => { m.Value = 42 })</c>
/// </summary>
public Saga<T> When<TMessage>(Func<T, Func<TMessage, IMessageHandlerContext, Task>> handlerSelector, Action<TMessage> messageInitializer = null)
{
var message = (TMessage)messageCreator.CreateInstance(typeof(TMessage));
messageInitializer?.Invoke(message);
return When((s, context) => handlerSelector(s)(message, context));
}
/// <summary>
/// Expires requested timeouts for the saga by simulating that time has passed
/// and then clears out all previous expectations.
/// This will only invoke timeouts set with a <see cref="TimeSpan"/> argument.
/// </summary>
/// <param name="after">The amount of time that has passed to simulate.</param>
public Saga<T> WhenSagaTimesOut(TimeSpan after)
{
InvokeTimeouts(testContext.previousTimeouts
.Where(t => t.Within.HasValue)
.Where(t => t.Within <= after));
return this;
}
/// <summary>
/// Expires requested timeouts for the saga by simulating the passed in date and time
/// and then clears out all previous expectations.
/// This will only invoke timeouts set with a <see cref="DateTime"/> argument.
/// </summary>
/// <param name="at">The Date and time to simuluate.</param>
public Saga<T> WhenSagaTimesOut(DateTime at)
{
InvokeTimeouts(testContext.previousTimeouts
.Where(t => t.At.HasValue)
.Where(t => t.At <= at));
return this;
}
/// <summary>
/// Expires all requested timeouts for the saga and then clears out all previous expectations.
/// </summary>
public Saga<T> WhenSagaTimesOut()
{
InvokeTimeouts(testContext.previousTimeouts);
return this;
}
/// <summary>
/// Verifies that the saga has been completed.
/// </summary>
public Saga<T> ExpectSagaCompleted()
{
testContext.AddExpectation(new ExpectSagaCompleted<T>(saga, true));
return this;
}
/// <summary>
/// Verifies that the saga has not been completed.
/// </summary>
/// <returns></returns>
public Saga<T> ExpectSagaNotCompleted()
{
testContext.AddExpectation(new ExpectSagaCompleted<T>(saga, false));
return this;
}
/// <summary>
/// Verifies that the saga is setting the specified timeout
/// </summary>
public Saga<T> ExpectTimeoutToBeSetIn<TMessage>(Func<TMessage, TimeSpan, bool> check = null)
{
testContext.AddExpectation(new ExpectDelayDeliveryWith<TMessage>(check));
return this;
}
/// <summary>
/// Verifies that the saga is setting the specified timeout
/// </summary>
public Saga<T> ExpectTimeoutToBeSetIn<TMessage>(Action<TMessage, TimeSpan> check)
{
return ExpectTimeoutToBeSetIn(CheckActionToFunc(check));
}
/// <summary>
/// Verifies that the saga is not setting the specified timeout
/// </summary>
public Saga<T> ExpectNoTimeoutToBeSetIn<TMessage>(Func<TMessage, TimeSpan, bool> check = null)
{
testContext.AddExpectation(new ExpectNotDelayDeliveryWith<TMessage>(check));
return this;
}
/// <summary>
/// Verifies that the saga is setting the specified timeout
/// </summary>
public Saga<T> ExpectTimeoutToBeSetAt<TMessage>(Func<TMessage, DateTimeOffset, bool> check = null)
{
testContext.AddExpectation(new ExpectDoNotDeliverBefore<TMessage>(check));
return this;
}
/// <summary>
/// Verifies that the saga is setting the specified timeout
/// </summary>
public Saga<T> ExpectTimeoutToBeSetAt<TMessage>(Action<TMessage, DateTimeOffset> check)
{
return ExpectTimeoutToBeSetAt(CheckActionToFunc(check));
}
/// <summary>
/// Verifies that the saga is not setting the specified timeout
/// </summary>
public Saga<T> ExpectNoTimeoutToBeSetAt<TMessage>(Func<TMessage, DateTimeOffset, bool> check = null)
{
testContext.AddExpectation(new ExpectNotDoNotDeliverBefore<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga sends the given message type to its local queue
/// and that the message complies with the given predicate.
/// </summary>
public Saga<T> ExpectSendLocal<TMessage>(Func<TMessage, bool> check = null)
{
testContext.AddExpectation(new ExpectSendLocal<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga sends the given message type to its local queue
/// and that the message complies with the given predicate.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectSendLocal<TMessage>(Action<TMessage> check)
{
return ExpectSendLocal(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga does not send a message type to its local queue that complies with the given predicate.
/// </summary>
public Saga<T> ExpectNotSendLocal<TMessage>(Func<TMessage, bool> check = null)
{
testContext.AddExpectation(new ExpectNotSendLocal<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga does not send a message type to its local queue that complies with the given predicate.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectNotSendLocal<TMessage>(Action<TMessage> check)
{
return ExpectNotSendLocal(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga sends the given message type to the appropriate destination.
/// </summary>
public Saga<T> ExpectSendToDestination<TMessage>(Func<TMessage, string, bool> check = null)
{
testContext.AddExpectation(new ExpectSendToDestination<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga does not send the given message type to the given destination.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectNotSendToDestination<TMessage>(Action<TMessage, string> check)
{
return ExpectNotSendToDestination(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga does not send the given message type to the given destination.
/// </summary>
public Saga<T> ExpectNotSendToDestination<TMessage>(Func<TMessage, string, bool> check = null)
{
testContext.AddExpectation(new ExpectNotSendToDestination<TMessage>(check));
return this;
}
/// <summary>
/// Check that the saga sends the given message type to the appropriate destination.
/// </summary>
/// <param name="check">An action that performs assertions on the message.</param>
public Saga<T> ExpectSendToDestination<TMessage>(Action<TMessage, string> check)
{
return ExpectSendToDestination(CheckActionToFunc(check));
}
/// <summary>
/// Check that the saga data matches the given type and comlies with the given predicate.
/// </summary>
public Saga<T> ExpectSagaData<TSagaData>(Func<TSagaData, bool> check) where TSagaData : IContainSagaData
{
testContext.AddExpectation(new ExpectSagaData<TSagaData>(saga, check));
return this;
}
void InvokeTimeouts(IEnumerable<TimeoutMessage<object>> messages)
{
messages
.OrderBy(t => t.Within)
.ToList()
.ForEach(t =>
{
var messageType = messageCreator.GetMappedTypeFor(t.Message.GetType());
var invokers = saga.GetType().CreateInvokers(messageType, typeof(IHandleTimeouts<>));
invokers.InvokeSerially(saga, t.Message, testContext).GetAwaiter().GetResult();
});
testContext.Validate();
testContext = new TestingContext(messageCreator);
}
static Func<T1, bool> CheckActionToFunc<T1>(Action<T1> check)
{
return arg =>
{
check(arg);
return true;
};
}
static Func<T1, T2, bool> CheckActionToFunc<T1, T2>(Action<T1, T2> check)
{
return (arg1, arg2) =>
{
check(arg1, arg2);
return true;
};
}
readonly T saga;
MessageMapper messageCreator = new MessageMapper();
TestingContext testContext;
}
}