|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Polly.Contrib.Simmy.Behavior; |
| 4 | +using Polly.Contrib.Simmy.Specs.Helpers; |
| 5 | +using Polly.Contrib.Simmy.Utilities; |
| 6 | +using Xunit; |
| 7 | +using FluentAssertions; |
| 8 | + |
| 9 | +namespace Polly.Contrib.Simmy.Specs.Behavior |
| 10 | +{ |
| 11 | + [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] |
| 12 | + public class InjectBehaviourTResultAsyncWithOptionsSpecs : IDisposable |
| 13 | + { |
| 14 | + public InjectBehaviourTResultAsyncWithOptionsSpecs() |
| 15 | + { |
| 16 | + ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; |
| 17 | + } |
| 18 | + |
| 19 | + public void Dispose() |
| 20 | + { |
| 21 | + ThreadSafeRandom_LockOncePerThread.Reset(); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void Given_not_enabled_should_not_inject_behaviour() |
| 26 | + { |
| 27 | + Boolean userDelegateExecuted = false; |
| 28 | + Boolean injectedBehaviourExecuted = false; |
| 29 | + |
| 30 | + var policy = MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 31 | + with.Behaviour(() => |
| 32 | + { |
| 33 | + injectedBehaviourExecuted = true; |
| 34 | + return Task.CompletedTask; |
| 35 | + }) |
| 36 | + .InjectionRate(0.6) |
| 37 | + .Enabled(false) |
| 38 | + ); |
| 39 | + |
| 40 | + policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); |
| 41 | + |
| 42 | + userDelegateExecuted.Should().BeTrue(); |
| 43 | + injectedBehaviourExecuted.Should().BeFalse(); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void Given_enabled_and_randomly_within_threshold_should_inject_behaviour() |
| 48 | + { |
| 49 | + Boolean userDelegateExecuted = false; |
| 50 | + Boolean injectedBehaviourExecuted = false; |
| 51 | + |
| 52 | + var policy = MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 53 | + with.Behaviour(() => |
| 54 | + { |
| 55 | + injectedBehaviourExecuted = true; |
| 56 | + return Task.CompletedTask; |
| 57 | + }) |
| 58 | + .InjectionRate(0.6) |
| 59 | + .Enabled() |
| 60 | + ); |
| 61 | + |
| 62 | + policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); |
| 63 | + |
| 64 | + userDelegateExecuted.Should().BeTrue(); |
| 65 | + injectedBehaviourExecuted.Should().BeTrue(); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void Given_enabled_and_randomly_not_within_threshold_should_not_inject_behaviour() |
| 70 | + { |
| 71 | + Boolean userDelegateExecuted = false; |
| 72 | + Boolean injectedBehaviourExecuted = false; |
| 73 | + |
| 74 | + var policy = MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 75 | + with.Behaviour(() => |
| 76 | + { |
| 77 | + injectedBehaviourExecuted = true; |
| 78 | + return Task.CompletedTask; |
| 79 | + }) |
| 80 | + .InjectionRate(0.4) |
| 81 | + .Enabled(false) |
| 82 | + ); |
| 83 | + |
| 84 | + policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); |
| 85 | + |
| 86 | + userDelegateExecuted.Should().BeTrue(); |
| 87 | + injectedBehaviourExecuted.Should().BeFalse(); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void Should_inject_behaviour_before_executing_user_delegate() |
| 92 | + { |
| 93 | + Boolean userDelegateExecuted = false; |
| 94 | + Boolean injectedBehaviourExecuted = false; |
| 95 | + |
| 96 | + var policy = MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 97 | + with.Behaviour(() => |
| 98 | + { |
| 99 | + userDelegateExecuted.Should().BeFalse(); // Not yet executed at the time the injected behaviour runs. |
| 100 | + injectedBehaviourExecuted = true; |
| 101 | + return Task.CompletedTask; |
| 102 | + }) |
| 103 | + .InjectionRate(0.6) |
| 104 | + .Enabled() |
| 105 | + ); |
| 106 | + |
| 107 | + policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); |
| 108 | + |
| 109 | + userDelegateExecuted.Should().BeTrue(); |
| 110 | + injectedBehaviourExecuted.Should().BeTrue(); |
| 111 | + } |
| 112 | + |
| 113 | + #region invalid threshold on configuration and execution time |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void Should_throw_error_on_configuration_time_when_threshold_is_negative() |
| 117 | + { |
| 118 | + Action act = () => MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 119 | + with.Behaviour(() => Task.CompletedTask) |
| 120 | + .Enabled() |
| 121 | + .InjectionRate(-1) |
| 122 | + ); |
| 123 | + |
| 124 | + act.ShouldThrow<ArgumentOutOfRangeException>() |
| 125 | + .WithMessage("Injection rate/threshold in Monkey policies should always be a double between [0, 1]; never a negative number.\r\nParameter name: injectionThreshold"); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void Should_throw_error_on_configuration_time_when_threshold_is_greater_than_one() |
| 130 | + { |
| 131 | + Action act = () => MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 132 | + with.Behaviour(() => Task.CompletedTask) |
| 133 | + .Enabled() |
| 134 | + .InjectionRate(1.1) |
| 135 | + ); |
| 136 | + |
| 137 | + act.ShouldThrow<ArgumentOutOfRangeException>() |
| 138 | + .WithMessage("Injection rate/threshold in Monkey policies should always be a double between [0, 1]; never a number greater than 1.\r\nParameter name: injectionThreshold"); |
| 139 | + } |
| 140 | + |
| 141 | + [Fact] |
| 142 | + public void Should_throw_error_on_execution_time_when_threshold_is_is_negative() |
| 143 | + { |
| 144 | + var policy = MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 145 | + with.Behaviour(() => Task.CompletedTask) |
| 146 | + .Enabled() |
| 147 | + .InjectionRate((_, __) => Task.FromResult(-1d)) |
| 148 | + ); |
| 149 | + |
| 150 | + policy.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) |
| 151 | + .ShouldThrow<ArgumentOutOfRangeException>() |
| 152 | + .WithMessage("Injection rate/threshold in Monkey policies should always be a double between [0, 1]; never a negative number.\r\nParameter name: injectionThreshold"); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void Should_throw_error_on_execution_time_when_threshold_is_greater_than_one() |
| 157 | + { |
| 158 | + var policy = MonkeyPolicy.InjectBehaviourAsync<ResultPrimitive>(with => |
| 159 | + with.Behaviour(() => Task.CompletedTask) |
| 160 | + .Enabled() |
| 161 | + .InjectionRate((_, __) => Task.FromResult(1.1)) |
| 162 | + ); |
| 163 | + |
| 164 | + policy.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) |
| 165 | + .ShouldThrow<ArgumentOutOfRangeException>() |
| 166 | + .WithMessage("Injection rate/threshold in Monkey policies should always be a double between [0, 1]; never a number greater than 1.\r\nParameter name: injectionThreshold"); |
| 167 | + } |
| 168 | + |
| 169 | + #endregion |
| 170 | + } |
| 171 | +} |
0 commit comments