public void InjectFault_Context_Free_Enabled_Should_not_execute_user_delegate_async() { string exceptionMessage = "exceptionMessage"; Exception fault = new Exception(exceptionMessage); Boolean executed = false; Func <Task <ResultPrimitive> > actionAsync = () => { executed = true; return(Task.FromResult(ResultPrimitive.Good)); }; var policy = MonkeyPolicy.InjectFaultAsync <ResultPrimitive>(fault, 0.6, () => true); policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)) .ShouldThrowExactly <Exception>() .WithMessage(exceptionMessage); executed.Should().BeFalse(); }
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 <Context, CancellationToken, Task <Exception> > fault = (ctx, cts) => { if (ctx["Message"] != null) { Exception ex = new InvalidOperationException(ctx["Message"].ToString()); return(Task.FromResult(ex)); } return(Task.FromResult(new Exception())); }; Func <Context, CancellationToken, Task <Double> > injectionRate = (ctx, ct) => { double rate = 0; if (ctx["InjectionRate"] != null) { rate = (double)ctx["InjectionRate"]; } return(Task.FromResult(rate)); }; Func <Context, CancellationToken, Task <bool> > enabled = (ctx, ct) => { return(Task.FromResult((bool)ctx["ShouldFail"])); }; var policy = MonkeyPolicy.InjectExceptionAsync(with => with.Fault(fault) .InjectionRate(injectionRate) .EnabledWhen(enabled) ); Func <Context, Task> actionAsync = (_) => { executed = true; return(TaskHelper.EmptyTask); }; policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) .ShouldThrowExactly <InvalidOperationException>() .WithMessage(failureMessage); executed.Should().BeFalse(); }
public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() { var executed = false; var policy = MonkeyPolicy.InjectLatency(with => with.Latency(TimeSpan.FromMilliseconds(500)) .InjectionRate(0.6) .Enabled(false) ); policy.Execute(() => { executed = true; }); executed.Should().BeTrue(); _totalTimeSlept.Should().Be(0); }
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 <Context, CancellationToken, Task <TimeSpan> > latencyProvider = async(ctx, ct) => { if ((bool)ctx["ShouldInjectLatency"]) { return(await Task.FromResult(delay)); } return(await Task.FromResult(TimeSpan.FromMilliseconds(0))); }; Boolean executed = false; Func <Context, CancellationToken, Task> actionAsync = (_, ct) => { executed = true; return(TaskHelper.EmptyTask); }; using (CancellationTokenSource cts = new CancellationTokenSource()) { Func <Context, CancellationToken, Task <bool> > enabled = async(ctx, ct) => { return(await Task.FromResult((bool)ctx["Enabled"])); }; Func <Context, CancellationToken, Task <double> > 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 <OperationCanceledException>(); } executed.Should().BeFalse(); _totalTimeSlept.Should().Be(0); }
public void InjectLatency_With_Context_With_Latency_Lambda_Should_Not_Introduce_Delay() { var delay = TimeSpan.FromMilliseconds(500); var context = new Context { ["ShouldInjectLatency"] = false, ["Enabled"] = true, ["InjectionRate"] = 0.6 }; Func <Context, CancellationToken, TimeSpan> latencyProvider = (ctx, ct) => { if ((bool)ctx["ShouldInjectLatency"]) { return(delay); } return(TimeSpan.FromMilliseconds(0)); }; Func <Context, CancellationToken, bool> enabled = (ctx, ct) => { return((bool)ctx["Enabled"]); }; Func <Context, CancellationToken, double> injectionRate = (ctx, ct) => { if (ctx["InjectionRate"] != null) { return((double)ctx["InjectionRate"]); } return(0); }; Boolean executed = false; var policy = MonkeyPolicy.InjectLatency <ResultPrimitive>(with => with.Latency(latencyProvider) .InjectionRate(injectionRate) .EnabledWhen(enabled) ); Func <Context, ResultPrimitive> 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); }
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 <Context, CancellationToken, bool> enabled = (ctx, ct) => { return((bool)ctx["Enabled"]); }; Func <Context, CancellationToken, double> injectionRate = (ctx, ct) => { if (ctx["InjectionRate"] != null) { return((double)ctx["InjectionRate"]); } return(0); }; Func <Context, CancellationToken, TimeSpan> 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 <OperationCanceledException>(); } executed.Should().BeFalse(); _totalTimeSlept.Should().Be(0); }
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 { ["ShouldInjectLatency"] = true, ["Enabled"] = true, ["InjectionRate"] = 0.3 }; Func <Context, CancellationToken, Task <TimeSpan> > latencyProvider = async(ctx, ct) => { if ((bool)ctx["ShouldInjectLatency"]) { return(await Task.FromResult(delay)); } return(await Task.FromResult(TimeSpan.FromMilliseconds(0))); }; Func <Context, CancellationToken, Task <bool> > enabled = async(ctx, ct) => { return(await Task.FromResult((bool)ctx["Enabled"])); }; Func <Context, CancellationToken, Task <double> > 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 <ResultPrimitive>(with => with.Latency(latencyProvider) .InjectionRate(injectionRate) .EnabledWhen(enabled) ); Func <Context, Task <ResultPrimitive> > 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); }
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 <Context, CancellationToken, Exception> fault = (ctx, cts) => { if (ctx["Message"] != null) { Exception ex = new InvalidOperationException(ctx["Message"].ToString()); return(ex); } return(new Exception()); }; Func <Context, CancellationToken, Double> injectionRate = (ctx, ct) => { double rate = 0; if (ctx["InjectionRate"] != null) { rate = (double)ctx["InjectionRate"]; } return(rate); }; Func <Context, CancellationToken, bool> 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 <OperationCanceledException>(); } executed.Should().BeFalse(); }
public void InjectFault_With_Context_Should_not_execute_user_delegate_async_basic() { Boolean executed = false; Context context = new Context(); Func <Context, CancellationToken, Task <Exception> > fault = (ctx, cts) => Task.FromResult(new Exception()); Func <Context, CancellationToken, Task <bool> > enabled = (ctx, ct) => Task.FromResult(true); Func <Context, CancellationToken, Task <Double> > injectionRate = (ctx, ct) => Task.FromResult(0.6); var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); Func <Context, Task> actionAsync = (_) => { executed = true; return(TaskHelper.EmptyTask); }; policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly <Exception>(); executed.Should().BeFalse(); }
public void InjectFault_should_throw_if_injection_rate_is_out_of_range_too_high() { Boolean executed = false; Context context = new Context(); Func <Context, CancellationToken, Task <Exception> > fault = (ctx, cts) => Task.FromResult(new Exception()); Func <Context, CancellationToken, Task <bool> > enabled = (ctx, ct) => Task.FromResult(true); Func <Context, CancellationToken, Task <Double> > injectionRate = (ctx, ct) => Task.FromResult(1.01); var policy = MonkeyPolicy.InjectFaultAsync(fault, injectionRate, enabled); Func <Context, Task> actionAsync = (_) => { executed = true; return(TaskHelper.EmptyTask); }; policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)).ShouldThrowExactly <ArgumentOutOfRangeException>(); executed.Should().BeFalse(); }
public void InjectFault_should_throw_if_injection_rate_is_out_of_range_too_high() { Boolean executed = false; Context context = new Context(); Func <Context, Exception> fault = (ctx) => new Exception(); Func <Context, double> injectionRate = (ctx) => 1.01; Func <Context, bool> enabled = (ctx) => true; var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) .ShouldThrowExactly <ArgumentOutOfRangeException>(); executed.Should().BeFalse(); }
public void Given_enabled_and_randomly_not_within_threshold_should_not_inject_behaviour() { Boolean userDelegateExecuted = false; Boolean injectedBehaviourExecuted = false; var policy = MonkeyPolicy.InjectBehaviour <ResultPrimitive>(with => with.Behaviour(() => { injectedBehaviourExecuted = true; }) .InjectionRate(0.4) .Enabled()); policy.Execute(() => { userDelegateExecuted = true; return(ResultPrimitive.Good); }); userDelegateExecuted.Should().BeTrue(); injectedBehaviourExecuted.Should().BeFalse(); }
public void InjectFault_With_Context_Should_not_execute_user_delegate_with_default_context() { Boolean executed = false; Context context = new Context(); Func <Context, Exception> fault = (ctx) => new Exception(); Func <Context, double> injectionRate = (ctx) => 0.6; Func <Context, bool> enabled = (ctx) => true; var policy = MonkeyPolicy.InjectFault(fault, injectionRate, enabled); policy.Invoking(x => x.Execute((ctx) => { executed = true; }, context)) .ShouldThrowExactly <Exception>(); executed.Should().BeFalse(); }
public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() { var executed = false; var policy = MonkeyPolicy.InjectLatencyAsync(with => with.Latency(TimeSpan.FromMilliseconds(500)) .InjectionRate(0.6) .Enabled(false) ); Func <Task> actionAsync = () => { executed = true; return(TaskHelper.EmptyTask); }; await policy.ExecuteAsync(actionAsync); executed.Should().BeTrue(); _totalTimeSlept.Should().Be(0); }
public void Given_enabled_and_randomly_within_threshold_should_inject_behaviour() { Boolean userDelegateExecuted = false; Boolean injectedBehaviourExecuted = false; var policy = MonkeyPolicy.InjectBehaviour(with => with.Behaviour(() => { injectedBehaviourExecuted = true; }) .InjectionRate(0.6) .Enabled()); policy.Execute(() => { userDelegateExecuted = true; }); userDelegateExecuted.Should().BeTrue(); injectedBehaviourExecuted.Should().BeTrue(); }
public IEnumerable <string> Get() { var isEnabled = true; var fault = new SocketException(errorCode: 10013); var chaosPolicy = MonkeyPolicy.InjectException(with => with.Fault(fault) .InjectionRate(0.5) // Fail 50% of requests //.InjectionRate(1) // Used to simulate circuit breaker .Enabled(isEnabled)); var result = chaosPolicy.Execute(() => GetSomeString()); return(result); }
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 <Context, Task <ResultPrimitive> > actionAsync = (ctx) => { executed = true; return(Task.FromResult(ResultPrimitive.Good)); }; Func <Context, CancellationToken, Task <Exception> > fault = (ctx, cts) => { if (ctx["Message"] != null) { Exception ex = new InvalidOperationException(ctx["Message"].ToString()); return(Task.FromResult(ex)); } return(Task.FromResult(new Exception())); }; Func <Context, Task <Double> > injectionRate = (ctx) => { double rate = 0; if (ctx["InjectionRate"] != null) { rate = (double)ctx["InjectionRate"]; } return(Task.FromResult(rate)); }; Func <Context, Task <bool> > enabled = (ctx) => { return(Task.FromResult((bool)ctx["ShouldFail"])); }; var policy = MonkeyPolicy.InjectFaultAsync <ResultPrimitive>(fault, injectionRate, enabled); policy.Awaiting(async x => await x.ExecuteAsync(actionAsync, context)) .ShouldThrowExactly <InvalidOperationException>(); executed.Should().BeFalse(); }
public void InjectFaultWith_Context_Should_not_execute_user_delegate_full_context() { string failureMessage = "Failure Message"; Boolean executed = false; Context context = new Context { ["ShouldFail"] = true, ["Message"] = failureMessage, ["InjectionRate"] = 0.6 }; Func <Context, ResultPrimitive> action = (ctx) => { executed = true; return(ResultPrimitive.Good); }; Func <Context, CancellationToken, Exception> fault = (ctx, ct) => { if (ctx["Message"] != null) { return(new InvalidOperationException(ctx["Message"].ToString())); } return(new Exception()); }; Func <Context, CancellationToken, double> injectionRate = (ctx, ct) => { if (ctx["InjectionRate"] != null) { return((double)ctx["InjectionRate"]); } return(0); }; Func <Context, CancellationToken, bool> enabled = (ctx, ct) => { return((bool)ctx["ShouldFail"]); }; var policy = MonkeyPolicy.InjectResult <ResultPrimitive>(with => with.Fault(fault) .InjectionRate(injectionRate) .EnabledWhen(enabled) ); policy.Invoking(x => x.Execute(action, context)) .ShouldThrowExactly <InvalidOperationException>(); executed.Should().BeFalse(); }
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(); }
public async Task GetManifestWithLatency() { var chaosPolicy = MonkeyPolicy.InjectLatencyAsync(with => with.Latency(TimeSpan.FromSeconds(10)) .InjectionRate(0.02) .Enabled(true)); var foo = new List <Task <HttpResponseMessage> >(); for (int i = 0; i < 10; i++) { foo.Add(chaosPolicy.ExecuteAsync(() => _client.GetAsync(_psapiUrl))); } await Task.WhenAll(foo); }
public void InjectFaultWith_Context_Should_not_execute_user_delegate_default_context() { Boolean executed = false; Context context = new Context(); Func <Context, ResultPrimitive> action = (ctx) => { executed = true; return(ResultPrimitive.Good); }; Func <Context, Exception> fault = (ctx) => new Exception(); Func <Context, double> injectionRate = (ctx) => 0.6; Func <Context, bool> enabled = (ctx) => true; var policy = MonkeyPolicy.InjectFault <ResultPrimitive>(fault, injectionRate, enabled); policy.Invoking(x => x.Execute(action, context)) .ShouldThrow <Exception>(); executed.Should().BeFalse(); }
public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Dissabled() { var executed = false; var policy = MonkeyPolicy.InjectLatency <ResultPrimitive>(with => with.Latency(TimeSpan.FromMilliseconds(500)) .InjectionRate(0.6) .Enabled(false) ); Func <ResultPrimitive> action = () => { executed = true; return(ResultPrimitive.Good); }; var result = policy.Execute(action); executed.Should().BeTrue(); result.Should().Be(ResultPrimitive.Good); _totalTimeSlept.Should().Be(0); }
public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_async() { Exception fault = new Exception(); var policy = MonkeyPolicy.InjectExceptionAsync(with => with.Fault(fault) .InjectionRate(0.3) .Enabled() ); Boolean executed = false; Func <Task> actionAsync = () => { executed = true; return(TaskHelper.EmptyTask); }; policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)).ShouldNotThrow <Exception>(); executed.Should().BeTrue(); }
public void InjectFaultContext_Free_Should_Not_Return_Fault() { Boolean executed = false; Func <ResultPrimitive> action = () => { executed = true; return(ResultPrimitive.Good); }; ResultPrimitive fault = ResultPrimitive.Fault; var policy = MonkeyPolicy.InjectResult <ResultPrimitive>(with => with.Result(fault) .InjectionRate(0.4) .Enabled() ); ResultPrimitive response = policy.Execute(action); response.Should().Be(ResultPrimitive.Good); executed.Should().BeTrue(); }
public void InjectFault_Context_Free_Enabled_Should_not_execute_user_delegate_async() { string exceptionMessage = "exceptionMessage"; Exception fault = new Exception(exceptionMessage); var policy = MonkeyPolicy.InjectExceptionAsync(with => with.Fault(fault) .InjectionRate(0.6) .Enabled() ); Boolean executed = false; Func <Task> actionAsync = () => { executed = true; return(TaskHelper.EmptyTask); }; policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)).ShouldThrowExactly <Exception>().WithMessage(exceptionMessage); executed.Should().BeFalse(); }
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(); }
public async Task InjectFault_Context_Free_Should_Not_Return_Fault_async() { Boolean executed = false; Func <Task <ResultPrimitive> > actionAsync = () => { executed = true; return(Task.FromResult(ResultPrimitive.Good)); }; ResultPrimitive fault = ResultPrimitive.Fault; var policy = MonkeyPolicy.InjectFaultAsync <ResultPrimitive>(fault, 0.4, () => true); ResultPrimitive response = await policy.ExecuteAsync(actionAsync); response.Should().Be(ResultPrimitive.Good); executed.Should().BeTrue(); }
public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_async() { string exceptionMessage = "exceptionMessage"; Exception fault = new Exception(exceptionMessage); Boolean executed = false; Func <Task <ResultPrimitive> > actionAsync = () => { executed = true; return(Task.FromResult(ResultPrimitive.Good)); }; var policy = MonkeyPolicy.InjectResultAsync <ResultPrimitive>(with => with.Fault <ResultPrimitive>(fault) .InjectionRate(0.3) .Enabled() ); policy.Awaiting(async x => await x.ExecuteAsync(actionAsync)) .ShouldNotThrow <Exception>(); executed.Should().BeTrue(); }
public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate() { Exception fault = new Exception("test"); var policy = MonkeyPolicy.InjectException(with => with.Fault(fault) .InjectionRate(0.3) .Enabled() ); Boolean executed = false; policy.Invoking(x => x.Execute(() => { executed = true; })) .ShouldNotThrow <Exception>(); executed.Should().BeTrue(); }
public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_not_throw_if_injected_fault_is_permitted_null() { Exception fault = null; var policy = MonkeyPolicy.InjectException(with => with.Fault(fault) .InjectionRate(0.6) .Enabled() ); Boolean executed = false; policy.Invoking(x => x.Execute(() => { executed = true; })) .ShouldNotThrow <Exception>(); executed.Should().BeTrue(); }