5
5
using Cleipnir . ResilientFunctions . Domain ;
6
6
using Cleipnir . ResilientFunctions . Helpers ;
7
7
using Cleipnir . ResilientFunctions . Storage ;
8
+ using Cleipnir . ResilientFunctions . Tests . Utils ;
8
9
using Shouldly ;
9
10
10
11
namespace Cleipnir . ResilientFunctions . Tests . TestTemplates . WatchDogsTests ;
@@ -14,13 +15,13 @@ public abstract class ReplicaWatchdogTests
14
15
public abstract Task SunshineScenario ( ) ;
15
16
public async Task SunshineScenario ( Task < IFunctionStore > storeTask )
16
17
{
17
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
18
+ var functionStore = await storeTask ;
19
+ var store = functionStore . ReplicaStore ;
18
20
var replicaId1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
19
21
using var watchdog1 = new ReplicaWatchdog (
20
22
replicaId1 ,
21
- store ,
22
- checkFrequency : TimeSpan . FromHours ( 1 ) ,
23
- onStrikeOut : _ => { }
23
+ functionStore ,
24
+ checkFrequency : TimeSpan . FromHours ( 1 )
24
25
) ;
25
26
await watchdog1 . Initialize ( ) ;
26
27
var allReplicas = await store . GetAll ( ) ;
@@ -31,9 +32,8 @@ public async Task SunshineScenario(Task<IFunctionStore> storeTask)
31
32
var replicaId2 = new ClusterInfo ( Guid . Parse ( "20000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
32
33
using var watchdog2 = new ReplicaWatchdog (
33
34
replicaId2 ,
34
- store ,
35
- checkFrequency : TimeSpan . FromHours ( 1 ) ,
36
- onStrikeOut : _ => { }
35
+ functionStore ,
36
+ checkFrequency : TimeSpan . FromHours ( 1 )
37
37
) ;
38
38
await watchdog2 . Initialize ( ) ;
39
39
allReplicas = await store . GetAll ( ) ;
@@ -68,13 +68,13 @@ public async Task SunshineScenario(Task<IFunctionStore> storeTask)
68
68
public abstract Task ReplicaWatchdogStartResultsInAddedReplicaInStore ( ) ;
69
69
public async Task ReplicaWatchdogStartResultsInAddedReplicaInStore ( Task < IFunctionStore > storeTask )
70
70
{
71
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
71
+ var functionStore = await storeTask ;
72
+ var store = functionStore . ReplicaStore ;
72
73
var replicaId1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
73
74
using var watchdog1 = new ReplicaWatchdog (
74
75
replicaId1 ,
75
- store ,
76
- checkFrequency : TimeSpan . FromHours ( 1 ) ,
77
- onStrikeOut : _ => { }
76
+ functionStore ,
77
+ checkFrequency : TimeSpan . FromHours ( 1 )
78
78
) ;
79
79
await watchdog1 . Start ( ) ;
80
80
var allReplicas = await store . GetAll ( ) ;
@@ -83,9 +83,8 @@ public async Task ReplicaWatchdogStartResultsInAddedReplicaInStore(Task<IFunctio
83
83
var replicaId2 = new ClusterInfo ( Guid . Parse ( "20000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
84
84
using var watchdog2 = new ReplicaWatchdog (
85
85
replicaId2 ,
86
- store ,
87
- checkFrequency : TimeSpan . FromHours ( 1 ) ,
88
- onStrikeOut : _ => { }
86
+ functionStore ,
87
+ checkFrequency : TimeSpan . FromHours ( 1 )
89
88
) ;
90
89
await watchdog2 . Start ( ) ;
91
90
allReplicas = await store . GetAll ( ) ;
@@ -95,24 +94,23 @@ public async Task ReplicaWatchdogStartResultsInAddedReplicaInStore(Task<IFunctio
95
94
public abstract Task StrikedOutReplicaIsRemovedFromStore ( ) ;
96
95
public async Task StrikedOutReplicaIsRemovedFromStore ( Task < IFunctionStore > storeTask )
97
96
{
98
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
97
+ var functionStore = await storeTask ;
98
+ var store = functionStore . ReplicaStore ;
99
99
var toBeStrikedOut = ReplicaId . NewId ( ) ;
100
- ReplicaId ? strikedOut = null ;
100
+
101
101
await store . Insert ( toBeStrikedOut ) ;
102
102
var replicaId1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
103
103
using var watchdog1 = new ReplicaWatchdog (
104
104
replicaId1 ,
105
- store ,
106
- checkFrequency : TimeSpan . FromHours ( 1 ) ,
107
- onStrikeOut : id => strikedOut = id
105
+ functionStore ,
106
+ checkFrequency : TimeSpan . FromHours ( 1 )
108
107
) ;
109
108
await watchdog1 . Initialize ( ) ;
110
109
await watchdog1 . PerformIteration ( ) ;
111
- strikedOut . ShouldBeNull ( ) ;
110
+ await store . GetAll ( ) . SelectAsync ( rs => rs . Count == 2 ) . ShouldBeTrueAsync ( ) ;
112
111
await watchdog1 . PerformIteration ( ) ;
113
- strikedOut . ShouldBeNull ( ) ;
112
+ await store . GetAll ( ) . SelectAsync ( rs => rs . Count == 2 ) . ShouldBeTrueAsync ( ) ;
114
113
await watchdog1 . PerformIteration ( ) ;
115
- strikedOut . ShouldBe ( toBeStrikedOut ) ;
116
114
117
115
var all = await store . GetAll ( ) ;
118
116
all . Count . ShouldBe ( 1 ) ;
@@ -122,14 +120,14 @@ public async Task StrikedOutReplicaIsRemovedFromStore(Task<IFunctionStore> store
122
120
public abstract Task RunningWatchdogUpdatesItsOwnHeartbeat ( ) ;
123
121
public async Task RunningWatchdogUpdatesItsOwnHeartbeat ( Task < IFunctionStore > storeTask )
124
122
{
125
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
126
- var anyStrikesOut = false ;
123
+ var functionStore = await storeTask ;
124
+ var store = functionStore . ReplicaStore ;
125
+
127
126
var replicaId1 = new ClusterInfo ( ReplicaId . NewId ( ) ) ;
128
127
using var watchdog1 = new ReplicaWatchdog (
129
128
replicaId1 ,
130
- store ,
131
- checkFrequency : TimeSpan . FromMilliseconds ( 100 ) ,
132
- onStrikeOut : _ => anyStrikesOut = true
129
+ functionStore ,
130
+ checkFrequency : TimeSpan . FromMilliseconds ( 100 )
133
131
) ;
134
132
135
133
await watchdog1 . Start ( ) ;
@@ -142,22 +140,21 @@ await BusyWait.Until(async () =>
142
140
single . ReplicaId . ShouldBe ( replicaId1 . ReplicaId ) ;
143
141
return single . Heartbeat > 0 ;
144
142
} ) ;
145
-
146
- anyStrikesOut . ShouldBe ( false ) ;
147
143
}
148
144
149
145
public abstract Task ReplicaIdOffsetIfCalculatedCorrectly ( ) ;
150
146
public async Task ReplicaIdOffsetIfCalculatedCorrectly ( Task < IFunctionStore > storeTask )
151
147
{
152
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
148
+ var store = await storeTask ;
149
+ var replicaStore = store . ReplicaStore ;
153
150
154
151
var replicaId1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
155
152
var replicaId2 = new ClusterInfo ( Guid . Parse ( "20000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
156
153
var replicaId3 = new ClusterInfo ( Guid . Parse ( "30000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
157
154
158
- var watchdog1 = new ReplicaWatchdog ( replicaId1 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
159
- var watchdog2 = new ReplicaWatchdog ( replicaId2 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
160
- var watchdog3 = new ReplicaWatchdog ( replicaId3 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
155
+ var watchdog1 = new ReplicaWatchdog ( replicaId1 , store , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
156
+ var watchdog2 = new ReplicaWatchdog ( replicaId2 , store , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
157
+ var watchdog3 = new ReplicaWatchdog ( replicaId3 , store , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
161
158
162
159
await watchdog1 . Initialize ( ) ;
163
160
await watchdog2 . Initialize ( ) ;
@@ -174,15 +171,16 @@ public async Task ReplicaIdOffsetIfCalculatedCorrectly(Task<IFunctionStore> stor
174
171
public abstract Task ReplicaIdOffsetIsUpdatedWhenNodeIsAddedAndDeleted ( ) ;
175
172
public async Task ReplicaIdOffsetIsUpdatedWhenNodeIsAddedAndDeleted ( Task < IFunctionStore > storeTask )
176
173
{
177
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
174
+ var functionStore = await storeTask ;
175
+ var store = functionStore . ReplicaStore ;
178
176
179
177
var cluster1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
180
178
var cluster2 = new ClusterInfo ( Guid . Parse ( "20000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
181
179
var cluster3 = new ClusterInfo ( Guid . Parse ( "30000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
182
180
183
- var watchdog1 = new ReplicaWatchdog ( cluster1 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
184
- var watchdog2 = new ReplicaWatchdog ( cluster2 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
185
- var watchdog3 = new ReplicaWatchdog ( cluster3 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
181
+ var watchdog1 = new ReplicaWatchdog ( cluster1 , functionStore , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
182
+ var watchdog2 = new ReplicaWatchdog ( cluster2 , functionStore , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
183
+ var watchdog3 = new ReplicaWatchdog ( cluster3 , functionStore , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
186
184
187
185
await watchdog3 . Initialize ( ) ;
188
186
cluster3 . Offset . ShouldBe ( 0 ) ;
@@ -222,15 +220,16 @@ public async Task ReplicaIdOffsetIsUpdatedWhenNodeIsAddedAndDeleted(Task<IFuncti
222
220
public abstract Task ActiveReplicasDoNotDeleteEachOther ( ) ;
223
221
public async Task ActiveReplicasDoNotDeleteEachOther ( Task < IFunctionStore > storeTask )
224
222
{
225
- var store = await storeTask . SelectAsync ( s => s . ReplicaStore ) ;
223
+ var store = await storeTask ;
224
+ var replicaStore = store . ReplicaStore ;
226
225
227
226
var cluster1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
228
227
var cluster2 = new ClusterInfo ( Guid . Parse ( "20000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
229
228
var cluster3 = new ClusterInfo ( Guid . Parse ( "30000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
230
229
231
- var watchdog1 = new ReplicaWatchdog ( cluster1 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
232
- var watchdog2 = new ReplicaWatchdog ( cluster2 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
233
- var watchdog3 = new ReplicaWatchdog ( cluster3 , store , checkFrequency : TimeSpan . FromHours ( 1 ) , onStrikeOut : _ => { } ) ;
230
+ var watchdog1 = new ReplicaWatchdog ( cluster1 , store , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
231
+ var watchdog2 = new ReplicaWatchdog ( cluster2 , store , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
232
+ var watchdog3 = new ReplicaWatchdog ( cluster3 , store , checkFrequency : TimeSpan . FromHours ( 1 ) ) ;
234
233
235
234
await watchdog1 . Initialize ( ) ;
236
235
await watchdog2 . Initialize ( ) ;
@@ -248,7 +247,7 @@ public async Task ActiveReplicasDoNotDeleteEachOther(Task<IFunctionStore> storeT
248
247
await watchdog1 . PerformIteration ( ) ;
249
248
await watchdog2 . PerformIteration ( ) ;
250
249
251
- var storedReplicas = await store . GetAll ( ) ;
250
+ var storedReplicas = await replicaStore . GetAll ( ) ;
252
251
storedReplicas . Count . ShouldBe ( 2 ) ;
253
252
storedReplicas . Any ( sr => sr . ReplicaId == cluster1 . ReplicaId ) . ShouldBeTrue ( ) ;
254
253
storedReplicas . Any ( sr => sr . ReplicaId == cluster2 . ReplicaId ) . ShouldBeTrue ( ) ;
@@ -262,4 +261,43 @@ public Task NonExistingReplicaIdOffsetIsNull(Task<IFunctionStore> storeTask)
262
261
263
262
return Task . CompletedTask ;
264
263
}
264
+
265
+ public abstract Task StrikedOutReplicasFunctionIsPostponedAfterCrash ( ) ;
266
+ public async Task StrikedOutReplicasFunctionIsPostponedAfterCrash ( Task < IFunctionStore > storeTask )
267
+ {
268
+ var functionStore = await storeTask ;
269
+
270
+ var toBeStrikedOut = ReplicaId . NewId ( ) ;
271
+ var storedId = TestStoredId . Create ( ) ;
272
+ await functionStore . CreateFunction (
273
+ storedId ,
274
+ humanInstanceId : "SomeInstanceId" ,
275
+ param : null ,
276
+ leaseExpiration : DateTime . UtcNow . Ticks ,
277
+ postponeUntil : null ,
278
+ timestamp : DateTime . UtcNow . Ticks ,
279
+ parent : null ,
280
+ owner : toBeStrikedOut
281
+ ) . ShouldBeTrueAsync ( ) ;
282
+
283
+ var store = functionStore . ReplicaStore ;
284
+
285
+ await store . Insert ( toBeStrikedOut ) ;
286
+ var replicaId1 = new ClusterInfo ( Guid . Parse ( "10000000-0000-0000-0000-000000000000" ) . ToReplicaId ( ) ) ;
287
+ using var watchdog1 = new ReplicaWatchdog (
288
+ replicaId1 ,
289
+ functionStore ,
290
+ checkFrequency : TimeSpan . FromHours ( 1 )
291
+ ) ;
292
+ await watchdog1 . Initialize ( ) ;
293
+ await watchdog1 . PerformIteration ( ) ;
294
+ await watchdog1 . PerformIteration ( ) ;
295
+ await watchdog1 . PerformIteration ( ) ;
296
+
297
+ var sf = await functionStore . GetFunction ( storedId ) . ShouldNotBeNullAsync ( ) ;
298
+ sf . Epoch . ShouldBe ( 1 ) ;
299
+ sf . Status . ShouldBe ( Status . Postponed ) ;
300
+ sf . Expires . ShouldBe ( 0 ) ;
301
+ sf . OwnerId . ShouldBeNull ( ) ;
302
+ }
265
303
}
0 commit comments