-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMonkeyEngine.cs
91 lines (77 loc) · 3.78 KB
/
MonkeyEngine.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
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
using System;
using System.Threading;
using Polly.Contrib.Simmy.Utilities;
namespace Polly.Contrib.Simmy
{
internal static class MonkeyEngine
{
private static bool ShouldInject(Context context, CancellationToken cancellationToken, Func<Context, CancellationToken, double> injectionRate, Func<Context, CancellationToken, bool> enabled)
{
// to prevent execute config delegates if token is signaled before to start.
cancellationToken.ThrowIfCancellationRequested();
if (!enabled(context, cancellationToken))
{
return false;
}
// to prevent execute injectionRate config delegate if token is signaled on enable configuration delegate.
cancellationToken.ThrowIfCancellationRequested();
double injectionThreshold = injectionRate(context, cancellationToken);
// to prevent execute further config delegates if token is signaled on injectionRate configuration delegate.
cancellationToken.ThrowIfCancellationRequested();
injectionThreshold.EnsureInjectionThreshold();
return ThreadSafeRandom_LockOncePerThread.NextDouble() < injectionThreshold;
}
internal static TResult InjectBehaviourImplementation<TResult>(
Func<Context, CancellationToken, TResult> action,
Context context,
CancellationToken cancellationToken,
Action<Context, CancellationToken> injectedBehaviour,
Func<Context, CancellationToken, Double> injectionRate,
Func<Context, CancellationToken, bool> enabled)
{
if (ShouldInject(context, cancellationToken, injectionRate, enabled))
{
injectedBehaviour(context, cancellationToken);
}
// to prevent execute the user's action if token is signaled on injectedBehaviour delegate.
cancellationToken.ThrowIfCancellationRequested();
return action(context, cancellationToken);
}
internal static TResult InjectExceptionImplementation<TResult>(
Func<Context, CancellationToken, TResult> action,
Context context,
CancellationToken cancellationToken,
Func<Context, CancellationToken, Exception> injectedException,
Func<Context, CancellationToken, Double> injectionRate,
Func<Context, CancellationToken, bool> enabled)
{
if (ShouldInject(context, cancellationToken, injectionRate, enabled))
{
Exception exception = injectedException(context, cancellationToken);
// to prevent throws the exception if token is signaled on injectedException configuration delegate.
cancellationToken.ThrowIfCancellationRequested();
if (exception != null)
{
throw exception;
}
}
return action(context, cancellationToken);
}
internal static TResult InjectResultImplementation<TResult>(
Func<Context, CancellationToken, TResult> action,
Context context,
CancellationToken cancellationToken,
Func<Context, CancellationToken, TResult> injectedResult,
Func<Context, CancellationToken, Double> injectionRate,
Func<Context, CancellationToken, bool> enabled)
{
if (ShouldInject(context, cancellationToken, injectionRate, enabled))
{
return injectedResult(context, cancellationToken);
}
// to prevent inject the result if token is signaled on injectedResult delegate.
cancellationToken.ThrowIfCancellationRequested();
return action(context, cancellationToken);
}
}
}