-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCircuitBreakerTestHelper.cs
61 lines (57 loc) · 1.99 KB
/
CircuitBreakerTestHelper.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
using System;
using ReliabilityPatterns;
namespace Tests
{
public class CircuitBreakerTestHelper
{
readonly ushort _allowedRetries;
public CircuitBreaker Breaker { get; private set; }
public int CallCount { get; private set; }
public CircuitBreakerTestHelper(uint threshold = 4, ushort allowedRetries = 0)
{
_allowedRetries = allowedRetries;
Breaker = new CircuitBreaker(threshold, TimeSpan.FromSeconds(120));
}
public void ExecuteFailingAction()
{
Console.WriteLine("Before: " + Breaker.State);
try
{
CallCount = 0;
if (_allowedRetries > 0)
{
Breaker.ExecuteWithRetries(() =>
{
CallCount++;
throw new Exception("foo");
}, new RetryOptions
{
AllowedRetries = _allowedRetries,
RetryInterval = TimeSpan.FromMilliseconds(10)
});
}
else
{
Breaker.Execute(() =>
{
CallCount++;
throw new Exception("foo");
});
}
}
catch (OpenCircuitException)
{
Console.WriteLine("Tripped due to open circuit");
}
catch (OperationFailedException)
{
Console.WriteLine("Tripped due to unhandled exception");
}
catch (AggregateException ex)
{
Console.WriteLine("Tripped due to unhandled exception. InnerExceptionCount: " + ex.InnerExceptions.Count);
}
Console.WriteLine("After {0} attempt/s: {1}", CallCount, Breaker.State + Environment.NewLine);
}
}
}