-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTransientOperationTest.cs
More file actions
385 lines (309 loc) · 19.6 KB
/
Copy pathTransientOperationTest.cs
File metadata and controls
385 lines (309 loc) · 19.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
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Cuemon.Diagnostics;
using Codebelt.Extensions.Xunit;
using Cuemon.Resilience.Assets;
using Xunit;
namespace Cuemon.Resilience
{
public class TransientOperationTest : Test
{
private readonly ConcurrentDictionary<Guid, int> _retryTracker = new ConcurrentDictionary<Guid, int>();
private readonly ConcurrentDictionary<Guid, TransientFaultEvidence> _transientFaultTracker = new ConcurrentDictionary<Guid, TransientFaultEvidence>();
private const string ExpectedResult = "OK";
private const int ExpectedRetryAttempts = 2;
private static readonly TimeSpan Jitter = TimeSpan.FromSeconds(Generate.RandomNumber(7, 15));
private const int NormalRunIncrement = 1;
private const int DescriptiveExceptionCauseIncrement = 1;
private static readonly TimeSpan ExpectedRecoveryWaitTime = TimeSpan.FromSeconds(1);
private static readonly TimeSpan ExpectedMaximumAllowedLatency = TimeSpan.FromMilliseconds(500);
public TransientOperationTest(ITestOutputHelper output) : base(output)
{
TransientOperation.FaultCallback = evidence => RetryTrackerCallback(evidence, _transientFaultTracker);
TransientOperationOptionsCallback = o =>
{
o.DetectionStrategy = DetectionStrategyCallback;
o.RetryAttempts = ExpectedRetryAttempts;
o.RetryStrategy = RetryStrategyCallback;
o.MaximumAllowedLatency = ExpectedMaximumAllowedLatency;
};
}
private static void RetryTrackerCallback(TransientFaultEvidence tfe, ConcurrentDictionary<Guid, TransientFaultEvidence> transientFaultTracker)
{
var indexOfId = Array.FindIndex(tfe.Descriptor.Parameters, name => name.Equals("id", StringComparison.OrdinalIgnoreCase));
if (tfe.Descriptor.Arguments[indexOfId] is Guid oId)
{
transientFaultTracker.TryAdd(oId, tfe);
}
}
private bool DetectionStrategyCallback(Exception ex)
{
return ex is HttpRequestException;
}
private TimeSpan RetryStrategyCallback(int retry)
{
return ExpectedRecoveryWaitTime;
}
private Action<TransientOperationOptions> TransientOperationOptionsCallback { get; }
[Fact]
public void WithFunc_ShouldBypassTransientFaultHandling()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithFunc(() => TransientOperation.WithFunc(FuncTransientOperation.MethodThatReturnsOkString, id, _retryTracker, TransientOperationOptionsCallback));
Assert.Equal(0, (int)profiler.Elapsed.TotalSeconds);
Assert.Equal(ExpectedResult, profiler.Result);
Assert.Equal(0, _retryTracker[id]);
}
[Fact]
public void WithFunc_ShouldTriggerRetryAndSucceed()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithFunc(() => TransientOperation.WithFunc(FuncTransientOperation.FailUntilExpectedRetryAttemptsIsReached, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.Equal(ExpectedResult, profiler.Result);
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public void WithFunc_ShouldTriggerTransientFaultException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() =>
{
var aex = Assert.Throws<AggregateException>(() => TransientOperation.WithFunc(FuncTransientOperation.TriggerTransientFaultException, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<TransientFaultException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + ExpectedRetryAttempts + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
});
var tfe = _transientFaultTracker.Single(pair => pair.Key == id).Value;
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
Assert.Equal((int)TimeSpan.FromSeconds(ExpectedRetryAttempts).TotalSeconds, (int)tfe.TotalRecoveryWaitTime.TotalSeconds);
Assert.Equal((int)ExpectedRecoveryWaitTime.TotalSeconds, (int)tfe.RecoveryWaitTime.TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, tfe.Attempts);
TestOutput.WriteLine(tfe.ToString());
}
[Fact]
public void WithFunc_ShouldTriggerLatencyException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() =>
{
var aex = Assert.Throws<AggregateException>(() => TransientOperation.WithFunc(FuncTransientOperation.TriggerLatencyException, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<LatencyException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
TestOutput.WriteLine(aex.ToString());
});
Assert.True(ExpectedMaximumAllowedLatency < profiler.Elapsed, "ExpectedMaximumAllowedLatency < profiler.Elapsed");
}
[Fact]
public void WithFunc_ShouldTriggerInvalidOperationException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() =>
{
var aex = Assert.Throws<AggregateException>(() => TransientOperation.WithFunc(FuncTransientOperation.FailWithNonTransientFaultException, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<InvalidOperationException>(aex.InnerExceptions.First());
TestOutput.WriteLine(aex.ToString());
});
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public void WithAction_ShouldBypassTransientFaultHandling()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() => TransientOperation.WithAction(ActionTransientOperation.MethodThatReturnsOkString, id, _retryTracker, TransientOperationOptionsCallback));
Assert.Equal(0, (int)profiler.Elapsed.TotalSeconds);
Assert.Equal(0, _retryTracker[id]);
}
[Fact]
public void WithAction_ShouldTriggerRetryAndSucceed()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() => TransientOperation.WithAction(ActionTransientOperation.FailUntilExpectedRetryAttemptsIsReached, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public void WithAction_ShouldTriggerTransientFaultException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() =>
{
var aex = Assert.Throws<AggregateException>(() => TransientOperation.WithAction(ActionTransientOperation.TriggerTransientFaultException, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<TransientFaultException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + ExpectedRetryAttempts + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
});
var tfe = _transientFaultTracker.Single(pair => pair.Key == id).Value;
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
Assert.Equal((int)TimeSpan.FromSeconds(ExpectedRetryAttempts).TotalSeconds, (int)tfe.TotalRecoveryWaitTime.TotalSeconds);
Assert.Equal((int)ExpectedRecoveryWaitTime.TotalSeconds, (int)tfe.RecoveryWaitTime.TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, tfe.Attempts);
TestOutput.WriteLine(tfe.ToString());
}
[Fact]
public void WithAction_ShouldTriggerLatencyException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() =>
{
var aex = Assert.Throws<AggregateException>(() => TransientOperation.WithAction(ActionTransientOperation.TriggerLatencyException, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<LatencyException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
TestOutput.WriteLine(aex.ToString());
});
Assert.True(ExpectedMaximumAllowedLatency < profiler.Elapsed, "ExpectedMaximumAllowedLatency < profiler.Elapsed");
}
[Fact]
public void WithAction_ShouldTriggerInvalidOperationException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = TimeMeasure.WithAction(() =>
{
var aex = Assert.Throws<AggregateException>(() => TransientOperation.WithAction(ActionTransientOperation.FailWithNonTransientFaultException, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<InvalidOperationException>(aex.InnerExceptions.First());
TestOutput.WriteLine(aex.ToString());
});
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public async Task WithActionAsync_ShouldBypassTransientFaultHandling()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(ct => TransientOperation.WithActionAsync(AsyncActionTransientOperation.MethodThatReturnsOkStringAsync, id, _retryTracker, TransientOperationOptionsCallback));
Assert.Equal(0, (int)profiler.Elapsed.TotalSeconds);
Assert.Equal(0, _retryTracker[id]);
}
[Fact]
public async Task WithActionAsync_ShouldTriggerRetryAndSucceed()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(ct => TransientOperation.WithActionAsync(AsyncActionTransientOperation.FailUntilExpectedRetryAttemptsIsReachedAsync, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public async Task WithActionAsync_ShouldTriggerTransientFaultException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(async ct =>
{
var aex = await Assert.ThrowsAsync<AggregateException>(() => TransientOperation.WithActionAsync(AsyncActionTransientOperation.TriggerTransientFaultExceptionAsync, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<TransientFaultException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + ExpectedRetryAttempts + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
});
var tfe = _transientFaultTracker.Single(pair => pair.Key == id).Value;
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
Assert.Equal((int)TimeSpan.FromSeconds(ExpectedRetryAttempts).TotalSeconds, (int)tfe.TotalRecoveryWaitTime.TotalSeconds);
Assert.Equal((int)ExpectedRecoveryWaitTime.TotalSeconds, (int)tfe.RecoveryWaitTime.TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, tfe.Attempts);
TestOutput.WriteLine(tfe.ToString());
}
[Fact]
public async Task WithActionAsync_ShouldTriggerLatencyException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(async ct =>
{
var aex = await Assert.ThrowsAsync<AggregateException>(() => TransientOperation.WithActionAsync(AsyncActionTransientOperation.TriggerLatencyExceptionAsync, id, _retryTracker, TransientOperationOptionsCallback));
TestOutput.WriteLine(aex.ToString());
Assert.IsType<LatencyException>(aex.InnerExceptions.First());
var low = NormalRunIncrement + DescriptiveExceptionCauseIncrement;
Assert.InRange(aex.InnerExceptions.Count, low, low + 1); // expect 2 - allow 3 in rare cases
TestOutput.WriteLine(aex.ToString());
});
Assert.True(ExpectedMaximumAllowedLatency < profiler.Elapsed, "ExpectedMaximumAllowedLatency < profiler.Elapsed");
}
[Fact]
public async Task WithActionAsync_ShouldTriggerInvalidOperationException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(async ct =>
{
var aex = await Assert.ThrowsAsync<AggregateException>(() => TransientOperation.WithActionAsync(AsyncActionTransientOperation.FailWithNonTransientFaultExceptionAsync, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<InvalidOperationException>(aex.InnerExceptions.First());
TestOutput.WriteLine(aex.ToString());
});
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public async Task WithFuncAsync_ShouldTriggerRetryAndSucceedAsync()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithFuncAsync(ct => TransientOperation.WithFuncAsync(AsyncFuncTransientOperation.FailUntilExpectedRetryAttemptsIsReachedAsync, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.Equal(ExpectedResult, profiler.Result);
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
[Fact]
public async Task WithFuncAsync_ShouldTriggerTransientFaultException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(async ct =>
{
var aex = await Assert.ThrowsAsync<AggregateException>(() => TransientOperation.WithFuncAsync(AsyncFuncTransientOperation.TriggerTransientFaultExceptionAsync, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<TransientFaultException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + ExpectedRetryAttempts + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
});
var tfe = _transientFaultTracker.Single(pair => pair.Key == id).Value;
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
Assert.Equal((int)TimeSpan.FromSeconds(ExpectedRetryAttempts).TotalSeconds, (int)tfe.TotalRecoveryWaitTime.TotalSeconds);
Assert.Equal((int)ExpectedRecoveryWaitTime.TotalSeconds, (int)tfe.RecoveryWaitTime.TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, tfe.Attempts);
TestOutput.WriteLine(tfe.ToString());
}
[Fact]
public async Task WithFuncAsync_ShouldTriggerLatencyException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(async ct =>
{
var aex = await Assert.ThrowsAsync<AggregateException>(() => TransientOperation.WithFuncAsync(AsyncFuncTransientOperation.TriggerLatencyExceptionAsync, id, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<LatencyException>(aex.InnerExceptions.First());
Assert.Equal(NormalRunIncrement + DescriptiveExceptionCauseIncrement, aex.InnerExceptions.Count);
TestOutput.WriteLine(aex.ToString());
});
Assert.True(ExpectedMaximumAllowedLatency < profiler.Elapsed, "ExpectedMaximumAllowedLatency < profiler.Elapsed");
}
[Fact]
public async Task WithFuncAsync_ShouldTriggerInvalidOperationException()
{
var id = Guid.NewGuid();
_retryTracker.TryAdd(id, -1);
var profiler = await TimeMeasure.WithActionAsync(async ct =>
{
var aex = await Assert.ThrowsAsync<AggregateException>(() => TransientOperation.WithFuncAsync(AsyncFuncTransientOperation.FailWithNonTransientFaultExceptionAsync, id, ExpectedRetryAttempts, _retryTracker, TransientOperationOptionsCallback));
Assert.IsType<InvalidOperationException>(aex.InnerExceptions.First());
TestOutput.WriteLine(aex.ToString());
});
TestOutput.WriteLine($"Profiler: {profiler.Elapsed.TotalSeconds} seconds.");
Assert.InRange(profiler.Elapsed.TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) - Jitter).TotalSeconds, (TimeSpan.FromSeconds(ExpectedRetryAttempts) + Jitter).TotalSeconds);
Assert.Equal(ExpectedRetryAttempts, _retryTracker[id]);
}
}
}