-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMessagesSubscriptionTests.cs
More file actions
747 lines (607 loc) · 30.6 KB
/
MessagesSubscriptionTests.cs
File metadata and controls
747 lines (607 loc) · 30.6 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.CoreRuntime;
using Cleipnir.ResilientFunctions.CoreRuntime.Invocation;
using Cleipnir.ResilientFunctions.CoreRuntime.Serialization;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Messaging;
using Cleipnir.ResilientFunctions.Queuing;
using Cleipnir.ResilientFunctions.Storage;
using Cleipnir.ResilientFunctions.Tests.Utils;
using Shouldly;
namespace Cleipnir.ResilientFunctions.Tests.Messaging.TestTemplates;
public abstract class MessagesSubscriptionTests
{
public abstract Task EventsSubscriptionSunshineScenario();
protected async Task EventsSubscriptionSunshineScenario(Task<IFunctionStore> functionStoreTask)
{
var functionId = TestStoredId.Create();
var functionStore = await functionStoreTask;
await functionStore.CreateFunction(
functionId,
"humanInstanceId",
Test.SimpleStoredParameter,
leaseExpiration: DateTime.UtcNow.Ticks,
postponeUntil: null,
timestamp: DateTime.UtcNow.Ticks,
parent: null,
owner: null
);
var messageStore = functionStore.MessageStore;
await messageStore
.GetMessages(functionId)
.SelectAsync(msgs => msgs.Any())
.ShouldBeFalseAsync();
var events = await messageStore.GetMessages(functionId);
events.ShouldBeEmpty();
await messageStore.AppendMessage(
functionId,
new StoredMessage("hello world". ToJson().ToUtf8Bytes(), typeof(string).SimpleQualifiedName().ToUtf8Bytes(), Position: 0)
);
events = await messageStore.GetMessages(functionId);
events.Count.ShouldBe(1);
DefaultSerializer
.Instance
.Deserialize(events[0].MessageContent, DefaultSerializer.Instance.ResolveType(events[0].MessageType)!)
.ShouldBe("hello world");
var skipPosition = events[0].Position;
var filteredEvents = (await messageStore.GetMessages(functionId)).Where(e => e.Position > skipPosition).ToList();
filteredEvents.ShouldBeEmpty();
await messageStore.AppendMessage(
functionId,
new StoredMessage("hello universe".ToJson().ToUtf8Bytes(), typeof(string).SimpleQualifiedName().ToUtf8Bytes(), Position: 0)
);
filteredEvents = (await messageStore.GetMessages(functionId)).Where(e => e.Position > skipPosition).ToList();
filteredEvents.Count.ShouldBe(1);
DefaultSerializer
.Instance
.Deserialize(filteredEvents[0].MessageContent, DefaultSerializer.Instance.ResolveType(filteredEvents[0].MessageType)!)
.ShouldBe("hello universe");
}
public abstract Task QueueClientCanPullSingleMessage();
protected async Task QueueClientCanPullSingleMessage(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientCanPullSingleMessage),
inner: (string _, Workflow workflow) => workflow.Message<string>()
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("test message");
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
result.ShouldBe("test message");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientCanPullMultipleMessages();
protected async Task QueueClientCanPullMultipleMessages(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch, watchdogCheckFrequency: TimeSpan.FromMilliseconds(100))
);
StoredId? storedId = null;
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientCanPullMultipleMessages),
inner: async Task<string> (string _, Workflow workflow) =>
{
storedId = workflow.StoredId;
var message1 = await workflow.Message<string>();
await workflow.Delay(TimeSpan.FromMilliseconds(100));
var message2 = await workflow.Message<string>();
await workflow.Delay(TimeSpan.FromMilliseconds(100));
var message3 = await workflow.Message<string>();
await workflow.Delay(TimeSpan.FromMilliseconds(100));
return $"{message1},{message2},{message3}";
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("first");
await messageWriter.AppendMessage("second");
await messageWriter.AppendMessage("third");
var result = await scheduled.Completion(TimeSpan.FromSeconds(5));
result.ShouldBe("first,second,third");
var results = await functionStore.EffectsStore.GetEffectResults([storedId!]);
var x = results.Values.Single();
Console.WriteLine(x);
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientReturnsNullAfterTimeout();
protected async Task QueueClientReturnsNullAfterTimeout(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientReturnsNullAfterTimeout),
inner: async Task<string?> (string _, Workflow workflow) =>
{
var message = await workflow.Message<string>(TimeSpan.FromMilliseconds(100));
return message;
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
// No message is sent, so the pull should timeout
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
result.ShouldBeNull();
var cp = await rFunc.ControlPanel("instanceId").ShouldNotBeNullAsync();
await cp.Messages.Append("hello world");
var restartResult = await cp.ScheduleRestart().Completion();
restartResult.ShouldBeNull();
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientPullsFiveMessagesAndTimesOutOnSixth();
protected async Task QueueClientPullsFiveMessagesAndTimesOutOnSixth(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var flag = new SyncedFlag();
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientPullsFiveMessagesAndTimesOutOnSixth),
inner: async Task<string> (string _, Workflow workflow) =>
{
var messages = new List<string>();
await flag.WaitForRaised();
for (var i = 0; i < 6; i++)
{
var message = await workflow.Message<string>(TimeSpan.FromMilliseconds(250));
messages.Add(message ?? "NULL");
}
return string.Join(",", messages);
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
// Send 5 messages
await messageWriter.AppendMessage("message1");
await messageWriter.AppendMessage("message2");
await messageWriter.AppendMessage("message3");
await messageWriter.AppendMessage("message4");
await messageWriter.AppendMessage("message5");
// Give FetchMessages background task time to fetch the messages
await Task.Delay(TimeSpan.FromSeconds(1.5));
flag.Raise();
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
result.ShouldBe("message1,message2,message3,message4,message5,NULL");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task OnlyFirstMessageWithSameIdempotencyKeyIsDeliveredAndBothAreRemovedAfterCompletion();
protected async Task OnlyFirstMessageWithSameIdempotencyKeyIsDeliveredAndBothAreRemovedAfterCompletion(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
StoredId? storedId = null;
var rFunc = functionsRegistry.RegisterFunc(
nameof(OnlyFirstMessageWithSameIdempotencyKeyIsDeliveredAndBothAreRemovedAfterCompletion),
inner: async Task<Tuple<string, string?>> (string _, Workflow workflow) =>
{
storedId = workflow.StoredId;
var message1 = await workflow.Message<string>();
var message2 = await workflow.Message<string>(TimeSpan.FromSeconds(1));
return Tuple.Create(message1, message2);
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
// Append two messages with the same idempotency key
await messageWriter.AppendMessage("first message", idempotencyKey: "duplicate-key");
await messageWriter.AppendMessage("second message", idempotencyKey: "duplicate-key");
await scheduled.Completion();
await BusyWait.Until(() => storedId != null);
await BusyWait.Until(async () => await functionStore.MessageStore.GetMessages(storedId!).SelectAsync(m => m.Count) == 0);
// Only the first message should be delivered
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
result.Item1.ShouldBe("first message");
result.Item2.ShouldBeNull();
// Verify both messages are removed from the store after completion
var messagesAfterCompletion = await functionStore.MessageStore.GetMessages(storedId!);
messagesAfterCompletion.ShouldBeEmpty();
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task MultipleIterationsWithDuplicateIdempotencyKeysProcessCorrectly();
protected async Task MultipleIterationsWithDuplicateIdempotencyKeysProcessCorrectly(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch, watchdogCheckFrequency: TimeSpan.FromMilliseconds(100))
);
StoredId? storedId = null;
var rFunc = functionsRegistry.RegisterFunc(
nameof(MultipleIterationsWithDuplicateIdempotencyKeysProcessCorrectly),
inner: async Task<string> (string _, Workflow workflow) =>
{
storedId = workflow.StoredId;
var receivedMessages = new List<string>();
// Pull messages until timeout - expecting 60 unique messages
var message = "";
while (message != "stop")
{
message = await workflow.Message<string>(
TimeSpan.FromMilliseconds(100)
);
if (message is null)
await workflow.Effect.Flush();
else if (message is "10" or "20" or "30" or "40")
{
await workflow.Delay(TimeSpan.FromMilliseconds(100));
receivedMessages.Add(message);
}
else if (message != "stop")
receivedMessages.Add(message);
}
return string.Join(",", receivedMessages);
}
);
// Schedule the function first
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
for (var iteration = 0; iteration < 100; iteration += 10)
for (var repeat = 0; repeat < 2; repeat++)
for (var i = 0; i < 10; i++)
await messageWriter.AppendMessage((iteration + i).ToString(), idempotencyKey: ((iteration + i) % 50).ToString());
await BusyWait.Until(() => storedId != null);
await BusyWait.Until(async () => await functionStore.MessageStore.GetMessages([storedId!]).SelectAsync(m => m[storedId!].Count) == 0, maxWait: TimeSpan.FromSeconds(30));
await messageWriter.AppendMessage("stop");
// Wait for completion
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(30));
var receivedMessages = result
.Split(',')
.Select(int.Parse)
.OrderBy(_ => _)
.ToList();
receivedMessages.Count.ShouldBe(50);
for (var i = 0; i < 50; i++)
receivedMessages[i].ShouldBe(i);
await BusyWait.Until(
async () => await functionStore.MessageStore.GetMessages(storedId!).SelectAsync(m => m.Count) == 0,
maxWait: TimeSpan.FromSeconds(30)
);
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientFilterParameterFiltersMessages();
protected async Task QueueClientFilterParameterFiltersMessages(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientFilterParameterFiltersMessages),
inner: async Task<string> (string _, Workflow workflow) =>
{
// Pull only messages that start with "even-"
var message1 = await workflow.Message<string>(
m => m.StartsWith("even-")
);
var message2 = await workflow.Message<string>(
m => m.StartsWith("even-")
);
var message3 = await workflow.Message<string>(
m => m.StartsWith("even-")
);
return $"{message1},{message2},{message3}";
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
// Send mixed messages - odd and even
await messageWriter.AppendMessage("odd-1");
await messageWriter.AppendMessage("even-2");
await messageWriter.AppendMessage("odd-3");
await messageWriter.AppendMessage("even-4");
await messageWriter.AppendMessage("odd-5");
await messageWriter.AppendMessage("even-6");
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
// Should only receive the even messages, filtered out the odd ones
result.ShouldBe("even-2,even-4,even-6");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientWorksWithCustomSerializer();
protected async Task QueueClientWorksWithCustomSerializer(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
// Use default serializer to ensure serialization works correctly
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch, messagesDefaultMaxWaitForCompletion: TimeSpan.FromMinutes(1))
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientWorksWithCustomSerializer),
inner: async Task<string> (string _, Workflow workflow) =>
{
// Pull different types of messages to verify serialization works
var message1 = await workflow.Message<string>();
var message2 = await workflow.Message<WrappedInt>();
var message3 = await workflow.Message<TestRecord>();
return $"{message1},{message2.Value},{message3.Value}";
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("hello");
await messageWriter.AppendMessage(new WrappedInt(42));
await messageWriter.AppendMessage(new TestRecord("world"));
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(10));
result.ShouldBe("hello,42,world");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
private record WrappedInt(int Value);
private record TestRecord(string Value);
public abstract Task BatchedMessagesAreDeliveredToMultipleFlows();
protected async Task BatchedMessagesAreDeliveredToMultipleFlows(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(BatchedMessagesAreDeliveredToMultipleFlows),
inner: (string _, Workflow workflow) => workflow.Message<string>()
);
// Send batched messages first
await rFunc.SendMessages(
[
new BatchedMessage("Instance#1", "hallo world 1", IdempotencyKey: "1"),
new BatchedMessage("Instance#2", "hallo world 2", IdempotencyKey: "2")
]
);
// Then schedule the workflows - they should pick up the messages
var scheduled1 = await rFunc.Schedule("Instance#1", "");
var scheduled2 = await rFunc.Schedule("Instance#2", "");
// Wait for completion
var result1 = await scheduled1.Completion(maxWait: TimeSpan.FromSeconds(10));
var result2 = await scheduled2.Completion(maxWait: TimeSpan.FromSeconds(10));
result1.ShouldBe("hallo world 1");
result2.ShouldBe("hallo world 2");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
private record Ping(int Number);
private record Pong(int Number);
public abstract Task QueueClientSupportsMultiFlowMessageExchange();
protected async Task QueueClientSupportsMultiFlowMessageExchange(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
functionStore = functionStore.WithPrefix("pingpong" + Guid.NewGuid().ToString("N"));
await functionStore.Initialize();
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch, messagesPullFrequency: TimeSpan.FromMilliseconds(10))
);
FuncRegistration<string, string>? pongRegistration = null;
FuncRegistration<string, string>? pingRegistration = null;
pingRegistration = functionsRegistry.RegisterFunc(
"PingFlow",
inner: async Task<string> (string _, Workflow workflow) =>
{
for (var i = 0; i < 10; i++)
{
await pongRegistration!.SendMessage("Pong", new Ping(i), idempotencyKey: $"Pong{i}");
await workflow.Message<Pong>(pong => pong.Number == i);
}
return "completed";
}
);
pongRegistration = functionsRegistry.RegisterFunc(
"PongFlow",
inner: async Task<string> (string _, Workflow workflow) =>
{
for (var i = 0; i < 10; i++)
{
await workflow.Message<Ping>(ping => ping.Number == i);
await pingRegistration!.SendMessage("Ping", new Pong(i), idempotencyKey: $"Ping{i}");
}
return "completed";
}
);
await pongRegistration.Schedule("Pong", "");
await pingRegistration.Schedule("Ping", "");
var pongCp = await pongRegistration.ControlPanel("Pong").ShouldNotBeNullAsync();
var pingCp = await pingRegistration.ControlPanel("Ping").ShouldNotBeNullAsync();
await pongCp.WaitForCompletion(allowPostponeAndSuspended: true);
await pingCp.WaitForCompletion(allowPostponeAndSuspended: true);
await pongCp.Refresh();
var pongResult = pongCp.Result;
pongResult.ShouldNotBeNull();
pongResult.ShouldBe("completed");
await pingCp.Refresh();
var pingResult = pingCp.Result;
pingResult.ShouldNotBeNull();
pingResult.ShouldBe("completed");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueManagerFailsOnMessageDeserializationError();
protected async Task QueueManagerFailsOnMessageDeserializationError(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
var unhandledExceptionHandler = new UnhandledExceptionHandler(unhandledExceptionCatcher.Catch);
var exceptionThrowingSerializer = new ExceptionThrowingEventSerializer(typeof(BadMessage));
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueManagerFailsOnMessageDeserializationError),
inner: async Task<string> (string _, Workflow workflow) =>
{
var flowTimeouts = new FlowTimeouts();
var flowsManager = new FlowsManager(functionStore, () => DateTime.UtcNow);
var flowState = flowsManager.CreateFlow(workflow.StoredId, flowTimeouts);
var queueManager = new QueueManager(
workflow.FlowId,
workflow.StoredId,
functionStore.MessageStore,
exceptionThrowingSerializer,
workflow.Effect,
flowState,
unhandledExceptionHandler,
flowTimeouts,
() => DateTime.UtcNow,
SettingsWithDefaults.Default
);
var queueClient = await queueManager.CreateQueueClient();
var message = await queueClient.Pull<GoodMessage>(
workflow,
workflow.Effect.CreateNextImplicitId()
);
return message.Value;
}
);
await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage(new BadMessage("will-fail"), idempotencyKey: "bad-message");
var controlPanel = await rFunc.ControlPanel("instanceId").ShouldNotBeNullAsync();
await controlPanel.BusyWaitUntil(c => c.Status == Status.Failed, maxWait: TimeSpan.FromSeconds(10));
controlPanel.Status.ShouldBe(Status.Failed);
unhandledExceptionCatcher.ThrownExceptions.Count.ShouldBeGreaterThanOrEqualTo(1);
var deserializationException = unhandledExceptionCatcher.ThrownExceptions
.Select(e => e.InnerException)
.OfType<DeserializationException>()
.FirstOrDefault();
deserializationException.ShouldNotBeNull();
deserializationException.Message.ShouldBe("Deserialization failed for BadMessage");
}
public abstract Task RegisteredTimeoutIsRemovedWhenPullingMessage();
protected async Task RegisteredTimeoutIsRemovedWhenPullingMessage(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
var unhandledExceptionHandler = new UnhandledExceptionHandler(unhandledExceptionCatcher.Catch);
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
StoredId? storedId = null;
var rFunc = functionsRegistry.RegisterFunc(
nameof(RegisteredTimeoutIsRemovedWhenPullingMessage),
inner: async Task<string> (string _, Workflow workflow) =>
{
storedId = workflow.StoredId;
var minimumTimeout = new FlowTimeouts();
var flowsManager = new FlowsManager(functionStore, () => DateTime.UtcNow);
var flowState = flowsManager.CreateFlow(workflow.StoredId, minimumTimeout);
var queueManager = new QueueManager(
workflow.FlowId,
workflow.StoredId,
functionStore.MessageStore,
DefaultSerializer.Instance,
workflow.Effect,
flowState,
unhandledExceptionHandler,
minimumTimeout,
() => DateTime.UtcNow,
SettingsWithDefaults.Default
);
var queueClient = await queueManager.CreateQueueClient();
// Verify timeout is not set before pull
minimumTimeout.MinimumTimeout.ShouldBeNull();
var message = await queueClient.Pull<string>(
workflow,
workflow.Effect.CreateNextImplicitId(),
timeout: TimeSpan.FromMinutes(5)
);
// Verify timeout is removed after successful pull
minimumTimeout.MinimumTimeout.ShouldBeNull();
return message!;
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("test message");
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
result.ShouldBe("test message");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task PullEnvelopeReturnsEnvelopeWithReceiverAndSender();
protected async Task PullEnvelopeReturnsEnvelopeWithReceiverAndSender(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
var unhandledExceptionHandler = new UnhandledExceptionHandler(unhandledExceptionCatcher.Catch);
using var functionsRegistry = new FunctionsRegistry(
functionStore,
new Settings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(PullEnvelopeReturnsEnvelopeWithReceiverAndSender),
inner: async Task<string> (string _, Workflow workflow) =>
{
var flowTimeouts = new FlowTimeouts();
var flowsManager = new FlowsManager(functionStore, () => DateTime.UtcNow);
var flowState = flowsManager.CreateFlow(workflow.StoredId, flowTimeouts);
var queueManager = new QueueManager(
workflow.FlowId,
workflow.StoredId,
functionStore.MessageStore,
DefaultSerializer.Instance,
workflow.Effect,
flowState,
unhandledExceptionHandler,
flowTimeouts,
() => DateTime.UtcNow,
SettingsWithDefaults.Default
);
var queueClient = await queueManager.CreateQueueClient();
// Pull envelope for specific receiver
var envelope = await queueClient.PullEnvelope<string>(
workflow,
workflow.Effect.CreateNextImplicitId(),
filter: _ => true
);
return $"{envelope.Message}|{envelope.Receiver}|{envelope.Sender}";
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("test message", receiver: "receiver1", sender: "sender1");
var result = await scheduled.Completion(maxWait: TimeSpan.FromSeconds(5));
result.ShouldBe("test message|receiver1|sender1");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
private record GoodMessage(string Value);
private record BadMessage(string Value);
private class ExceptionThrowingEventSerializer : ISerializer
{
private readonly Type _failDeserializationOnType;
public ExceptionThrowingEventSerializer(Type failDeserializationOnType)
=> _failDeserializationOnType = failDeserializationOnType;
public byte[] Serialize(object value, Type type)
=> DefaultSerializer.Instance.Serialize(value, type);
public object Deserialize(byte[] json, Type type)
{
if (type == _failDeserializationOnType)
throw new DeserializationException("Deserialization failed for BadMessage", new Exception("Inner cause"));
return DefaultSerializer.Instance.Deserialize(json, type);
}
}
}