public void InjectFaultWith_Context_InjectionRate_Should_Not_Return_Fault() { Boolean executed = false; Context context = new Context(); context["InjectionRate"] = 0.4; Func <Context, ResultPrimitive> action = (ctx) => { executed = true; return(ResultPrimitive.Good); }; Func <Context, ResultPrimitive> fault = (ctx) => { return(ResultPrimitive.Fault); }; Func <Context, Double> injectionRate = (ctx) => { if (ctx["InjectionRate"] != null) { return((double)ctx["InjectionRate"]); } return(0); }; Func <Context, bool> enabled = (ctx) => { return(true); }; var policy = MonkeyPolicy.InjectFault <ResultPrimitive>(fault, injectionRate, enabled); ResultPrimitive response = policy.Execute(action, context); response.Should().Be(ResultPrimitive.Good); executed.Should().BeTrue(); }
public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value__default_for_value_type() { ResultPrimitive valueToReturnFromCache = default; ResultPrimitive valueToReturnFromExecution = ResultPrimitive.Good; valueToReturnFromExecution.Should().NotBe(valueToReturnFromCache); const string operationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync <ResultPrimitive>(stubCacheProvider, TimeSpan.MaxValue); await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false).ConfigureAwait(false); bool delegateExecuted = false; (await cache.ExecuteAsync(async ctx => { delegateExecuted = true; await TaskHelper.EmptyTask.ConfigureAwait(false); return(valueToReturnFromExecution); }, new Context(operationKey)) .ConfigureAwait(false)) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); }
public async Task InjectFault_With_Context_Enabled_Should_Not_Return_Fault_async() { Boolean executed = false; Context context = new Context { ["ShouldFail"] = false }; Func <Context, Task <ResultPrimitive> > actionAsync = (ctx) => { executed = true; return(Task.FromResult(ResultPrimitive.Good)); }; ResultPrimitive fault = ResultPrimitive.Fault; Func <Context, CancellationToken, Task <bool> > enabled = (ctx, ct) => { return(Task.FromResult((bool)ctx["ShouldFail"])); }; var policy = MonkeyPolicy.InjectResultAsync <ResultPrimitive>(with => with.Result(fault) .InjectionRate(0.6) .EnabledWhen(enabled) ); ResultPrimitive response = await policy.ExecuteAsync(actionAsync, context); response.Should().Be(ResultPrimitive.Good); executed.Should().BeTrue(); }
public void Should_handle_exception_when_TResult_policy_handling_exceptions_only() { Policy <ResultPrimitive> policy = Policy <ResultPrimitive> .Handle <DivideByZeroException>().Retry(1); ResultPrimitive result = policy.RaiseResultAndOrExceptionSequence(new DivideByZeroException(), ResultPrimitive.Good); result.Should().Be(ResultPrimitive.Good); }
public void Should_not_return_handled_result_when_handled_result_raised_less_number_of_times_than_retry_count() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .Retry(3); ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.Good); }
public async Task Should_return_result_when_result_is_not_the_specified_handled_result() { var policy = Policy .HandleResult(ResultPrimitive.Fault) .RetryAsync(); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.FaultAgain, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.FaultAgain); }
public async Task Should_not_return_handled_result_when_handled_result_raised_same_number_of_times_as_retry_count() { var policy = Policy .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good).ConfigureAwait(false); result.Should().Be(ResultPrimitive.Good); }
public async Task Should_return_handled_result_when_handled_result_raised_more_times_then_retry_count() { var policy = Policy .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good).ConfigureAwait(false); result.Should().Be(ResultPrimitive.Fault); // It should give up retrying after 3 retries and return the last failure, so should return Fault, not Good. }
public void Should_return_handled_result_when_handled_result_raised_more_times_then_retry_count() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .Retry(3); ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.Fault); // It should give up retrying after 3 retries and return the last failure, so should return Fault, not Good. }
public void Should_return_result_when_result_is_not_the_specified_handled_result() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .Retry(); ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.FaultAgain, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.FaultAgain); }
public async void Should_return_result_when_result_is_not_the_specified_handled_result() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .RetryAsync(); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.FaultAgain, ResultPrimitive.Good).ConfigureAwait(false); result.Should().Be(ResultPrimitive.FaultAgain); }
public async void Should_not_return_handled_result_when_handled_result_raised_less_number_of_times_than_retry_count() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.Fault, ResultPrimitive.Good).ConfigureAwait(false); result.Should().Be(ResultPrimitive.Good); }
public async Task Should_return_result_when_result_is_not_one_of_the_specified_handled_results() { var policy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) .RetryAsync(); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.FaultYetAgain, ResultPrimitive.Good).ConfigureAwait(false); result.Should().Be(ResultPrimitive.FaultYetAgain); }
public async Task Should_return_handled_result_when_one_of_the_handled_results_is_raised_more_times_then_retry_count() { var policy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) .RetryAsync(3); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.FaultAgain, ResultPrimitive.FaultAgain, ResultPrimitive.FaultAgain, ResultPrimitive.FaultAgain, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.FaultAgain); }
public void Should_handle_both_exception_and_specified_result_if_raised_same_number_of_times_as_retry_count__when_configuring_exception_before_result() { Policy <ResultPrimitive> policy = Policy .Handle <DivideByZeroException>() .OrResult(ResultPrimitive.Fault) .Retry(2); ResultPrimitive result = policy.RaiseResultAndOrExceptionSequence(ResultPrimitive.Fault, new DivideByZeroException(), ResultPrimitive.Good); result.Should().Be(ResultPrimitive.Good); }
public void Should_return_unhandled_result_if_not_one_of_results_or_exceptions_specified() { Policy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .Or <DivideByZeroException>() .Retry(2); ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.FaultAgain); result.Should().Be(ResultPrimitive.FaultAgain); }
public async void Should_return_handled_result_when_one_of_the_handled_results_is_raised_more_times_then_retry_count() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) .RetryAsync(3); ResultPrimitive result = await policy.RaiseResultSequenceAsync(ResultPrimitive.FaultAgain, ResultPrimitive.FaultAgain, ResultPrimitive.FaultAgain, ResultPrimitive.FaultAgain, ResultPrimitive.Good).ConfigureAwait(false); result.Should().Be(ResultPrimitive.FaultAgain); }
public void Should_not_return_handled_result_when_one_of_the_handled_results_raised_same_number_of_times_as_retry_count() { RetryPolicy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) .Retry(3); ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.FaultAgain, ResultPrimitive.Fault, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.Good); }
public void Should_not_throw_when_timeout_is_greater_than_execution_duration__pessimistic() { var policy = Policy.TimeoutAsync <ResultPrimitive>(TimeSpan.FromSeconds(1), TimeoutStrategy.Pessimistic); ResultPrimitive result = ResultPrimitive.Undefined; Func <Task> act = async() => result = await policy.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good)); act.Should().NotThrow(); result.Should().Be(ResultPrimitive.Good); }
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.InjectFault <ResultPrimitive>(fault, 0.4, () => true); ResultPrimitive response = policy.Execute(action); response.Should().Be(ResultPrimitive.Good); executed.Should().BeTrue(); }
public void Should_handle_both_exceptions_and_specified_results_if_raised_same_number_of_times_as_retry_count__mixing_exceptions_and_results_specifying_results_first() { Policy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .Or <DivideByZeroException>() .OrResult(ResultPrimitive.FaultAgain) .Or <ArgumentException>() .Retry(4); ResultPrimitive result = policy.RaiseResultAndOrExceptionSequence(ResultPrimitive.Fault, new DivideByZeroException(), new ArgumentException(), ResultPrimitive.FaultAgain, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.Good); }
public void Should_return_handled_result_when_handled_result_returned_next_after_retries_exhaust_handling_both_exceptions_and_specified_results__mixing_exceptions_and_results_specifying_results_first() { Policy <ResultPrimitive> policy = Policy .HandleResult(ResultPrimitive.Fault) .Or <DivideByZeroException>() .OrResult(ResultPrimitive.FaultAgain) .Or <ArgumentException>() .Retry(3); ResultPrimitive result = policy.RaiseResultAndOrExceptionSequence(ResultPrimitive.Fault, new DivideByZeroException(), new ArgumentException(), ResultPrimitive.FaultAgain, ResultPrimitive.Good); result.Should().Be(ResultPrimitive.FaultAgain); }
public void Should_not_throw_when_timeout_is_greater_than_execution_duration__optimistic() { var policy = Policy.TimeoutAsync <ResultPrimitive>(TimeSpan.FromSeconds(1), TimeoutStrategy.Optimistic); ResultPrimitive result = ResultPrimitive.Undefined; var userCancellationToken = CancellationToken.None; Func <Task> act = async() => result = await policy.ExecuteAsync(ct => Task.FromResult(ResultPrimitive.Good), userCancellationToken) .ConfigureAwait(false); act.ShouldNotThrow(); result.Should().Be(ResultPrimitive.Good); }
public void Should_not_throw_when_timeout_is_greater_than_execution_duration__pessimistic() { var policy = Policy.Timeout <ResultPrimitive>(TimeSpan.FromSeconds(1), TimeoutStrategy.Pessimistic); ResultPrimitive result = ResultPrimitive.Undefined; policy.Invoking(p => { result = p.Execute(() => ResultPrimitive.Good); }).ShouldNotThrow(); result.Should().Be(ResultPrimitive.Good); }
public void Should_execute_delegate_and_put_value_in_cache_for_non_nullable_types_if_cache_does_not_hold_value() { const ResultPrimitive valueToReturn = ResultPrimitive.Substitute; const string operationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); stubCacheProvider.Get(operationKey).Should().BeNull(); cache.Execute(ctx => { return(valueToReturn); }, new Context(operationKey)).Should().Be(valueToReturn); stubCacheProvider.Get(operationKey).Should().Be(valueToReturn); }
public void Should_not_throw_when_timeout_is_greater_than_execution_duration__optimistic() { var policy = Policy.Timeout <ResultPrimitive>(TimeSpan.FromSeconds(1), TimeoutStrategy.Optimistic); var userCancellationToken = CancellationToken.None; ResultPrimitive result = ResultPrimitive.Undefined; policy.Invoking(p => { result = p.Execute(ct => ResultPrimitive.Good, userCancellationToken); }).ShouldNotThrow(); result.Should().Be(ResultPrimitive.Good); }
public void Should_not_error_for_executions_on_non_nullable_types_if_cache_does_not_hold_value() { const string operationKey = "SomeOperationKey"; bool onErrorCalled = false; Action <Context, string, Exception> onError = (ctx, key, exc) => { onErrorCalled = true; }; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue, onError); stubCacheProvider.Get(operationKey).Should().BeNull(); ResultPrimitive result = cache.Execute(ctx => ResultPrimitive.Substitute, new Context(operationKey)); onErrorCalled.Should().BeFalse(); }
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 async Task Double_generic_SerializingCacheProvider_should_not_serialize_on_put_for_defaultTResult() { bool serializeInvoked = false; StubSerializer <ResultPrimitive, StubSerialized <ResultPrimitive> > stubTResultSerializer = new StubSerializer <ResultPrimitive, StubSerialized <ResultPrimitive> >( serialize: o => { serializeInvoked = true; return(new StubSerialized <ResultPrimitive>(o)); }, deserialize: s => s.Original ); StubCacheProvider stubCacheProvider = new StubCacheProvider(); ResultPrimitive objectToCache = default(ResultPrimitive); string key = "some key"; SerializingCacheProviderAsync <ResultPrimitive, StubSerialized <ResultPrimitive> > serializingCacheProvider = new SerializingCacheProviderAsync <ResultPrimitive, StubSerialized <ResultPrimitive> >(stubCacheProvider.AsyncFor <StubSerialized <ResultPrimitive> >(), stubTResultSerializer); await serializingCacheProvider.PutAsync(key, objectToCache, new Ttl(TimeSpan.FromMinutes(1)), CancellationToken.None, false); serializeInvoked.Should().Be(false); stubCacheProvider.Get(key).Should().BeNull(); }
public void InjectFaultContext_Free_Should_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.6) .Enabled() ); ResultPrimitive response = policy.Execute(action); response.Should().Be(ResultPrimitive.Fault); executed.Should().BeFalse(); }
public ResultClass(ResultPrimitive resultCode, string someString) : this(resultCode) { SomeString = someString; }
public ResultClass(ResultPrimitive resultCode) { ResultCode = resultCode; }