-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEffectStoreTests.cs
More file actions
890 lines (790 loc) · 33 KB
/
Copy pathEffectStoreTests.cs
File metadata and controls
890 lines (790 loc) · 33 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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
using System;
using System.Linq;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Storage;
using Cleipnir.ResilientFunctions.Tests.Utils;
using Shouldly;
using static Cleipnir.ResilientFunctions.Storage.CrudOperation;
namespace Cleipnir.ResilientFunctions.Tests.TestTemplates;
public abstract class EffectStoreTests
{
public abstract Task SunshineScenarioTest();
protected async Task SunshineScenarioTest(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
await store
.GetEffectResults(functionId)
.SelectAsync(l => l.Any())
.ShouldBeFalseAsync();
await store.SetEffectResult(functionId, storedEffect1.ToStoredChange(functionId, Insert), session: null);
var storedEffects = await store
.GetEffectResults(functionId);
storedEffects.Count.ShouldBe(1);
var se = storedEffects[0];
se.ShouldBe(storedEffect1);
await store.SetEffectResult(functionId, storedEffect2.ToStoredChange(functionId, Insert), session: null);
storedEffects = await store.GetEffectResults(functionId);
storedEffects.Count.ShouldBe(2);
storedEffects.Any(s => s == storedEffect1).ShouldBeTrue();
storedEffects.Any(s => s == storedEffect2).ShouldBeTrue();
await store.SetEffectResult(functionId, storedEffect2.ToStoredChange(functionId, Update), session: null);
await store.GetEffectResults(functionId);
await store.SetEffectResult(functionId, storedEffect2.ToStoredChange(functionId, Update), session: null);
storedEffects = await store.GetEffectResults(functionId);
storedEffects.Count.ShouldBe(2);
storedEffects.Any(s => s == storedEffect1).ShouldBeTrue();
storedEffects.Any(s => s == storedEffect2).ShouldBeTrue();
}
public abstract Task SingleEffectWithResultLifeCycle();
protected async Task SingleEffectWithResultLifeCycle(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var effect = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
await store.GetEffectResults(functionId)
.SelectAsync(r => r.Any())
.ShouldBeFalseAsync();
await store.SetEffectResult(functionId, effect.ToStoredChange(functionId, Insert), session: null);
var storedEffect = await store.GetEffectResults(functionId).SelectAsync(r => r.Single());
storedEffect.ShouldBe(effect);
effect = effect with { WorkStatus = WorkStatus.Completed, Result = "Hello World".ToUtf8Bytes() };
await store.SetEffectResult(functionId, effect.ToStoredChange(functionId, Update), session: null);
storedEffect = await store.GetEffectResults(functionId).SelectAsync(r => r.Single());
storedEffect.EffectId.ShouldBe(effect.EffectId);
storedEffect.StoredException.ShouldBe(effect.StoredException);
storedEffect.Result!.ToStringFromUtf8Bytes().ShouldBe(effect.Result.ToStringFromUtf8Bytes());
storedEffect.WorkStatus.ShouldBe(effect.WorkStatus);
}
public abstract Task SingleFailingEffectLifeCycle();
protected async Task SingleFailingEffectLifeCycle(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedException = new StoredException(
"Some Exception Message",
"SomeStackTrace",
"Some Exception Type"
);
var storedEffect = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
await store.GetEffectResults(functionId)
.SelectAsync(r => r.Any())
.ShouldBeFalseAsync();
await store.SetEffectResult(functionId, storedEffect.ToStoredChange(functionId, Insert), session: null);
var effect = await store.GetEffectResults(functionId).SelectAsync(r => r.Single());
effect.ShouldBe(storedEffect);
storedEffect = storedEffect with { WorkStatus = WorkStatus.Completed, StoredException = storedException };
await store.SetEffectResult(functionId, storedEffect.ToStoredChange(functionId, Update), session: null);
effect = await store.GetEffectResults(functionId).SelectAsync(r => r.Single());
effect.ShouldBe(storedEffect);
}
public abstract Task EffectCanBeDeleted();
protected async Task EffectCanBeDeleted(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
await store.SetEffectResult(functionId, storedEffect1.ToStoredChange(functionId, Insert), session: null);
await store.SetEffectResult(functionId, storedEffect2.ToStoredChange(functionId, Insert), session: null);
await store
.GetEffectResults(functionId)
.SelectAsync(sas => sas.Count() == 2)
.ShouldBeTrueAsync();
await store.DeleteEffectResult(functionId, storedEffect2.EffectId, storageSession: null);
var storedEffects = await store.GetEffectResults(functionId);
storedEffects.Count.ShouldBe(1);
storedEffects[0].EffectId.ShouldBe(storedEffect1.EffectId);
await store.DeleteEffectResult(functionId, storedEffect2.EffectId, storageSession: null);
storedEffects = await store.GetEffectResults(functionId);
storedEffects.Count.ShouldBe(1);
storedEffects[0].EffectId.ShouldBe(storedEffect1.EffectId);
await store.DeleteEffectResult(functionId, storedEffect1.EffectId, storageSession: null);
await store
.GetEffectResults(functionId)
.SelectAsync(sas => sas.Any())
.ShouldBeFalseAsync();
}
public abstract Task DeleteFunctionIdDeletesAllRelatedEffects();
protected async Task DeleteFunctionIdDeletesAllRelatedEffects(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var otherFunctionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
await functionStore.CreateFunction(
otherFunctionId,
"OtherInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
await store.SetEffectResult(functionId, storedEffect1.ToStoredChange(functionId, Insert), session: null);
await store.SetEffectResult(functionId, storedEffect2.ToStoredChange(functionId, Insert), session: null);
await store.SetEffectResult(otherFunctionId, storedEffect1.ToStoredChange(otherFunctionId, Insert), session: null);
await store
.GetEffectResults(functionId)
.SelectAsync(sas => sas.Count() == 2)
.ShouldBeTrueAsync();
await store.Remove(functionId);
await store
.GetEffectResults(functionId)
.SelectAsync(e => e.Any())
.ShouldBeFalseAsync();
await store
.GetEffectResults(otherFunctionId)
.SelectAsync(e => e.Any())
.ShouldBeTrueAsync();
}
public abstract Task TruncateDeletesAllEffects();
protected async Task TruncateDeletesAllEffects(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var otherFunctionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
await functionStore.CreateFunction(
otherFunctionId,
"OtherInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
await store.SetEffectResult(functionId, storedEffect1.ToStoredChange(functionId, Insert), session: null);
await store.SetEffectResult(functionId, storedEffect2.ToStoredChange(functionId, Insert), session: null);
await store.SetEffectResult(otherFunctionId, storedEffect1.ToStoredChange(otherFunctionId, Insert), session: null);
await store.Truncate();
await store
.GetEffectResults(functionId)
.SelectAsync(e => e.Any())
.ShouldBeFalseAsync();
await store
.GetEffectResults(otherFunctionId)
.SelectAsync(e => e.Any())
.ShouldBeFalseAsync();
}
public abstract Task BulkInsertTest();
protected async Task BulkInsertTest(Task<IFunctionStore> storeTask)
{
var storedId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
storedId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: "some result 1".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: "some result 2".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
await store.SetEffectResults(
storedId,
[storedEffect1.ToStoredChange(storedId, Insert), storedEffect2.ToStoredChange(storedId, Insert)],
session: null
);
var effects = await store.GetEffectResults(storedId);
effects.Count.ShouldBe(2);
var effect1 = effects.Single(e => e.EffectId == storedEffect1.EffectId);
effect1.Result.ShouldBe("some result 1".ToUtf8Bytes());
var effect2 = effects.Single(e => e.EffectId == storedEffect2.EffectId);
effect2.Result.ShouldBe("some result 2".ToUtf8Bytes());
}
public abstract Task BulkInsertAndDeleteTest();
protected async Task BulkInsertAndDeleteTest(Task<IFunctionStore> storeTask)
{
var storedId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
storedId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: "some result 1".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: "some result 2".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
var storedEffect3 = new StoredEffect(
3.ToEffectId(),
WorkStatus.Completed,
Result: "some result 3".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
await store.SetEffectResults(storedId, [storedEffect1.ToStoredChange(storedId, Insert), storedEffect2.ToStoredChange(storedId, Insert)], session: null);
await store.SetEffectResults(
storedId,
changes: [
storedEffect3.ToStoredChange(storedId, Insert),
StoredEffectChange.CreateDelete(storedId, storedEffect1.EffectId),
StoredEffectChange.CreateDelete(storedId, storedEffect2.EffectId)
],
session: null
);
var effects = await store.GetEffectResults(storedId);
effects.Count.ShouldBe(1);
var effect3 = effects.Single(e => e.EffectId == storedEffect3.EffectId);
effect3.Result.ShouldBe("some result 3".ToUtf8Bytes());
}
public abstract Task BulkDeleteTest();
protected async Task BulkDeleteTest(Task<IFunctionStore> storeTask)
{
var storedId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
storedId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: "some result 1".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: "some result 2".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
await store.SetEffectResults(
storedId,
changes: [storedEffect1.ToStoredChange(storedId, Insert), storedEffect2.ToStoredChange(storedId, Insert)],
session: null
);
await store.SetEffectResults(
storedId,
changes: [
StoredEffectChange.CreateDelete(storedId, storedEffect1.EffectId),
StoredEffectChange.CreateDelete(storedId, storedEffect2.EffectId)
],
session: null
);
var effects = await store.GetEffectResults(storedId);
effects.Count.ShouldBe(0);
}
public abstract Task UpsertEmptyCollectionOfEffectsDoesNotThrowException();
protected async Task UpsertEmptyCollectionOfEffectsDoesNotThrowException(Task<IFunctionStore> storeTask)
{
var storedId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
storedId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
await store.SetEffectResults(
storedId,
changes: [],
session: null
);
}
public abstract Task EffectsForDifferentIdsCanBeFetched();
protected async Task EffectsForDifferentIdsCanBeFetched(Task<IFunctionStore> storeTask)
{
var id1 = TestStoredId.Create();
var id2 = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
id1,
"HumanInstanceId1",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
await functionStore.CreateFunction(
id2,
"HumanInstanceId2",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
await store.SetEffectResult(id1, storedEffect1.ToStoredChange(id1, Insert), session: null);
await store.SetEffectResult(id1, storedEffect2.ToStoredChange(id1, Insert), session: null);
await store.SetEffectResult(id2, storedEffect1.ToStoredChange(id2, Insert), session: null);
await store.SetEffectResult(id2, storedEffect2.ToStoredChange(id2, Insert), session: null);
var results = await store.GetEffectResults([id1, id2]);
results.Count.ShouldBe(2);
var resultsId1 = results[id1];
resultsId1.Count.ShouldBe(2);
resultsId1.Any(r => r.EffectId == storedEffect1.EffectId).ShouldBeTrue();
resultsId1.Any(r => r.EffectId == storedEffect2.EffectId).ShouldBeTrue();
var resultsId2 = results[id2];
resultsId2.Count.ShouldBe(2);
resultsId2.Any(r => r.EffectId == storedEffect1.EffectId).ShouldBeTrue();
resultsId2.Any(r => r.EffectId == storedEffect2.EffectId).ShouldBeTrue();
}
public abstract Task OverwriteExistingEffectWorks();
protected async Task OverwriteExistingEffectWorks(Task<IFunctionStore> storeTask)
{
var store = await storeTask;
var effectStore = store.EffectsStore;
var storedId = TestStoredId.Create();
var storageSession = await store.CreateFunction(
storedId,
"SomeInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
await effectStore.SetEffectResult(storedId, storedEffect1.ToStoredChange(storedId, Insert), storageSession);
await effectStore.SetEffectResult(storedId, storedEffect2.ToStoredChange(storedId, Update), storageSession);
var storedEffects = await effectStore.GetEffectResults(storedId);
var storedEffect = storedEffects.Single();
storedEffect.EffectId.ShouldBe(1.ToEffectId());
storedEffect.WorkStatus.ShouldBe(WorkStatus.Completed);
}
public abstract Task StoreCanHandleMultipleEffectsWithSameIdOnDifferentSessions();
protected async Task StoreCanHandleMultipleEffectsWithSameIdOnDifferentSessions(Task<IFunctionStore> storeTask)
{
var store = await storeTask;
var effectStore = store.EffectsStore;
var storedId = TestStoredId.Create();
var crashingReplicaId = ReplicaId.NewId();
var storageSession1 = await store.CreateFunction(
storedId,
"SomeInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: crashingReplicaId
);
await store.RescheduleCrashedFunctions(crashingReplicaId);
var storageSession2 = await store.ClaimFunction(
storedId,
owner: ReplicaId.NewId()
).SelectAsync(s => s!.StorageSession);
var storedEffect1 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var storedEffect2 = new StoredEffect(
1.ToEffectId(),
WorkStatus.Completed,
Result: null,
StoredException: null,
Alias: null
);
try
{
await effectStore.SetEffectResult(storedId, storedEffect1.ToStoredChange(storedId, Insert), storageSession1);
}
catch (Exception)
{
// ignored
}
try
{
await effectStore.SetEffectResult(storedId, storedEffect2.ToStoredChange(storedId, Insert),
storageSession2);
}
catch (Exception)
{
//ignored
}
var storedEffects = await effectStore.GetEffectResults(storedId);
var storedEffect = storedEffects.Single();
storedEffect.EffectId.ShouldBe(1.ToEffectId());
(storedEffect.WorkStatus is WorkStatus.Completed or WorkStatus.Started).ShouldBeTrue();
}
public abstract Task MultipleSequentialUpdatesWithoutRefresh();
protected async Task MultipleSequentialUpdatesWithoutRefresh(Task<IFunctionStore> storeTask)
{
var store = await storeTask;
var effectStore = store.EffectsStore;
var storedId = TestStoredId.Create();
// Create initial session
var session = await store.CreateFunction(storedId, "instance", null, null, 0, null, ReplicaId.NewId());
// Sequential updates - each should increment version properly
var effect1 = new StoredEffect(1.ToEffectId(), WorkStatus.Completed, "result1".ToUtf8Bytes(), null, Alias: null);
await effectStore.SetEffectResult(storedId, effect1.ToStoredChange(storedId, Insert), session);
var effect2 = new StoredEffect(2.ToEffectId(), WorkStatus.Completed, "result2".ToUtf8Bytes(), null, Alias: null);
await effectStore.SetEffectResult(storedId, effect2.ToStoredChange(storedId, Insert), session);
var effect3 = new StoredEffect(3.ToEffectId(), WorkStatus.Completed, "result3".ToUtf8Bytes(), null, Alias: null);
await effectStore.SetEffectResult(storedId, effect3.ToStoredChange(storedId, Insert), session);
// Verify all three effects were persisted
var effects = await effectStore.GetEffectResults(storedId);
effects.Count.ShouldBe(3);
effects.Any(e => e.EffectId == effect1.EffectId).ShouldBeTrue();
effects.Any(e => e.EffectId == effect2.EffectId).ShouldBeTrue();
effects.Any(e => e.EffectId == effect3.EffectId).ShouldBeTrue();
}
public abstract Task StoreHandlesLargeNumberOfEffects();
protected async Task StoreHandlesLargeNumberOfEffects(Task<IFunctionStore> storeTask)
{
var store = await storeTask;
var effectStore = store.EffectsStore;
var storedId = TestStoredId.Create();
// Create initial session
var session = await store.CreateFunction(storedId, "instance", null, null, 0, null, ReplicaId.NewId());
// Insert 100 effects
const int effectCount = 100;
for (int i = 0; i < effectCount; i++)
{
var effect = new StoredEffect(
i.ToEffectId(),
WorkStatus.Completed,
$"result{i}".ToUtf8Bytes(),
null,
Alias: null
);
await effectStore.SetEffectResult(storedId, effect.ToStoredChange(storedId, Insert), session);
}
// Verify all effects were persisted
var effects = await effectStore.GetEffectResults(storedId);
effects.Count.ShouldBe(effectCount);
// Verify a few specific effects
effects.Any(e => e.EffectId == 0.ToEffectId()).ShouldBeTrue();
effects.Any(e => e.EffectId == 50.ToEffectId()).ShouldBeTrue();
effects.Any(e => e.EffectId == 99.ToEffectId()).ShouldBeTrue();
}
public abstract Task EffectsSerializeAndDeserializeCorrectly();
protected async Task EffectsSerializeAndDeserializeCorrectly(Task<IFunctionStore> storeTask)
{
var storedId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
storedId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
// Create effects with various edge cases
var effectWithNullResult = new StoredEffect(
1.ToEffectId(),
WorkStatus.Started,
Result: null,
StoredException: null,
Alias: null
);
var effectWithLargeResult = new StoredEffect(
2.ToEffectId(),
WorkStatus.Completed,
Result: new byte[10000], // 10KB
StoredException: null,
Alias: null
);
var effectWithException = new StoredEffect(
3.ToEffectId(),
WorkStatus.Failed,
Result: null,
StoredException: new StoredException(
"Message with special chars: \n\t\r\"'\\",
"Stack trace with\nmultiple\nlines",
"System.InvalidOperationException"
),
Alias: null
);
var effectWithSpecialChars = new StoredEffect(
4.ToEffectId(),
WorkStatus.Completed,
Result: "Special chars: 🚀 \n\t\r\"'\\".ToUtf8Bytes(),
StoredException: null,
Alias: null
);
// Insert all effects
await store.SetEffectResult(storedId, effectWithNullResult.ToStoredChange(storedId, Insert), null);
await store.SetEffectResult(storedId, effectWithLargeResult.ToStoredChange(storedId, Insert), null);
await store.SetEffectResult(storedId, effectWithException.ToStoredChange(storedId, Insert), null);
await store.SetEffectResult(storedId, effectWithSpecialChars.ToStoredChange(storedId, Insert), null);
// Retrieve and verify
var effects = await store.GetEffectResults(storedId);
effects.Count.ShouldBe(4);
var retrievedNull = effects.Single(e => e.EffectId == 1.ToEffectId());
retrievedNull.Result.ShouldBeNull();
retrievedNull.StoredException.ShouldBeNull();
var retrievedLarge = effects.Single(e => e.EffectId == 2.ToEffectId());
retrievedLarge.Result.ShouldNotBeNull();
retrievedLarge.Result!.Length.ShouldBe(10000);
var retrievedException = effects.Single(e => e.EffectId == 3.ToEffectId());
retrievedException.StoredException.ShouldNotBeNull();
retrievedException.StoredException!.ExceptionMessage.ShouldBe("Message with special chars: \n\t\r\"'\\");
var retrievedSpecial = effects.Single(e => e.EffectId == 4.ToEffectId());
retrievedSpecial.Result!.ToStringFromUtf8Bytes().ShouldBe("Special chars: 🚀 \n\t\r\"'\\");
}
public abstract Task MixedInsertUpdateDeleteInSequence();
protected async Task MixedInsertUpdateDeleteInSequence(Task<IFunctionStore> storeTask)
{
var store = await storeTask;
var effectStore = store.EffectsStore;
var storedId = TestStoredId.Create();
// Create initial session
var session = await store.CreateFunction(storedId, "instance", null, null, 0, null, ReplicaId.NewId());
// INSERT effect1
var effect1 = new StoredEffect(1.ToEffectId(), WorkStatus.Started, null, null, Alias: null);
await effectStore.SetEffectResult(storedId, effect1.ToStoredChange(storedId, Insert), session);
// UPDATE effect1
effect1 = effect1 with { WorkStatus = WorkStatus.Completed, Result = "done".ToUtf8Bytes() };
await effectStore.SetEffectResult(storedId, effect1.ToStoredChange(storedId, Update), session);
// DELETE effect2 (doesn't exist - should be no-op)
await effectStore.DeleteEffectResult(storedId, 2.ToEffectId(), session);
// INSERT effect3
var effect3 = new StoredEffect(3.ToEffectId(), WorkStatus.Completed, null, null, Alias: null);
await effectStore.SetEffectResult(storedId, effect3.ToStoredChange(storedId, Insert), session);
// UPDATE effect1 again
effect1 = effect1 with { Result = "done2".ToUtf8Bytes() };
await effectStore.SetEffectResult(storedId, effect1.ToStoredChange(storedId, Update), session);
// INSERT effect2
var effect2 = new StoredEffect(2.ToEffectId(), WorkStatus.Started, null, null, Alias: null);
await effectStore.SetEffectResult(storedId, effect2.ToStoredChange(storedId, Insert), session);
// DELETE effect3
await effectStore.DeleteEffectResult(storedId, effect3.EffectId, session);
// Verify final state: effect1 (updated) and effect2 should exist, effect3 should be deleted
var effects = await effectStore.GetEffectResults(storedId);
effects.Count.ShouldBe(2);
var finalEffect1 = effects.Single(e => e.EffectId == 1.ToEffectId());
finalEffect1.WorkStatus.ShouldBe(WorkStatus.Completed);
finalEffect1.Result!.ToStringFromUtf8Bytes().ShouldBe("done2");
effects.Any(e => e.EffectId == 2.ToEffectId()).ShouldBeTrue();
effects.Any(e => e.EffectId == 3.ToEffectId()).ShouldBeFalse();
}
public abstract Task EffectWithAliasCanBePersistedAndFetched();
protected async Task EffectWithAliasCanBePersistedAndFetched(Task<IFunctionStore> storeTask)
{
var functionId = TestStoredId.Create();
var functionStore = await storeTask;
await functionStore.CreateFunction(
functionId,
"HumanInstanceId",
param: null,
postponeUntil: null,
timestamp: 0,
parent: null,
owner: ReplicaId.NewId()
);
var store = functionStore.EffectsStore;
// Create effect with alias
var effectWithAlias = new StoredEffect(
1.ToEffectId(),
WorkStatus.Completed,
Result: "Hello World".ToUtf8Bytes(),
StoredException: null,
Alias: "MyAlias"
);
// Persist effect
await store.SetEffectResult(functionId, effectWithAlias.ToStoredChange(functionId, Insert), session: null);
// Fetch effect
var storedEffects = await store.GetEffectResults(functionId);
storedEffects.Count.ShouldBe(1);
var retrievedEffect = storedEffects[0];
retrievedEffect.EffectId.ShouldBe(effectWithAlias.EffectId);
retrievedEffect.WorkStatus.ShouldBe(effectWithAlias.WorkStatus);
retrievedEffect.Result!.ToStringFromUtf8Bytes().ShouldBe("Hello World");
retrievedEffect.Alias.ShouldBe("MyAlias");
}
}