diff --git a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourAsyncSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourAsyncSpecs.cs deleted file mode 100644 index 5c9d5fa..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourAsyncSpecs.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.Threading.Tasks; -using FluentAssertions; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Behavior -{ - [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectBehaviourAsyncSpecs : IDisposable - { - public InjectBehaviourAsyncSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - [Fact] - public void Given_not_enabled_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => { injectedBehaviourExecuted = true; return Task.CompletedTask; }, 0.6, () => Task.FromResult(false)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.CompletedTask; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Given_enabled_and_randomly_within_threshold_should_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => { injectedBehaviourExecuted = true; return Task.CompletedTask; }, 0.6, () => Task.FromResult(true)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.CompletedTask; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - - [Fact] - public void Given_enabled_and_randomly_not_within_threshold_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => { injectedBehaviourExecuted = true; return Task.CompletedTask; }, 0.4, () => Task.FromResult(true)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.CompletedTask; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Should_inject_behaviour_before_executing_user_delegate() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => - { - userDelegateExecuted.Should().BeFalse(); // Not yet executed at the time the injected behaviour runs. - injectedBehaviourExecuted = true; - return Task.CompletedTask; - }, 0.6, () => Task.FromResult(true)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.CompletedTask; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourSpecs.cs deleted file mode 100644 index 2ace1c0..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourSpecs.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using FluentAssertions; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Behavior -{ - [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectBehaviourSpecs : IDisposable - { - public InjectBehaviourSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - [Fact] - public void Given_not_enabled_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => { injectedBehaviourExecuted = true; }, 0.6, () => false); - - policy.Execute(() => { userDelegateExecuted = true; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Given_enabled_and_randomly_within_threshold_should_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => { injectedBehaviourExecuted = true; }, 0.6, () => true); - - policy.Execute(() => { userDelegateExecuted = true; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - - [Fact] - public void Given_enabled_and_randomly_not_within_threshold_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => { injectedBehaviourExecuted = true; }, 0.4, () => true); - - policy.Execute(() => { userDelegateExecuted = true; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Should_inject_behaviour_before_executing_user_delegate() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => - { - userDelegateExecuted.Should().BeFalse(); // Not yet executed at the time the injected behaviour runs. - injectedBehaviourExecuted = true; - }, 0.6, () => true); - - policy.Execute(() => { userDelegateExecuted = true; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - } -} diff --git a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourTResultAsyncSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourTResultAsyncSpecs.cs deleted file mode 100644 index e4a74e4..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourTResultAsyncSpecs.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Threading.Tasks; -using FluentAssertions; -using Polly.Contrib.Simmy.Specs.Helpers; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Behavior -{ - [Collection(Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectBehaviourTResultAsyncSpecs : IDisposable - { - public InjectBehaviourTResultAsyncSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - [Fact] - public void Given_not_enabled_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => { injectedBehaviourExecuted = true; return Task.CompletedTask; }, 0.6, () => Task.FromResult(false)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Given_enabled_and_randomly_within_threshold_should_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => { injectedBehaviourExecuted = true; return Task.CompletedTask; }, 0.6, () => Task.FromResult(true)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - - [Fact] - public void Given_enabled_and_randomly_not_within_threshold_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => { injectedBehaviourExecuted = true; return Task.CompletedTask; }, 0.4, () => Task.FromResult(true)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Should_inject_behaviour_before_executing_user_delegate() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviourAsync(() => - { - userDelegateExecuted.Should().BeFalse(); // Not yet executed at the time the injected behaviour runs. - injectedBehaviourExecuted = true; - return Task.CompletedTask; - }, 0.6, () => Task.FromResult(true)); - - policy.ExecuteAsync(() => { userDelegateExecuted = true; return Task.FromResult(ResultPrimitive.Good); }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourTResultSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourTResultSpecs.cs deleted file mode 100644 index faf3a37..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourTResultSpecs.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using FluentAssertions; -using Polly.Contrib.Simmy.Specs.Helpers; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Behavior -{ - [Collection(Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectBehaviourTResultSpecs : IDisposable - { - public InjectBehaviourTResultSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - [Fact] - public void Given_not_enabled_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => { injectedBehaviourExecuted = true; }, 0.6, () => false); - - policy.Execute(() => { userDelegateExecuted = true; return ResultPrimitive.Good; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Given_enabled_and_randomly_within_threshold_should_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => { injectedBehaviourExecuted = true; }, 0.6, () => true); - - policy.Execute(() => { userDelegateExecuted = true; return ResultPrimitive.Good; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - - [Fact] - public void Given_enabled_and_randomly_not_within_threshold_should_not_inject_behaviour() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => { injectedBehaviourExecuted = true; }, 0.4, () => true); - - policy.Execute(() => { userDelegateExecuted = true; return ResultPrimitive.Good; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeFalse(); - } - - [Fact] - public void Should_inject_behaviour_before_executing_user_delegate() - { - Boolean userDelegateExecuted = false; - Boolean injectedBehaviourExecuted = false; - - var policy = MonkeyPolicy.InjectBehaviour(() => - { - userDelegateExecuted.Should().BeFalse(); // Not yet executed at the time the injected behaviour runs. - injectedBehaviourExecuted = true; - }, 0.6, () => true); - - policy.Execute(() => { userDelegateExecuted = true; return ResultPrimitive.Good; }); - - userDelegateExecuted.Should().BeTrue(); - injectedBehaviourExecuted.Should().BeTrue(); - } - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyAsyncSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyAsyncSpecs.cs deleted file mode 100644 index 4513d11..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyAsyncSpecs.cs +++ /dev/null @@ -1,627 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using Polly.Contrib.Simmy.Utilities; -using Polly.Timeout; -using Polly.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Latency -{ - [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectLatencyAsyncSpecs : IDisposable - { - private int _totalTimeSlept = 0; - - public InjectLatencyAsyncSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - SystemClock.SleepAsync = async (span, ct) => _totalTimeSlept += await Task.FromResult(span.Milliseconds); - } - - public void Dispose() - { - _totalTimeSlept = 0; - SystemClock.Reset(); - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Context Free - - [Fact] - public async Task InjectLatency_Context_Free_Should_Introduce_Delay_If_Enabled() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, () => true); - var executed = false; - - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() - { - var policy = MonkeyPolicy.InjectLatencyAsync(TimeSpan.FromMilliseconds(500), 0.6, () => false); - var executed = false; - - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_Context_Free_Should_Introduce_Delay_If_InjectionRate_Is_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, () => true); - var executed = false; - - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.3, () => true); - var executed = false; - - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region With Context - - [Fact] - public async Task InjectLatency_With_Context_With_Enabled_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = false; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.3, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, injectionRate, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, injectionRate, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Latency_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = false; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Boolean executed = false; - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Func actionAsync = (_, ct) => { executed = true; return TaskHelper.EmptyTask; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Boolean executed = false; - Func actionAsync = (_, ct) => { executed = true; return TaskHelper.EmptyTask; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - cts.Cancel(); - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Boolean executed = false; - Func actionAsync = (_, ct) => { executed = true; return TaskHelper.EmptyTask; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - cts.Cancel(); - - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_latency_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - Func actionAsync = (_, ct) => { executed = true; return TaskHelper.EmptyTask; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Func> latencyProvider = async (ctx, ct) => - { - cts.Cancel(); - - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Theory] - [InlineData(TimeoutStrategy.Optimistic)] - [InlineData(TimeoutStrategy.Pessimistic)] - public void InjectLatency_With_Context_Should_not_inject_the_whole_latency_if_user_cancelationtoken_is_signaled_from_timeout(TimeoutStrategy timeoutStrategy) - { - SystemClock.Reset(); - var timeout = TimeSpan.FromSeconds(5); - var delay = TimeSpan.FromSeconds(10); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - Stopwatch watch = new Stopwatch(); - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func actionAsync = (_, ct) => - { - executed = true; - return TaskHelper.EmptyTask; - }; - - var policy = Policy.TimeoutAsync(timeout, timeoutStrategy) - .WrapAsync(MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled)); - - watch.Start(); - policy.Awaiting(async x => { await x.ExecuteAsync(actionAsync, context, cts.Token); }) - .ShouldThrow(); - watch.Stop(); - } - - executed.Should().BeFalse(); - watch.Elapsed.Should().BeCloseTo(timeout, ((int)TimeSpan.FromSeconds(3).TotalMilliseconds)); - } - - #endregion - } -} diff --git a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencySpecs.cs b/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencySpecs.cs deleted file mode 100644 index ce971db..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencySpecs.cs +++ /dev/null @@ -1,600 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; -using FluentAssertions; -using Polly.Contrib.Simmy.Utilities; -using Polly.Timeout; -using Polly.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Latency -{ - [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectLatencySpecs : IDisposable - { - private int _totalTimeSlept = 0; - - public InjectLatencySpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - SystemClock.Sleep = (span, ct) => _totalTimeSlept += span.Milliseconds; - } - - public void Dispose() - { - _totalTimeSlept = 0; - SystemClock.Reset(); - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Context Free - - [Fact] - public void InjectLatency_Context_Free_Should_Introduce_Delay_If_Enabled() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, () => true); - var executed = false; - - policy.Execute(() => { executed = true; }); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() - { - var policy = MonkeyPolicy.InjectLatency(TimeSpan.FromMilliseconds(500), 0.6, () => false); - var executed = false; - - policy.Execute(() => { executed = true; }); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_Context_Free_Should_Introduce_Delay_If_InjectionRate_Is_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, () => true); - var executed = false; - - policy.Execute(() => { executed = true; }); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatency(delay, 0.3, () => true); - var executed = false; - - policy.Execute(() => { executed = true; }); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region With Context - - [Fact] - public void InjectLatency_With_Context_With_Enabled_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = false; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatency(delay, 0.3, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(delay, injectionRate, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(delay, injectionRate, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_Latency_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = false; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - policy.Execute((ctx) => { executed = true; }, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Boolean executed = false; - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Boolean executed = false; - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - cts.Cancel(); - return (bool)ctx["Enabled"]; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Boolean executed = false; - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - cts.Cancel(); - - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_latency_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func latencyProvider = (ctx, ct) => - { - cts.Cancel(); - - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Theory] - [InlineData(TimeoutStrategy.Optimistic)] - [InlineData(TimeoutStrategy.Pessimistic)] - public void InjectLatency_With_Context_Should_not_inject_the_whole_latency_if_user_cancelationtoken_is_signaled_from_timeout(TimeoutStrategy timeoutStrategy) - { - SystemClock.Reset(); - var timeout = TimeSpan.FromSeconds(5); - var delay = TimeSpan.FromSeconds(10); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - Stopwatch watch = new Stopwatch(); - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - var policy = Policy.Timeout(timeout, timeoutStrategy) - .Wrap(MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled)); - - watch.Start(); - policy.Invoking(x => { x.Execute((ctx, ct) => { executed = true; }, context, cts.Token); }) - .ShouldThrow(); - watch.Stop(); - } - - executed.Should().BeFalse(); - watch.Elapsed.Should().BeCloseTo(timeout, ((int)TimeSpan.FromSeconds(3).TotalMilliseconds)); - } - - #endregion - } -} diff --git a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyTResultAsyncSpecs .cs b/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyTResultAsyncSpecs .cs deleted file mode 100644 index c5c27a1..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyTResultAsyncSpecs .cs +++ /dev/null @@ -1,641 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using Polly.Utilities; -using Polly.Contrib.Simmy.Specs.Helpers; -using Polly.Contrib.Simmy.Utilities; -using Polly.Timeout; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Latency -{ - [Collection(Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectLatencyTResultAsyncSpecs : IDisposable - { - private int _totalTimeSlept = 0; - - public InjectLatencyTResultAsyncSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - SystemClock.SleepAsync = async (span, ct) => _totalTimeSlept += await Task.FromResult(span.Milliseconds); - } - - public void Dispose() - { - _totalTimeSlept = 0; - SystemClock.Reset(); - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Context Free - - [Fact] - public async Task InjectLatency_Context_Free_Should_Introduce_Delay_If_Enabled() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, () => true); - var executed = false; - - Func> actionAsync = () => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() - { - var policy = MonkeyPolicy.InjectLatencyAsync(TimeSpan.FromMilliseconds(500), 0.6, () => false); - var executed = false; - - Func> actionAsync = () => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_Context_Free_Should_Introduce_Delay_If_InjectionRate_Is_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, () => true); - var executed = false; - - Func> actionAsync = () => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.3, () => true); - var executed = false; - - Func> actionAsync = () => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region With Context - - [Fact] - public async Task InjectLatency_With_Context_With_Enabled_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = false; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.6, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, 0.3, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, injectionRate, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(delay, injectionRate, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Latency_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = false; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public async Task InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func> actionAsync = _ => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - var result = await policy.ExecuteAsync(actionAsync, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Boolean executed = false; - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - Func> actionAsync = (_, ct) => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Boolean executed = false; - Func> actionAsync = (_, ct) => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - cts.Cancel(); - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Boolean executed = false; - Func> actionAsync = (_, ct) => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - cts.Cancel(); - - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_latency_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - Func> actionAsync = (_, ct) => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Func> latencyProvider = async (ctx, ct) => - { - cts.Cancel(); - - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - var policy = MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Theory] - [InlineData(TimeoutStrategy.Optimistic)] - [InlineData(TimeoutStrategy.Pessimistic)] - public void InjectLatency_With_Context_Should_not_inject_the_whole_latency_if_user_cancelationtoken_is_signaled_from_timeout(TimeoutStrategy timeoutStrategy) - { - SystemClock.Reset(); - var timeout = TimeSpan.FromSeconds(5); - var delay = TimeSpan.FromSeconds(10); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - Stopwatch watch = new Stopwatch(); - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["Enabled"]); - }; - - Func> injectionRate = async (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return await Task.FromResult((double)ctx["InjectionRate"]); - } - - return await Task.FromResult(0); - }; - - Func> latencyProvider = async (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return await Task.FromResult(delay); - } - - return await Task.FromResult(TimeSpan.FromMilliseconds(0)); - }; - - Func> actionAsync = (_, ct) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - var policy = Policy.TimeoutAsync(timeout, timeoutStrategy) - .WrapAsync(MonkeyPolicy.InjectLatencyAsync(latencyProvider, injectionRate, enabled)); - - watch.Start(); - policy.Awaiting(async x => { await x.ExecuteAsync(actionAsync, context, cts.Token); }) - .ShouldThrow(); - - watch.Stop(); - } - - executed.Should().BeFalse(); - watch.Elapsed.Should().BeCloseTo(timeout, ((int)TimeSpan.FromSeconds(3).TotalMilliseconds)); - } - - #endregion - } -} diff --git a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyTResultSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyTResultSpecs.cs deleted file mode 100644 index a24d2f4..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyTResultSpecs.cs +++ /dev/null @@ -1,632 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; -using FluentAssertions; -using Polly.Utilities; -using Polly.Contrib.Simmy.Specs.Helpers; -using Polly.Contrib.Simmy.Utilities; -using Xunit; -using Polly.Timeout; - -namespace Polly.Contrib.Simmy.Specs.Latency -{ - [Collection(Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectLatencyTResultSpecs : IDisposable - { - private int _totalTimeSlept = 0; - - public InjectLatencyTResultSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - SystemClock.Sleep = (span, ct) => _totalTimeSlept += span.Milliseconds; - } - - public void Dispose() - { - _totalTimeSlept = 0; - SystemClock.Reset(); - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Context Free - - [Fact] - public void InjectLatency_Context_Free_Should_Introduce_Delay_If_Enabled() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, () => true); - var executed = false; - - Func action = () => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() - { - var policy = MonkeyPolicy.InjectLatency(TimeSpan.FromMilliseconds(500), 0.6, () => false); - var executed = false; - - Func action = () => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_Context_Free_Should_Introduce_Delay_If_InjectionRate_Is_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, () => true); - var executed = false; - - Func action = () => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var policy = MonkeyPolicy.InjectLatency(delay, 0.3, () => true); - var executed = false; - - Func action = () => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region With Context - - [Fact] - public void InjectLatency_With_Context_With_Enabled_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = false; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatency(delay, 0.6, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_Enabled_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - var policy = MonkeyPolicy.InjectLatency(delay, 0.3, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(delay, injectionRate, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_With_Context_With_InjectionRate_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(delay, injectionRate, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_Latency_Lambda_Should_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(delay.Milliseconds); - } - - [Fact] - public void InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = false; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - result.Should().Be(ResultPrimitive.Good); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay_If_InjectionRate_Is_Not_Covered() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.3; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["Enabled"]); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Boolean executed = false; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - var result = policy.Execute(action, context); - - executed.Should().BeTrue(); - _totalTimeSlept.Should().Be(0); - } - - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Boolean executed = false; - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Boolean executed = false; - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - cts.Cancel(); - return (bool)ctx["Enabled"]; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Boolean executed = false; - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - cts.Cancel(); - - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Fact] - public void InjectLatency_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_latency_config_delegate() - { - var delay = TimeSpan.FromMilliseconds(500); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func latencyProvider = (ctx, ct) => - { - cts.Cancel(); - - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - var policy = MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled); - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - _totalTimeSlept.Should().Be(0); - } - - [Theory] - [InlineData(TimeoutStrategy.Optimistic)] - [InlineData(TimeoutStrategy.Pessimistic)] - public void InjectLatency_With_Context_Should_not_inject_the_whole_latency_if_user_cancelationtoken_is_signaled_from_timeout(TimeoutStrategy timeoutStrategy) - { - SystemClock.Reset(); - var timeout = TimeSpan.FromSeconds(5); - var delay = TimeSpan.FromSeconds(10); - var context = new Context(); - context["ShouldInjectLatency"] = true; - context["Enabled"] = true; - context["InjectionRate"] = 0.6; - - Boolean executed = false; - Stopwatch watch = new Stopwatch(); - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["Enabled"]; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func latencyProvider = (ctx, ct) => - { - if ((bool)ctx["ShouldInjectLatency"]) - { - return delay; - } - - return TimeSpan.FromMilliseconds(0); - }; - - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - var policy = Policy.Timeout(timeout, timeoutStrategy) - .Wrap(MonkeyPolicy.InjectLatency(latencyProvider, injectionRate, enabled)); - - watch.Start(); - policy.Invoking(x => { x.Execute(action, context, cts.Token); }) - .ShouldThrow(); - watch.Stop(); - } - - executed.Should().BeFalse(); - watch.Elapsed.Should().BeCloseTo(timeout, ((int)TimeSpan.FromSeconds(3).TotalMilliseconds)); - } - - #endregion - } -} diff --git a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultAsyncSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultAsyncSpecs.cs deleted file mode 100644 index 60ed231..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultAsyncSpecs.cs +++ /dev/null @@ -1,439 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using Polly.Contrib.Simmy.Utilities; -using Polly.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Outcomes -{ - [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectFaultAsyncSpecs : IDisposable - { - public InjectFaultAsyncSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Basic Overload, Exception, Context Free - [Fact] - public void InjectFault_Context_Free_Enabled_Should_not_execute_user_delegate_async() - { - string exceptionMessage = "exceptionMessage"; - Exception fault = new Exception(exceptionMessage); - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.6, () => true); - Boolean executed = false; - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)).ShouldThrowExactly().WithMessage(exceptionMessage); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_async() - { - Exception fault = new Exception(); - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.3, () => true); - Boolean executed = false; - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)).ShouldNotThrow(); - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_not_throw_if_injected_fault_is_permitted_null() - { - Exception fault = null; - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.3, () => true); - - Boolean executed = false; - Func actionAsync = () => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)).ShouldNotThrow(); - - executed.Should().BeTrue(); - } - #endregion - - #region Basic Overload, Exception, With Context - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_async() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - var policy = MonkeyPolicy.InjectFaultAsync( - new Exception("test"), - 0.6, - async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["ShouldFail"]); - }); - - Func actionAsync = (_) => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_execute_user_delegate_async() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - var policy = MonkeyPolicy.InjectFaultAsync( - new Exception("test"), - 0.4, - async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["ShouldFail"]); - }); - - Func actionAsync = _ => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldNotThrow(); - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFault_With_Context_Should_execute_user_delegate_async_with_enabled_lambda_false() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = false; - var policy = MonkeyPolicy.InjectFaultAsync( - new Exception("test"), - 0.6, - async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["ShouldFail"]); - }); - - Func actionAsync = (_) => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldNotThrow(); - executed.Should().BeTrue(); - } - #endregion - - #region Overload, All based on context - - [Fact] - public void InjectFault_should_throw_if_injection_rate_is_out_of_range_too_low() - { - Boolean executed = false; - Context context = new Context(); - - Func> fault = (ctx, cts) => Task.FromResult(new Exception()); - Func> enabled = (ctx, ct) => Task.FromResult(true); - Func> injectionRate = (ctx, ct) => Task.FromResult(-0.1); - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - Func actionAsync = (_) => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly(); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_should_throw_if_injection_rate_is_out_of_range_too_high() - { - Boolean executed = false; - Context context = new Context(); - - Func> fault = (ctx, cts) => Task.FromResult(new Exception()); - Func> enabled = (ctx, ct) => Task.FromResult(true); - Func> injectionRate = (ctx, ct) => Task.FromResult(1.01); - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - Func actionAsync = (_) => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly(); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_async_basic() - { - Boolean executed = false; - Context context = new Context(); - - Func> fault = (ctx, cts) => Task.FromResult(new Exception()); - Func> enabled = (ctx, ct) => Task.FromResult(true); - Func> injectionRate = (ctx, ct) => Task.FromResult(0.6); - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - Func actionAsync = (_) => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly(); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_async_with_all_context_set() - { - Boolean executed = false; - Context context = new Context(); - string failureMessage = "Failure Message"; - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - Func actionAsync = (_) => { executed = true; return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) - .ShouldThrowExactly() - .WithMessage(failureMessage); - executed.Should().BeFalse(); - } - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func actionAsync = (ctx, ct) => - { - executed = true; - return TaskHelper.EmptyTask; - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func actionAsync = (ctx, ct) => - { - executed = true; - return TaskHelper.EmptyTask; - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = (ctx, ct) => - { - cts.Cancel(); - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func actionAsync = (ctx, ct) => - { - executed = true; - return TaskHelper.EmptyTask; - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - cts.Cancel(); - return Task.FromResult(rate); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_fault_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func actionAsync = (ctx, ct) => - { - executed = true; - return TaskHelper.EmptyTask; - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> fault = (ctx, ct) => - { - cts.Cancel(); - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - #endregion - } -} diff --git a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultSpecs.cs deleted file mode 100644 index 62d6abd..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultSpecs.cs +++ /dev/null @@ -1,423 +0,0 @@ -using System; -using System.Threading; -using FluentAssertions; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Outcomes -{ - [Collection(Helpers.Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectFaultSpecs : IDisposable - { - public InjectFaultSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Basic Overload, Exception, Context Free - [Fact] - public void InjectFault_Context_Free_Enabled_Should_not_execute_user_delegate() - { - string exceptionMessage = "exceptionMessage"; - Exception fault = new Exception(exceptionMessage); - var policy = MonkeyPolicy.InjectFault(fault, 0.6, () => true); - - Boolean executed = false; - policy.Invoking(x => x.Execute(() => { executed = true; })) - .ShouldThrowExactly().WithMessage(exceptionMessage); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate() - { - Exception fault = new Exception("test"); - var policy = MonkeyPolicy.InjectFault(fault, 0.3, () => true); - - Boolean executed = false; - policy.Invoking(x => x.Execute(() => { executed = true; })) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_not_throw_if_injected_fault_is_permitted_null() - { - Exception fault = null; - var policy = MonkeyPolicy.InjectFault(fault, 0.6, () => true); - - Boolean executed = false; - policy.Invoking(x => x.Execute(() => { executed = true; })) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - - #endregion - - #region Basic Overload, Exception, With Context - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - var policy = MonkeyPolicy.InjectFault( - new Exception("test"), - 0.6, - (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }); - - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_execute_user_delegate() - { - Exception fault = new Exception("test"); - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - var policy = MonkeyPolicy.InjectFault( - new Exception("test"), - 0.4, - (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }); - - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFault_With_Context_Should_execute_user_delegate_with_enabled_lambda_return_false() - { - Exception fault = new Exception("test"); - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = false; - var policy = MonkeyPolicy.InjectFault( - new Exception("test"), - 0.6, - (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }); - - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - #endregion - - #region Overload, All based on context - - [Fact] - public void InjectFault_should_throw_if_injection_rate_is_out_of_range_too_low() - { - Boolean executed = false; - Context context = new Context(); - - Func fault = (ctx, ct) => new Exception(); - Func injectionRate = (ctx, ct) => -0.1; - Func enabled = (ctx, ct) => true; - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_should_throw_if_injection_rate_is_out_of_range_too_high() - { - Boolean executed = false; - Context context = new Context(); - - Func fault = (ctx, ct) => new Exception(); - Func injectionRate = (ctx, ct) => 1.01; - Func enabled = (ctx, ct) => true; - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_with_default_context() - { - Boolean executed = false; - Context context = new Context(); - - Func fault = (ctx, ct) => new Exception(); - Func injectionRate = (ctx, ct) => 0.6; - Func enabled = (ctx, ct) => true; - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_with_all_context_set() - { - Boolean executed = false; - Context context = new Context(); - string failureMessage = "Failure Message"; - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, ct) => - { - if (ctx["Message"] != null) - { - return new InvalidOperationException(ctx["Message"].ToString()); - } - - return new Exception(); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) - .ShouldThrowExactly().WithMessage(failureMessage); - - executed.Should().BeFalse(); - } - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return rate; - }; - - Func enabled = (ctx, ct) => - { - return (bool)ctx["ShouldFail"]; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return rate; - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - cts.Cancel(); - return (bool)ctx["ShouldFail"]; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["ShouldFail"]; - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - cts.Cancel(); - return rate; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_fault_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["ShouldFail"]; - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return rate; - }; - - Func fault = (ctx, ct) => - { - cts.Cancel(); - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - - policy.Invoking(x => x.Execute((ctx, ct) => { executed = true; }, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultAsyncSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultAsyncSpecs.cs deleted file mode 100644 index c326c25..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultAsyncSpecs.cs +++ /dev/null @@ -1,589 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using Polly.Contrib.Simmy.Specs.Helpers; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Outcomes -{ - [Collection(Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectFaultTResultAsyncSpecs : IDisposable - { - public InjectFaultTResultAsyncSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Basic Overload, Exception, Context Free - [Fact] - public void InjectFault_Context_Free_Enabled_Should_not_execute_user_delegate_async() - { - string exceptionMessage = "exceptionMessage"; - Exception fault = new Exception(exceptionMessage); - Boolean executed = false; - Func> actionAsync = () => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.6, () => true); - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)) - .ShouldThrowExactly() - .WithMessage(exceptionMessage); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_async() - { - string exceptionMessage = "exceptionMessage"; - Exception fault = new Exception(exceptionMessage); - Boolean executed = false; - Func> actionAsync = () => { executed = true; return Task.FromResult(ResultPrimitive.Good); }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.3, () => true); - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)) - .ShouldNotThrow(); - executed.Should().BeTrue(); - } - #endregion - - #region Basic Overload, Exception, With Context - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_async() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func> actionAsync = (ctx) => - { - executed = true; return Task.FromResult(ResultPrimitive.Good); - }; - - var policy = MonkeyPolicy.InjectFaultAsync( - new Exception(), - 0.6, - async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["ShouldFail"]); - }); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly(); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_execute_user_delegate_async() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func> actionAsync = (ctx) => - { - executed = true; return Task.FromResult(ResultPrimitive.Good); - }; - - var policy = MonkeyPolicy.InjectFaultAsync( - new Exception(), - 0.4, - async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["ShouldFail"]); - }); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFault_With_Context_Should_execute_user_delegate_async_with_enabled_lambda_return_false() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = false; - Func> actionAsync = (ctx) => - { - executed = true; return Task.FromResult(ResultPrimitive.Good); - }; - - var policy = MonkeyPolicy.InjectFaultAsync( - new Exception(), - 0.4, - async (ctx, ct) => - { - return await Task.FromResult((bool)ctx["ShouldFail"]); - }); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - #endregion - - #region Overload, All based on context - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_async_with_default_values() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func> actionAsync = (ctx) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => Task.FromResult(new Exception()); - Func> injectionRate = (ctx, ct) => Task.FromResult(0.6); - Func> enabled = (ctx, ct) => Task.FromResult(true); - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) - .ShouldThrowExactly(); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_async_with_all_values_set() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func> actionAsync = (ctx) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) - .ShouldThrowExactly(); - executed.Should().BeFalse(); - } - - #endregion - - #region TResult Based Monkey Policies - [Fact] - public async Task InjectFault_Context_Free_Should_Return_Fault_async() - { - Boolean executed = false; - Func> actionAsync = () => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - ResultPrimitive fault = ResultPrimitive.Fault; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.6, () => true); - ResultPrimitive response = await policy.ExecuteAsync(actionAsync); - response.Should().Be(ResultPrimitive.Fault); - executed.Should().BeFalse(); - } - - [Fact] - public async Task InjectFault_Context_Free_Should_Not_Return_Fault_async() - { - Boolean executed = false; - Func> actionAsync = () => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - ResultPrimitive fault = ResultPrimitive.Fault; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.4, () => true); - ResultPrimitive response = await policy.ExecuteAsync(actionAsync); - response.Should().Be(ResultPrimitive.Good); - executed.Should().BeTrue(); - } - - [Fact] - public async Task InjectFault_With_Context_Enabled_Should_Return_Fault_async() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func> actionAsync = (ctx) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - ResultPrimitive fault = ResultPrimitive.Fault; - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.6, enabled); - ResultPrimitive response = await policy.ExecuteAsync(actionAsync, context); - response.Should().Be(ResultPrimitive.Fault); - executed.Should().BeFalse(); - } - - [Fact] - public async Task InjectFault_With_Context_Enabled_Should_Not_Return_Fault_async() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = false; - Func> actionAsync = (ctx) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - ResultPrimitive fault = ResultPrimitive.Fault; - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, 0.6, enabled); - ResultPrimitive response = await policy.ExecuteAsync(actionAsync, context); - response.Should().Be(ResultPrimitive.Good); - executed.Should().BeTrue(); - } - - [Fact] - public async Task InjectFault_With_Context_InjectionRate_Should_Return_Fault_async() - { - Boolean executed = false; - Context context = new Context(); - context["InjectionRate"] = 0.6; - - Func> actionAsync = (ctx) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => - { - return Task.FromResult(ResultPrimitive.Fault); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> enabled = (ctx, ct) => - { - return Task.FromResult(true); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - ResultPrimitive response = await policy.ExecuteAsync(actionAsync, context); - response.Should().Be(ResultPrimitive.Fault); - executed.Should().BeFalse(); - } - - [Fact] - public async Task InjectFault_With_Context_InjectionRate_Should_Not_Return_Fault_async() - { - Boolean executed = false; - Context context = new Context(); - context["InjectionRate"] = 0.4; - - Func> actionAsync = (ctx) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => - { - return Task.FromResult(ResultPrimitive.Fault); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> enabled = (ctx, ct) => - { - return Task.FromResult(true); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - ResultPrimitive response = await policy.ExecuteAsync(actionAsync, context); - response.Should().Be(ResultPrimitive.Good); - executed.Should().BeTrue(); - } - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func> actionAsync = (ctx, ct) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func> actionAsync = (ctx, ct) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = (ctx, ct) => - { - cts.Cancel(); - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func> actionAsync = (ctx, ct) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - Func> fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - cts.Cancel(); - return Task.FromResult(rate); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_fault_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func> actionAsync = (ctx, ct) => - { - executed = true; - return Task.FromResult(ResultPrimitive.Good); - }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func> enabled = (ctx, ct) => - { - return Task.FromResult((bool)ctx["ShouldFail"]); - }; - - Func> injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return Task.FromResult(rate); - }; - - Func> fault = (ctx, ct) => - { - cts.Cancel(); - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return Task.FromResult(ex); - } - - return Task.FromResult(new Exception()); - }; - - var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); - - policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultSpecs.cs b/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultSpecs.cs deleted file mode 100644 index c5e1f17..0000000 --- a/src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultSpecs.cs +++ /dev/null @@ -1,528 +0,0 @@ -using System; -using System.Threading; -using FluentAssertions; -using Polly.Contrib.Simmy.Specs.Helpers; -using Polly.Contrib.Simmy.Utilities; -using Xunit; - -namespace Polly.Contrib.Simmy.Specs.Outcomes -{ - [Collection(Constants.AmbientContextDependentTestCollection)] - [Obsolete] - public class InjectFaultTResultSpecs : IDisposable - { - public InjectFaultTResultSpecs() - { - ThreadSafeRandom_LockOncePerThread.NextDouble = () => 0.5; - } - - public void Dispose() - { - ThreadSafeRandom_LockOncePerThread.Reset(); - } - - #region Basic Overload, Exception, Context Free - [Fact] - public void InjectFaultContext_Free_Enabled_Should_not_execute_user_delegate() - { - string exceptionMessage = "exceptionMessage"; - Exception fault = new Exception(exceptionMessage); - Boolean executed = false; - Func action = () => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault(fault, 0.6, () => true); - policy.Invoking(x => x.Execute(action)) - .ShouldThrowExactly().WithMessage(exceptionMessage); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFaultContext_Free_Enabled_Should_execute_user_delegate() - { - string exceptionMessage = "exceptionMessage"; - Exception fault = new Exception(exceptionMessage); - Boolean executed = false; - Func action = () => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault(fault, 0.3, () => true); - policy.Invoking(x => x.Execute(action)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - #endregion - - #region Basic Overload, Exception, With Context - [Fact] - public void InjectFaultWith_Context_Should_not_execute_user_delegate() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault( - new Exception(), - 0.6, - (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }); - - policy.Invoking(x => x.Execute(action, context)) - .ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFaultWith_Context_Should_execute_user_delegate() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault( - new Exception(), - 0.4, - (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }); - - policy.Invoking(x => x.Execute(action, context)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFaultWith_Context_Should_execute_user_delegate_with_enabled_lambda_disabled() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = false; - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault( - new Exception(), - 0.6, - (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }); - - policy.Invoking(x => x.Execute(action, context)) - .ShouldNotThrow(); - - executed.Should().BeTrue(); - } - #endregion - - #region Overload, All based on context - [Fact] - public void InjectFaultWith_Context_Should_not_execute_user_delegate_default_context() - { - Boolean executed = false; - Context context = new Context(); - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - - Func fault = (ctx, ct) => new Exception(); - Func injectionRate = (ctx, ct) => 0.6; - Func enabled = (ctx, ct) => true; - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - policy.Invoking(x => x.Execute(action, context)) - .ShouldThrow(); - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFaultWith_Context_Should_not_execute_user_delegate_full_context() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - - Func fault = (ctx, ct) => - { - if (ctx["Message"] != null) - { - return new InvalidOperationException(ctx["Message"].ToString()); - } - - return new Exception(); - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func enabled = (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - policy.Invoking(x => x.Execute(action, context)) - .ShouldThrowExactly(); - - executed.Should().BeFalse(); - } - #endregion - - #region TResult Based Monkey Policies - [Fact] - public void InjectFaultContext_Free_Should_Return_Fault() - { - Boolean executed = false; - Func action = () => { executed = true; return ResultPrimitive.Good; }; - ResultPrimitive fault = ResultPrimitive.Fault; - - var policy = MonkeyPolicy.InjectFault(fault, 0.6, () => true); - ResultPrimitive response = policy.Execute(action); - response.Should().Be(ResultPrimitive.Fault); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFaultContext_Free_Should_Not_Return_Fault() - { - Boolean executed = false; - Func action = () => { executed = true; return ResultPrimitive.Good; }; - ResultPrimitive fault = ResultPrimitive.Fault; - - var policy = MonkeyPolicy.InjectFault(fault, 0.4, () => true); - ResultPrimitive response = policy.Execute(action); - response.Should().Be(ResultPrimitive.Good); - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFaultWith_Context_Enabled_Should_Return_Fault() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - ResultPrimitive fault = ResultPrimitive.Fault; - Func enabled = (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFault(fault, 0.6, enabled); - ResultPrimitive response = policy.Execute(action, context); - response.Should().Be(ResultPrimitive.Fault); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFaultWith_Context_Enabled_Should_Not_Return_Fault() - { - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = false; - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - ResultPrimitive fault = ResultPrimitive.Fault; - Func enabled = (ctx, ct) => - { - return ((bool)ctx["ShouldFail"]); - }; - - var policy = MonkeyPolicy.InjectFault(fault, 0.6, enabled); - ResultPrimitive response = policy.Execute(action, context); - response.Should().Be(ResultPrimitive.Good); - executed.Should().BeTrue(); - } - - [Fact] - public void InjectFaultWith_Context_InjectionRate_Should_Return_Fault() - { - Boolean executed = false; - Context context = new Context(); - context["InjectionRate"] = 0.6; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - Func fault = (ctx, ct) => - { - return ResultPrimitive.Fault; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func enabled = (ctx, ct) => - { - return true; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - ResultPrimitive response = policy.Execute(action, context); - response.Should().Be(ResultPrimitive.Fault); - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFaultWith_Context_InjectionRate_Should_Not_Return_Fault() - { - Boolean executed = false; - Context context = new Context(); - context["InjectionRate"] = 0.4; - - Func action = (ctx) => { executed = true; return ResultPrimitive.Good; }; - Func fault = (ctx, ct) => - { - return ResultPrimitive.Fault; - }; - - Func injectionRate = (ctx, ct) => - { - if (ctx["InjectionRate"] != null) - { - return (double)ctx["InjectionRate"]; - } - - return 0; - }; - - Func enabled = (ctx, ct) => - { - return true; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - ResultPrimitive response = policy.Execute(action, context); - response.Should().Be(ResultPrimitive.Good); - executed.Should().BeTrue(); - } - #endregion - - #region Cancellable scenarios - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_before_to_start_execution() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return rate; - }; - - Func enabled = (ctx, ct) => - { - return (bool)ctx["ShouldFail"]; - }; - - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - cts.Cancel(); - - policy.Invoking(x => x.Execute((ctx, ct) => action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_enabled_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return rate; - }; - - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - cts.Cancel(); - return (bool)ctx["ShouldFail"]; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_injectionrate_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - Func fault = (ctx, cts) => - { - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["ShouldFail"]; - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - cts.Cancel(); - return rate; - }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - [Fact] - public void InjectFault_With_Context_Should_not_execute_user_delegate_if_user_cancelationtoken_cancelled_on_fault_config_delegate() - { - string failureMessage = "Failure Message"; - Boolean executed = false; - Context context = new Context(); - context["ShouldFail"] = true; - context["Message"] = failureMessage; - context["InjectionRate"] = 0.6; - - using (CancellationTokenSource cts = new CancellationTokenSource()) - { - Func enabled = (ctx, ct) => - { - return (bool)ctx["ShouldFail"]; - }; - - Func injectionRate = (ctx, ct) => - { - double rate = 0; - if (ctx["InjectionRate"] != null) - { - rate = (double)ctx["InjectionRate"]; - } - - return rate; - }; - - Func fault = (ctx, ct) => - { - cts.Cancel(); - if (ctx["Message"] != null) - { - Exception ex = new InvalidOperationException(ctx["Message"].ToString()); - return ex; - } - - return new Exception(); - }; - - Func action = (ctx, ct) => { executed = true; return ResultPrimitive.Good; }; - - var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); - - policy.Invoking(x => x.Execute(action, context, cts.Token)) - .ShouldThrow(); - } - - executed.Should().BeFalse(); - } - - #endregion - } -} diff --git a/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourPolicy.cs b/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourPolicy.cs index 2e6c4ab..19abdb0 100644 --- a/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourPolicy.cs +++ b/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourPolicy.cs @@ -11,13 +11,6 @@ public class AsyncInjectBehaviourPolicy : AsyncMonkeyPolicy { private readonly Func _behaviour; - [Obsolete] - internal AsyncInjectBehaviourPolicy(Func behaviour, Func> injectionRate, Func> enabled) - : base(injectionRate, enabled) - { - _behaviour = behaviour ?? throw new ArgumentNullException(nameof(behaviour)); - } - internal AsyncInjectBehaviourPolicy(InjectBehaviourAsyncOptions options) : base(options.InjectionRate, options.Enabled) { @@ -47,13 +40,6 @@ public class AsyncInjectBehaviourPolicy : AsyncMonkeyPolicy { private readonly Func _behaviour; - [Obsolete] - internal AsyncInjectBehaviourPolicy(Func behaviour, Func> injectionRate, Func> enabled) - : base(injectionRate, enabled) - { - _behaviour = behaviour ?? throw new ArgumentNullException(nameof(behaviour)); - } - internal AsyncInjectBehaviourPolicy(InjectBehaviourAsyncOptions options) : base(options.InjectionRate, options.Enabled) { diff --git a/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourSyntax.cs b/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourSyntax.cs deleted file mode 100644 index 3f63601..0000000 --- a/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourSyntax.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Polly.Contrib.Simmy.Behavior; - -namespace Polly.Contrib.Simmy -{ - /// - /// Fluent API for defining Monkey . - /// - public partial class MonkeyPolicy - { - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed without context - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Double injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task BehaviourLambda(Context _, CancellationToken __) => behaviour(); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviourAsync(BehaviourLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Double injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviourAsync(behaviour, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Double injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return InjectBehaviourAsync(behaviour, InjectionRateLambda, enabled); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Func> injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectBehaviourPolicy( - behaviour, - injectionRate, - enabled); - } - } -} diff --git a/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourTResultSyntax.cs b/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourTResultSyntax.cs deleted file mode 100644 index eb07e3e..0000000 --- a/src/Polly.Contrib.Simmy/Behavior/AsyncInjectBehaviourTResultSyntax.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Polly.Contrib.Simmy.Behavior; - -namespace Polly.Contrib.Simmy -{ - /// - /// Fluent API for defining Monkey . - /// - public partial class MonkeyPolicy - { - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed without context - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Double injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task BehaviourLambda(Context _, CancellationToken __) => behaviour(); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviourAsync(BehaviourLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Double injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviourAsync(behaviour, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Double injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return InjectBehaviourAsync(behaviour, InjectionRateLambda, enabled); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectBehaviourPolicy InjectBehaviourAsync( - Func behaviour, - Func> injectionRate, - Func> enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectBehaviourPolicy( - behaviour, - injectionRate, - enabled); - } - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourPolicy.cs b/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourPolicy.cs index 44fc67d..c38c6ea 100644 --- a/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourPolicy.cs +++ b/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourPolicy.cs @@ -10,13 +10,6 @@ public class InjectBehaviourPolicy : Simmy.MonkeyPolicy { private readonly Action _behaviour; - [Obsolete] - internal InjectBehaviourPolicy(Action behaviour, Func injectionRate, Func enabled) - : base(injectionRate, enabled) - { - _behaviour = behaviour ?? throw new ArgumentNullException(nameof(behaviour)); - } - internal InjectBehaviourPolicy(InjectBehaviourOptions options) : base(options.InjectionRate, options.Enabled) { @@ -43,13 +36,6 @@ public class InjectBehaviourPolicy : MonkeyPolicy { private readonly Action _behaviour; - [Obsolete] - internal InjectBehaviourPolicy(Action behaviour, Func injectionRate, Func enabled) - : base(injectionRate, enabled) - { - _behaviour = behaviour ?? throw new ArgumentNullException(nameof(behaviour)); - } - internal InjectBehaviourPolicy(InjectBehaviourOptions options) : base(options.InjectionRate, options.Enabled) { diff --git a/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourSyntax.cs b/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourSyntax.cs deleted file mode 100644 index fd04aa2..0000000 --- a/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourSyntax.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Threading; -using Polly.Contrib.Simmy.Behavior; - -namespace Polly.Contrib.Simmy -{ - /// - /// Fluent API for defining Monkey . - /// - public partial class MonkeyPolicy - { - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed without context - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Double injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - void BehaviourLambda(Context _, CancellationToken __) => behaviour(); - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviour(BehaviourLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Double injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviour(behaviour, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Double injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - return InjectBehaviour(behaviour, (Func)InjectionRateLambda, enabled); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Func injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectBehaviourPolicy( - behaviour, - injectionRate, - enabled); - } - } -} diff --git a/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourTResultSyntax.cs b/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourTResultSyntax.cs deleted file mode 100644 index 5b56306..0000000 --- a/src/Polly.Contrib.Simmy/Behavior/InjectBehaviourTResultSyntax.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Threading; -using Polly.Contrib.Simmy.Behavior; - -namespace Polly.Contrib.Simmy -{ - /// - /// Fluent API for defining Monkey . - /// - public partial class MonkeyPolicy - { - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed without context - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Double injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - void BehaviourLambda(Context _, CancellationToken __) => behaviour(); - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviour(BehaviourLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free mode - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Double injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return InjectBehaviour(behaviour, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// The injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Double injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - return InjectBehaviour(behaviour, InjectionRateLambda, enabled); - } - - /// - /// Builds a which executes a behaviour if returns true and - /// a random number is within range of . - /// - /// Behaviour Delegate to be executed - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectBehaviourPolicy InjectBehaviour( - Action behaviour, - Func injectionRate, - Func enabled) - { - if (behaviour == null) throw new ArgumentNullException(nameof(behaviour)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectBehaviourPolicy( - behaviour, - injectionRate, - enabled); - } - } -} diff --git a/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencyPolicy.cs b/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencyPolicy.cs index ba1ee09..81d6003 100644 --- a/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencyPolicy.cs +++ b/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencyPolicy.cs @@ -12,16 +12,6 @@ public class AsyncInjectLatencyPolicy : AsyncMonkeyPolicy { private readonly Func> _latencyProvider; - [Obsolete] - internal AsyncInjectLatencyPolicy( - Func> latencyProvider, - Func> injectionRate, - Func> enabled) - : base(injectionRate, enabled) - { - _latencyProvider = latencyProvider ?? throw new ArgumentNullException(nameof(latencyProvider)); - } - internal AsyncInjectLatencyPolicy(InjectLatencyAsyncOptions options) : base(options.InjectionRate, options.Enabled) { @@ -63,16 +53,6 @@ public class AsyncInjectLatencyPolicy : AsyncMonkeyPolicy { private readonly Func> _latencyProvider; - [Obsolete] - internal AsyncInjectLatencyPolicy( - Func> latencyProvider, - Func> injectionRate, - Func> enabled) - : base(injectionRate, enabled) - { - _latencyProvider = latencyProvider ?? throw new ArgumentNullException(nameof(latencyProvider)); - } - internal AsyncInjectLatencyPolicy(InjectLatencyAsyncOptions options) : base(options.InjectionRate, options.Enabled) { diff --git a/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencySyntax.cs b/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencySyntax.cs deleted file mode 100644 index 3060696..0000000 --- a/src/Polly.Contrib.Simmy/Latency/AsyncInjectLatencySyntax.cs +++ /dev/null @@ -1,187 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Polly.Contrib.Simmy.Latency; - -namespace Polly.Contrib.Simmy -{ - public partial class MonkeyPolicy - { - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// The latency to inject - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - TimeSpan latency, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task LatencyProvider(Context _, CancellationToken __) => Task.FromResult(latency); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => Task.FromResult(enabled()); - - return new AsyncInjectLatencyPolicy(LatencyProvider, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// The latency to inject - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - TimeSpan latency, - Double injectionRate, - Func> enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task LatencyProvider(Context _, CancellationToken __) => Task.FromResult(latency); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return new AsyncInjectLatencyPolicy(LatencyProvider, InjectionRateLambda, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - TimeSpan latency, - Func> injectionRate, - Func> enabled) - { - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task LatencyProvider(Context _, CancellationToken __) => Task.FromResult(latency); - return new AsyncInjectLatencyPolicy(LatencyProvider, injectionRate, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - Func> latency, - Func> injectionRate, - Func> enabled) - { - if (latency == null) throw new ArgumentNullException(nameof(latency)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectLatencyPolicy(latency, injectionRate, enabled); - } - } - - public partial class MonkeyPolicy - { - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - TimeSpan latency, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task LatencyProvider(Context _, CancellationToken __) => Task.FromResult(latency); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => Task.FromResult(enabled()); - - return new AsyncInjectLatencyPolicy(LatencyProvider, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - TimeSpan latency, - Double injectionRate, - Func> enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task LatencyProvider(Context _, CancellationToken __) => Task.FromResult(latency); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return new AsyncInjectLatencyPolicy(LatencyProvider, InjectionRateLambda, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - TimeSpan latency, - Func> injectionRate, - Func> enabled) - { - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task LatencyProvider(Context _, CancellationToken __) => Task.FromResult(latency); - return new AsyncInjectLatencyPolicy(LatencyProvider, injectionRate, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectLatencyPolicy InjectLatencyAsync( - Func> latency, - Func> injectionRate, - Func> enabled) - { - if (latency == null) throw new ArgumentNullException(nameof(latency)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectLatencyPolicy(latency, injectionRate, enabled); - } - } -} diff --git a/src/Polly.Contrib.Simmy/Latency/InjectLatencyPolicy.cs b/src/Polly.Contrib.Simmy/Latency/InjectLatencyPolicy.cs index a4d372c..40962ee 100644 --- a/src/Polly.Contrib.Simmy/Latency/InjectLatencyPolicy.cs +++ b/src/Polly.Contrib.Simmy/Latency/InjectLatencyPolicy.cs @@ -11,16 +11,6 @@ public class InjectLatencyPolicy : MonkeyPolicy { private readonly Func _latencyProvider; - [Obsolete] - internal InjectLatencyPolicy( - Func latencyProvider, - Func injectionRate, - Func enabled) - : base(injectionRate, enabled) - { - _latencyProvider = latencyProvider ?? throw new ArgumentNullException(nameof(latencyProvider)); - } - internal InjectLatencyPolicy(InjectLatencyOptions options) : base(options.InjectionRate, options.Enabled) { @@ -55,16 +45,6 @@ public class InjectLatencyPolicy : MonkeyPolicy { private readonly Func _latencyProvider; - [Obsolete] - internal InjectLatencyPolicy( - Func latencyProvider, - Func injectionRate, - Func enabled) - : base(injectionRate, enabled) - { - _latencyProvider = latencyProvider ?? throw new ArgumentNullException(nameof(latencyProvider)); - } - internal InjectLatencyPolicy(InjectLatencyOptions options) : base(options.InjectionRate, options.Enabled) { diff --git a/src/Polly.Contrib.Simmy/Latency/InjectLatencySyntax.cs b/src/Polly.Contrib.Simmy/Latency/InjectLatencySyntax.cs deleted file mode 100644 index 1164401..0000000 --- a/src/Polly.Contrib.Simmy/Latency/InjectLatencySyntax.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; -using System.Threading; -using Polly.Contrib.Simmy.Latency; - -namespace Polly.Contrib.Simmy -{ - public partial class MonkeyPolicy - { - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// The latency to inject - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - TimeSpan latency, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy((_, __) => latency, (_, __) => injectionRate, (_, __) => enabled()); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// The latency to inject - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - TimeSpan latency, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy((_, __) => latency, (_, __) => injectionRate, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - TimeSpan latency, - Func injectionRate, - Func enabled) - { - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy((_, __) => latency, injectionRate, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - Func latency, - Func injectionRate, - Func enabled) - { - if (latency == null) throw new ArgumentNullException(nameof(latency)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy(latency, injectionRate, enabled); - } - } - - public partial class MonkeyPolicy - { - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - TimeSpan latency, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy((_, __) => latency, (_, __) => injectionRate, (_, __) => enabled()); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - TimeSpan latency, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy((_, __) => latency, (_, __) => injectionRate, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - TimeSpan latency, - Func injectionRate, - Func enabled) - { - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy((_, __) => latency, injectionRate, enabled); - } - - /// - /// Builds an which injects latency if returns true and - /// a random number is within range of . - /// - /// lambda to get the latency object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectLatencyPolicy InjectLatency( - Func latency, - Func injectionRate, - Func enabled) - { - if (latency == null) throw new ArgumentNullException(nameof(latency)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectLatencyPolicy(latency, injectionRate, enabled); - } - } -} diff --git a/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomePolicy.cs b/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomePolicy.cs index cb07314..8422357 100644 --- a/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomePolicy.cs +++ b/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomePolicy.cs @@ -12,13 +12,6 @@ public class AsyncInjectOutcomePolicy : AsyncMonkeyPolicy { private readonly Func> _faultProvider; - [Obsolete] - internal AsyncInjectOutcomePolicy(Func> faultProvider, Func> injectionRate, Func> enabled) - : base(injectionRate, enabled) - { - _faultProvider = faultProvider ?? throw new ArgumentNullException(nameof(faultProvider)); - } - internal AsyncInjectOutcomePolicy(InjectOutcomeAsyncOptions options) : base(options.InjectionRate, options.Enabled) { @@ -48,20 +41,6 @@ public class AsyncInjectOutcomePolicy : AsyncMonkeyPolicy private readonly Func> _faultProvider; private readonly Func> _resultProvider; - [Obsolete] - internal AsyncInjectOutcomePolicy(Func> faultProvider, Func> injectionRate, Func> enabled) - : base(injectionRate, enabled) - { - _faultProvider = faultProvider ?? throw new ArgumentNullException(nameof(faultProvider)); - } - - [Obsolete] - internal AsyncInjectOutcomePolicy(Func> resultProvider, Func> injectionRate, Func> enabled) - : base(injectionRate, enabled) - { - _resultProvider = resultProvider ?? throw new ArgumentNullException(nameof(resultProvider)); - } - internal AsyncInjectOutcomePolicy(InjectOutcomeAsyncOptions options) : base(options.InjectionRate, options.Enabled) { diff --git a/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomeSyntax.cs b/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomeSyntax.cs deleted file mode 100644 index 989fce2..0000000 --- a/src/Polly.Contrib.Simmy/Outcomes/AsyncInjectOutcomeSyntax.cs +++ /dev/null @@ -1,219 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Polly.Contrib.Simmy.Outcomes; - -namespace Polly.Contrib.Simmy -{ - public partial class MonkeyPolicy - { - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Exception fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task FaultLambda(Context _, CancellationToken __) => Task.FromResult(fault); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => Task.FromResult(enabled()); - - return new AsyncInjectOutcomePolicy(FaultLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Exception fault, - Double injectionRate, - Func> enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task FaultLambda(Context _, CancellationToken __) => Task.FromResult(fault); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return new AsyncInjectOutcomePolicy(FaultLambda, InjectionRateLambda, enabled); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// lambda to get the fault exception object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Func> faultProvider, - Func> injectionRate, - Func> enabled) - { - if (faultProvider == null) throw new ArgumentNullException(nameof(faultProvider)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectOutcomePolicy(faultProvider, injectionRate, enabled); - } - } - - public partial class MonkeyPolicy - { - #region Exception Based Faults - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Exception fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task FaultLambda(Context _, CancellationToken __) => Task.FromResult(fault); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) => Task.FromResult(enabled()); - - return new AsyncInjectOutcomePolicy((Func>)FaultLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Exception fault, - Double injectionRate, - Func> enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task FaultLambda(Context _, CancellationToken __) => Task.FromResult(fault); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return new AsyncInjectOutcomePolicy((Func>)FaultLambda, InjectionRateLambda, enabled); - } - - /// - /// Builds an which executes a fault if returns true and - /// a random number is within range of . - /// - /// lambda to get the fault exception object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Func> faultProvider, - Func> injectionRate, - Func> enabled) - { - if (faultProvider == null) throw new ArgumentNullException(nameof(faultProvider)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectOutcomePolicy(faultProvider, injectionRate, enabled); - } - #endregion - - #region TResult based Faults - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - TResult fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task FaultLambda(Context _, CancellationToken __) => Task.FromResult(fault); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - Task EnabledLambda(Context _, CancellationToken __) - { - return Task.FromResult(enabled()); - } - - return new AsyncInjectOutcomePolicy((Func>)FaultLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - TResult fault, - Double injectionRate, - Func> enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Task FaultLambda(Context _, CancellationToken __) => Task.FromResult(fault); - Task InjectionRateLambda(Context _, CancellationToken __) => Task.FromResult(injectionRate); - - return new AsyncInjectOutcomePolicy((Func>)FaultLambda, InjectionRateLambda, enabled); - } - - /// - /// Builds an which executes a fault if returns true and - /// a random number is within range of . - /// - /// lambda to get the fault exception object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static AsyncInjectOutcomePolicy InjectFaultAsync( - Func> fault, - Func> injectionRate, - Func> enabled) - { - if (fault == null) throw new ArgumentNullException(nameof(fault)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new AsyncInjectOutcomePolicy(fault, injectionRate, enabled); - } - #endregion - } -} \ No newline at end of file diff --git a/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomePolicy.cs b/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomePolicy.cs index 7411d4b..fe4a043 100644 --- a/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomePolicy.cs +++ b/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomePolicy.cs @@ -11,13 +11,6 @@ public class InjectOutcomePolicy : MonkeyPolicy { private readonly Func _faultProvider; - [Obsolete] - internal InjectOutcomePolicy(Func faultProvider, Func injectionRate, Func enabled) - : base(injectionRate, enabled) - { - _faultProvider = faultProvider ?? throw new ArgumentNullException(nameof(faultProvider)); - } - internal InjectOutcomePolicy(InjectOutcomeOptions options) : base(options.InjectionRate, options.Enabled) { @@ -45,20 +38,6 @@ public class InjectOutcomePolicy : MonkeyPolicy private readonly Func _faultProvider; private readonly Func _resultProvider; - [Obsolete] - internal InjectOutcomePolicy(Func faultProvider, Func injectionRate, Func enabled) - : base(injectionRate, enabled) - { - _faultProvider = faultProvider ?? throw new ArgumentNullException(nameof(faultProvider)); - } - - [Obsolete] - internal InjectOutcomePolicy(Func resultProvider, Func injectionRate, Func enabled) - : base(injectionRate, enabled) - { - _resultProvider = resultProvider ?? throw new ArgumentNullException(nameof(resultProvider)); - } - internal InjectOutcomePolicy(InjectOutcomeOptions options) : base(options.InjectionRate, options.Enabled) { diff --git a/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomeSyntax.cs b/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomeSyntax.cs deleted file mode 100644 index ac98d7e..0000000 --- a/src/Polly.Contrib.Simmy/Outcomes/InjectOutcomeSyntax.cs +++ /dev/null @@ -1,217 +0,0 @@ -using System; -using System.Threading; -using Polly.Contrib.Simmy.Outcomes; - -namespace Polly.Contrib.Simmy -{ - public partial class MonkeyPolicy - { - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Exception fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Exception FaultLambda(Context _, CancellationToken __) => fault; - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return new InjectOutcomePolicy(FaultLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Exception fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Exception FaultLambda(Context _, CancellationToken __) => fault; - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - - return new InjectOutcomePolicy(FaultLambda, InjectionRateLambda, enabled); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// lambda to get the fault exception object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Func faultProvider, - Func injectionRate, - Func enabled) - { - if (faultProvider == null) throw new ArgumentNullException(nameof(faultProvider)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectOutcomePolicy(faultProvider, injectionRate, enabled); - } - } - - public partial class MonkeyPolicy - { - #region Exception Based Faults - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Exception fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Exception FaultLambda(Context _, CancellationToken __) => fault; - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return new InjectOutcomePolicy((Func)FaultLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault exception object to throw - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Exception fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - Exception FaultLambda(Context _, CancellationToken __) => fault; - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - - return new InjectOutcomePolicy((Func)FaultLambda, InjectionRateLambda, enabled); - } - - /// - /// Builds an which executes a fault if returns true and - /// a random number is within range of . - /// - /// lambda to get the fault exception object - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Func faultProvider, - Func injectionRate, - Func enabled) - { - if (faultProvider == null) throw new ArgumentNullException(nameof(faultProvider)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectOutcomePolicy(faultProvider, injectionRate, enabled); - } - - #endregion - - #region TResult Based Faults - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault result object to inject - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in context free fashion - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - TResult fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - TResult FaultLambda(Context _, CancellationToken __) => fault; - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - bool EnabledLambda(Context _, CancellationToken __) => enabled(); - - return new InjectOutcomePolicy((Func)FaultLambda, InjectionRateLambda, EnabledLambda); - } - - /// - /// Builds an which injects a fault if returns true and - /// a random number is within range of . - /// - /// The fault result object to inject - /// injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - TResult fault, - Double injectionRate, - Func enabled) - { - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - TResult FaultLambda(Context _, CancellationToken __) => fault; - double InjectionRateLambda(Context _, CancellationToken __) => injectionRate; - - return new InjectOutcomePolicy((Func)FaultLambda, InjectionRateLambda, enabled); - } - - /// - /// Builds an which executes a fault if returns true and - /// a random number is within range of . - /// - /// The fault result object to inject - /// lambda to get injection rate between [0, 1] - /// Lambda to check if this policy is enabled in current context - /// The policy instance. - [Obsolete("This overload is going to be deprecated, use the overload which takes Action instead.")] - public static InjectOutcomePolicy InjectFault( - Func faultProvider, - Func injectionRate, - Func enabled) - { - if (faultProvider == null) throw new ArgumentNullException(nameof(faultProvider)); - if (injectionRate == null) throw new ArgumentNullException(nameof(injectionRate)); - if (enabled == null) throw new ArgumentNullException(nameof(enabled)); - - return new InjectOutcomePolicy(faultProvider, injectionRate, enabled); - } - - #endregion - } -}