From 8a8f3d8fa167f3c6da22bd6c93e9c8f7d5e4226d Mon Sep 17 00:00:00 2001 From: sukreshmanda <34400639+sukreshmanda@users.noreply.github.com> Date: Tue, 30 Jul 2024 22:10:53 +0530 Subject: [PATCH] Fix S4225 warning (#2253) Refactor specs to avoid S4225 warning. --- test/Polly.Core.Tests/Retry/RetryHelperTests.cs | 2 ++ test/Polly.Specs/Caching/CacheAsyncSpecs.cs | 8 ++++---- test/Polly.Specs/Caching/CacheSpecs.cs | 8 ++++---- test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs | 8 ++++---- test/Polly.Specs/Caching/CacheTResultSpecs.cs | 8 ++++---- .../AdvancedCircuitBreakerAsyncSpecs.cs | 10 +++++----- .../CircuitBreaker/AdvancedCircuitBreakerSpecs.cs | 10 +++++----- .../CircuitBreaker/CircuitBreakerAsyncSpecs.cs | 10 +++++----- .../CircuitBreaker/CircuitBreakerSpecs.cs | 10 +++++----- .../CircuitBreakerTResultAsyncSpecs.cs | 10 +++++----- .../CircuitBreaker/CircuitBreakerTResultSpecs.cs | 10 +++++----- test/Polly.Specs/ContextSpecs.cs | 2 +- test/Polly.Specs/DictionaryHelpers.cs | 10 ++++++++++ test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs | 12 ++++++------ test/Polly.Specs/Fallback/FallbackSpecs.cs | 12 ++++++------ .../Fallback/FallbackTResultAsyncSpecs.cs | 12 ++++++------ test/Polly.Specs/Fallback/FallbackTResultSpecs.cs | 12 ++++++------ test/Polly.Specs/Helpers/ObjectExtensions.cs | 11 ----------- test/Polly.Specs/Polly.Specs.csproj | 1 + test/Polly.Specs/Retry/RetryAsyncSpecs.cs | 12 ++++++------ test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs | 6 +++--- test/Polly.Specs/Retry/RetryForeverSpecs.cs | 6 +++--- test/Polly.Specs/Retry/RetrySpecs.cs | 12 ++++++------ test/Polly.Specs/Retry/RetryTResultAsyncSpecs.cs | 14 +++++++------- test/Polly.Specs/Retry/RetryTResultSpecs.cs | 14 +++++++------- test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs | 8 ++++---- .../Retry/WaitAndRetryForeverAsyncSpecs.cs | 6 +++--- test/Polly.Specs/Retry/WaitAndRetryForeverSpecs.cs | 6 +++--- test/Polly.Specs/Retry/WaitAndRetrySpecs.cs | 8 ++++---- 29 files changed, 130 insertions(+), 128 deletions(-) create mode 100644 test/Polly.Specs/DictionaryHelpers.cs delete mode 100644 test/Polly.Specs/Helpers/ObjectExtensions.cs diff --git a/test/Polly.Core.Tests/Retry/RetryHelperTests.cs b/test/Polly.Core.Tests/Retry/RetryHelperTests.cs index be770eca1e9..822d62dcc28 100644 --- a/test/Polly.Core.Tests/Retry/RetryHelperTests.cs +++ b/test/Polly.Core.Tests/Retry/RetryHelperTests.cs @@ -11,6 +11,7 @@ public class RetryHelperTests public static TheoryData Attempts() { #pragma warning disable IDE0028 +#pragma warning disable IDE0022 // Use expression body for method return new() { 1, @@ -23,6 +24,7 @@ public static TheoryData Attempts() 1_024, 1_025, }; +#pragma warning restore IDE0022 // Use expression body for method #pragma warning restore IDE0028 } diff --git a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs index 27cf84dc8e5..5ff243e7998 100644 --- a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs @@ -468,10 +468,10 @@ public async Task Should_allow_custom_FuncCacheKeyStrategy() bool funcExecuted = false; Func> func = async _ => { funcExecuted = true; await TaskHelper.EmptyTask; return new object(); }; - (await cache.ExecuteAsync(func, new Context("person", new { id = "1" }.AsDictionary()))).Should().BeSameAs(person1); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "1")))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - (await cache.ExecuteAsync(func, new Context("person", new { id = "2" }.AsDictionary()))).Should().BeSameAs(person2); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "2")))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } @@ -493,10 +493,10 @@ public async Task Should_allow_custom_ICacheKeyStrategy() bool funcExecuted = false; Func> func = async _ => { funcExecuted = true; await TaskHelper.EmptyTask; return new object(); }; - (await cache.ExecuteAsync(func, new Context("person", new { id = "1" }.AsDictionary()))).Should().BeSameAs(person1); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "1")))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - (await cache.ExecuteAsync(func, new Context("person", new { id = "2" }.AsDictionary()))).Should().BeSameAs(person2); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "2")))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } diff --git a/test/Polly.Specs/Caching/CacheSpecs.cs b/test/Polly.Specs/Caching/CacheSpecs.cs index 11ce7deca32..9392cc2d51f 100644 --- a/test/Polly.Specs/Caching/CacheSpecs.cs +++ b/test/Polly.Specs/Caching/CacheSpecs.cs @@ -467,10 +467,10 @@ public void Should_allow_custom_FuncCacheKeyStrategy() bool funcExecuted = false; Func func = _ => { funcExecuted = true; return new object(); }; - cache.Execute(func, new Context("person", new { id = "1" }.AsDictionary())).Should().BeSameAs(person1); + cache.Execute(func, new Context("person", CreateDictionary("id", "1"))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - cache.Execute(func, new Context("person", new { id = "2" }.AsDictionary())).Should().BeSameAs(person2); + cache.Execute(func, new Context("person", CreateDictionary("id", "2"))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } @@ -492,10 +492,10 @@ public void Should_allow_custom_ICacheKeyStrategy() bool funcExecuted = false; Func func = _ => { funcExecuted = true; return new object(); }; - cache.Execute(func, new Context("person", new { id = "1" }.AsDictionary())).Should().BeSameAs(person1); + cache.Execute(func, new Context("person", CreateDictionary("id", "1"))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - cache.Execute(func, new Context("person", new { id = "2" }.AsDictionary())).Should().BeSameAs(person2); + cache.Execute(func, new Context("person", CreateDictionary("id", "2"))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } diff --git a/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs index 02efec46d8c..675642fd96e 100644 --- a/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs @@ -303,10 +303,10 @@ public async Task Should_allow_custom_FuncCacheKeyStrategy() bool funcExecuted = false; Func> func = async _ => { funcExecuted = true; await TaskHelper.EmptyTask; return new ResultClass(ResultPrimitive.Fault, "should never return this one"); }; - (await cache.ExecuteAsync(func, new Context("person", new { id = "1" }.AsDictionary()))).Should().BeSameAs(person1); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "1")))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - (await cache.ExecuteAsync(func, new Context("person", new { id = "2" }.AsDictionary()))).Should().BeSameAs(person2); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "2")))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } @@ -329,10 +329,10 @@ public async Task Should_allow_custom_ICacheKeyStrategy() bool funcExecuted = false; Func> func = async _ => { funcExecuted = true; await TaskHelper.EmptyTask; return new ResultClass(ResultPrimitive.Fault, "should never return this one"); }; - (await cache.ExecuteAsync(func, new Context("person", new { id = "1" }.AsDictionary()))).Should().BeSameAs(person1); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "1")))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - (await cache.ExecuteAsync(func, new Context("person", new { id = "2" }.AsDictionary()))).Should().BeSameAs(person2); + (await cache.ExecuteAsync(func, new Context("person", CreateDictionary("id", "2")))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } diff --git a/test/Polly.Specs/Caching/CacheTResultSpecs.cs b/test/Polly.Specs/Caching/CacheTResultSpecs.cs index ba6c8411b8e..ae7cced1ae3 100644 --- a/test/Polly.Specs/Caching/CacheTResultSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultSpecs.cs @@ -1240,10 +1240,10 @@ public void Should_allow_custom_FuncICacheKeyStrategy() bool funcExecuted = false; Func func = _ => { funcExecuted = true; return new ResultClass(ResultPrimitive.Fault, "should never return this one"); }; - cache.Execute(func, new Context("person", new { id = "1" }.AsDictionary())).Should().BeSameAs(person1); + cache.Execute(func, new Context("person", CreateDictionary("id", "1"))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - cache.Execute(func, new Context("person", new { id = "2" }.AsDictionary())).Should().BeSameAs(person2); + cache.Execute(func, new Context("person", CreateDictionary("id", "2"))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } @@ -1265,10 +1265,10 @@ public void Should_allow_custom_ICacheKeyStrategy() bool funcExecuted = false; Func func = _ => { funcExecuted = true; return new ResultClass(ResultPrimitive.Fault, "should never return this one"); }; - cache.Execute(func, new Context("person", new { id = "1" }.AsDictionary())).Should().BeSameAs(person1); + cache.Execute(func, new Context("person", CreateDictionary("id", "1"))).Should().BeSameAs(person1); funcExecuted.Should().BeFalse(); - cache.Execute(func, new Context("person", new { id = "2" }.AsDictionary())).Should().BeSameAs(person2); + cache.Execute(func, new Context("person", CreateDictionary("id", "2"))).Should().BeSameAs(person2); funcExecuted.Should().BeFalse(); } diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs index 096929a761f..a44849d0904 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs @@ -2692,7 +2692,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) breaker.CircuitState.Should().Be(CircuitState.Closed); await breaker.Awaiting(x => x.RaiseExceptionAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().ThrowAsync(); + CreateDictionary("key1", "value1", "key2", "value2"))).Should().ThrowAsync(); breaker.CircuitState.Should().Be(CircuitState.Open); contextData.Should() @@ -2745,7 +2745,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration should invoke onReset, with context - await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, new { key1 = "value1", key2 = "value2" }.AsDictionary()); + await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, CreateDictionary("key1", "value1", "key2", "value2")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextData.Should() @@ -2756,7 +2756,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) [Fact] public async Task Context_should_be_empty_if_execute_not_called_with_any_context_data() { - IDictionary contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary(); + IDictionary contextData = CreateDictionary("key1", "value1", "key2", "value2"); Action onBreak = (_, _, context) => { contextData = context; }; Action onReset = _ => { }; @@ -2833,7 +2833,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().ThrowAsync(); breaker.CircuitState.Should().Be(CircuitState.Closed); - await breaker.Awaiting(x => x.RaiseExceptionAsync(new { key = "original_value" }.AsDictionary())) + await breaker.Awaiting(x => x.RaiseExceptionAsync(CreateDictionary("key", "original_value"))) .Should().ThrowAsync(); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -2846,7 +2846,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync(new { k // but not yet reset // first call after duration is successful, so circuit should reset - await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, new { key = "new_value" }.AsDictionary()); + await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, CreateDictionary("key", "new_value")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextValue.Should().Be("new_value"); diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs index 5876e978bf2..db3ddcf8c1c 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs @@ -2531,7 +2531,7 @@ public void Should_call_onbreak_with_the_passed_context() breaker.CircuitState.Should().Be(CircuitState.Closed); breaker.Invoking(x => x.RaiseException( - new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().Throw(); + CreateDictionary("key1", "value1", "key2", "value2"))).Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); contextData.Should() @@ -2584,7 +2584,7 @@ public void Should_call_onreset_with_the_passed_context() breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration should invoke onReset, with context - breaker.Execute(_ => { }, new { key1 = "value1", key2 = "value2" }.AsDictionary()); + breaker.Execute(_ => { }, CreateDictionary("key1", "value1", "key2", "value2")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextData.Should() @@ -2595,7 +2595,7 @@ public void Should_call_onreset_with_the_passed_context() [Fact] public void Context_should_be_empty_if_execute_not_called_with_any_context_data() { - IDictionary contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary(); + IDictionary contextData = CreateDictionary("key1", "value1", "key2", "value2"); Action onBreak = (_, _, context) => { contextData = context; }; Action onReset = _ => { }; @@ -2672,7 +2672,7 @@ public void Should_create_new_context_for_each_call_to_execute() .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Invoking(x => x.RaiseException(new { key = "original_value" }.AsDictionary())) + breaker.Invoking(x => x.RaiseException(CreateDictionary("key", "original_value"))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -2685,7 +2685,7 @@ public void Should_create_new_context_for_each_call_to_execute() // but not yet reset // first call after duration is successful, so circuit should reset - breaker.Execute(_ => { }, new { key = "new_value" }.AsDictionary()); + breaker.Execute(_ => { }, CreateDictionary("key", "new_value")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs index 34ca095abd0..8eb173ec0ef 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs @@ -1230,7 +1230,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().ThrowAsync(); await breaker.Awaiting(x => x.RaiseExceptionAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().ThrowAsync(); + CreateDictionary("key1", "value1", "key2", "value2"))).Should().ThrowAsync(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1266,7 +1266,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration should invoke onReset, with context - await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, new { key1 = "value1", key2 = "value2" }.AsDictionary()); + await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -1276,7 +1276,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) [Fact] public async Task Context_should_be_empty_if_execute_not_called_with_any_context_data() { - IDictionary contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary(); + IDictionary contextData = CreateDictionary("key1", "value1", "key2", "value2"); Action onBreak = (_, _, context) => { contextData = context; }; Action onReset = _ => { }; @@ -1317,7 +1317,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().ThrowAsync(); // 2 exception raised, circuit is now open - await breaker.Awaiting(x => x.RaiseExceptionAsync(new { key = "original_value" }.AsDictionary())) + await breaker.Awaiting(x => x.RaiseExceptionAsync(CreateDictionary("key", "original_value"))) .Should().ThrowAsync(); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -1330,7 +1330,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync(new { k // but not yet reset // first call after duration is successful, so circuit should reset - await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, new { key = "new_value" }.AsDictionary()); + await breaker.ExecuteAsync(_ => TaskHelper.EmptyTask, CreateDictionary("key", "new_value")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs index a6cd48f229d..f109dab089f 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs @@ -1224,7 +1224,7 @@ public void Should_call_onbreak_with_the_passed_context() .Should().Throw(); breaker.Invoking(x => x.RaiseException( - new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().Throw(); + CreateDictionary("key1", "value1", "key2", "value2"))).Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1260,7 +1260,7 @@ public void Should_call_onreset_with_the_passed_context() breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration should invoke onReset, with context - breaker.Execute(_ => { }, new { key1 = "value1", key2 = "value2" }.AsDictionary()); + breaker.Execute(_ => { }, CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -1270,7 +1270,7 @@ public void Should_call_onreset_with_the_passed_context() [Fact] public void Context_should_be_empty_if_execute_not_called_with_any_context_data() { - IDictionary contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary(); + IDictionary contextData = CreateDictionary("key1", "value1", "key2", "value2"); Action onBreak = (_, _, context) => { contextData = context; }; Action onReset = _ => { }; @@ -1311,7 +1311,7 @@ public void Should_create_new_context_for_each_call_to_execute() .Should().Throw(); // 2 exception raised, circuit is now open - breaker.Invoking(x => x.RaiseException(new { key = "original_value" }.AsDictionary())) + breaker.Invoking(x => x.RaiseException(CreateDictionary("key", "original_value"))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -1324,7 +1324,7 @@ public void Should_create_new_context_for_each_call_to_execute() // but not yet reset // first call after duration is successful, so circuit should reset - breaker.Execute(_ => { }, new { key = "new_value" }.AsDictionary()); + breaker.Execute(_ => { }, CreateDictionary("key", "new_value")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs index 1637b10e999..c48f9ffd756 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs @@ -1253,7 +1253,7 @@ public async Task Should_call_onbreak_with_the_passed_context() (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); - (await breaker.RaiseResultSequenceAsync(new { key1 = "value1", key2 = "value2" }.AsDictionary(), + (await breaker.RaiseResultSequenceAsync(CreateDictionary("key1", "value1", "key2", "value2"), ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); @@ -1291,7 +1291,7 @@ public async Task Should_call_onreset_with_the_passed_context() breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration should invoke onReset, with context - await breaker.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Good), new { key1 = "value1", key2 = "value2" }.AsDictionary()); + await breaker.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Good), CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -1301,7 +1301,7 @@ public async Task Should_call_onreset_with_the_passed_context() [Fact] public async Task Context_should_be_empty_if_execute_not_called_with_any_context_data() { - IDictionary contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary(); + IDictionary contextData = CreateDictionary("key1", "value1", "key2", "value2"); Action, TimeSpan, Context> onBreak = (_, _, context) => { contextData = context; }; Action onReset = _ => { }; @@ -1342,7 +1342,7 @@ public async Task Should_create_new_context_for_each_call_to_execute() .Should().Be(ResultPrimitive.Fault); // 2 exception or fault raised, circuit is now open - (await breaker.RaiseResultSequenceAsync(new { key = "original_value" }.AsDictionary(), ResultPrimitive.Fault)) + (await breaker.RaiseResultSequenceAsync(CreateDictionary("key", "original_value"), ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -1355,7 +1355,7 @@ public async Task Should_create_new_context_for_each_call_to_execute() // but not yet reset // first call after duration is successful, so circuit should reset - await breaker.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Good), new { key = "new_value" }.AsDictionary()); + await breaker.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Good), CreateDictionary("key", "new_value")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs index caf19baf7ee..0464f4ff57d 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs @@ -1232,7 +1232,7 @@ public void Should_call_onbreak_with_the_passed_context() breaker.RaiseResultSequence(ResultPrimitive.Fault) .Should().Be(ResultPrimitive.Fault); - breaker.RaiseResultSequence(new { key1 = "value1", key2 = "value2" }.AsDictionary(), + breaker.RaiseResultSequence(CreateDictionary("key1", "value1", "key2", "value2"), ResultPrimitive.Fault) .Should().Be(ResultPrimitive.Fault); @@ -1270,7 +1270,7 @@ public void Should_call_onreset_with_the_passed_context() breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration should invoke onReset, with context - breaker.Execute(_ => ResultPrimitive.Good, new { key1 = "value1", key2 = "value2" }.AsDictionary()); + breaker.Execute(_ => ResultPrimitive.Good, CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -1280,7 +1280,7 @@ public void Should_call_onreset_with_the_passed_context() [Fact] public void Context_should_be_empty_if_execute_not_called_with_any_context_data() { - IDictionary contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary(); + IDictionary contextData = CreateDictionary("key1", "value1", "key2", "value2"); Action, TimeSpan, Context> onBreak = (_, _, context) => { contextData = context; }; Action onReset = _ => { }; @@ -1321,7 +1321,7 @@ public void Should_create_new_context_for_each_call_to_execute() .Should().Be(ResultPrimitive.Fault); // 2 exception raised, circuit is now open - breaker.RaiseResultSequence(new { key = "original_value" }.AsDictionary(), ResultPrimitive.Fault) + breaker.RaiseResultSequence(CreateDictionary("key", "original_value"), ResultPrimitive.Fault) .Should().Be(ResultPrimitive.Fault); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -1334,7 +1334,7 @@ public void Should_create_new_context_for_each_call_to_execute() // but not yet reset // first call after duration is successful, so circuit should reset - breaker.Execute(_ => ResultPrimitive.Good, new { key = "new_value" }.AsDictionary()); + breaker.Execute(_ => ResultPrimitive.Good, CreateDictionary("key", "new_value")); breaker.CircuitState.Should().Be(CircuitState.Closed); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/ContextSpecs.cs b/test/Polly.Specs/ContextSpecs.cs index 4c3ec4f8093..d9cb105c949 100644 --- a/test/Polly.Specs/ContextSpecs.cs +++ b/test/Polly.Specs/ContextSpecs.cs @@ -15,7 +15,7 @@ public void Should_assign_OperationKey_from_constructor() [Fact] public void Should_assign_OperationKey_and_context_data_from_constructor() { - Context context = new Context("SomeKey", new { key1 = "value1", key2 = "value2" }.AsDictionary()); + Context context = new Context("SomeKey", CreateDictionary("key1", "value1", "key2", "value2")); context.OperationKey.Should().Be("SomeKey"); context["key1"].Should().Be("value1"); diff --git a/test/Polly.Specs/DictionaryHelpers.cs b/test/Polly.Specs/DictionaryHelpers.cs new file mode 100644 index 00000000000..a8ab552dfae --- /dev/null +++ b/test/Polly.Specs/DictionaryHelpers.cs @@ -0,0 +1,10 @@ +namespace Polly.Specs; + +public static class DictionaryHelpers +{ + public static Dictionary CreateDictionary(TKey key, object value) + where TKey : notnull => new() { [key] = value }; + + public static Dictionary CreateDictionary(TKey key1, object value1, TKey key2, object value2) + where TKey : notnull => new() { [key1] = value1, [key2] = value2 }; +} diff --git a/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs b/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs index 9d6f4aea81f..df724354f8d 100644 --- a/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs @@ -309,7 +309,7 @@ public async Task Should_call_onFallback_with_the_passed_context() .FallbackAsync(fallbackActionAsync, onFallbackAsync); await fallbackPolicy.Awaiting(p => p.ExecuteAsync(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrowAsync(); contextData.Should() @@ -331,7 +331,7 @@ public async Task Should_call_onFallback_with_the_passed_context_when_execute_an .FallbackAsync(fallbackActionAsync, onFallbackAsync); await fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrowAsync(); contextData.Should() @@ -353,10 +353,10 @@ public async Task Should_call_onFallback_with_independent_context_for_independen .Or() .FallbackAsync(fallbackActionAsync, onFallbackAsync); - await fallbackPolicy.Awaiting(p => p.ExecuteAsync(_ => throw new ArgumentNullException(), new { key = "value1" }.AsDictionary())) + await fallbackPolicy.Awaiting(p => p.ExecuteAsync(_ => throw new ArgumentNullException(), CreateDictionary("key", "value1"))) .Should().NotThrowAsync(); - await fallbackPolicy.Awaiting(p => p.ExecuteAsync(_ => throw new DivideByZeroException(), new { key = "value2" }.AsDictionary())) + await fallbackPolicy.Awaiting(p => p.ExecuteAsync(_ => throw new DivideByZeroException(), CreateDictionary("key", "value2"))) .Should().NotThrowAsync(); contextData.Count.Should().Be(2); @@ -401,7 +401,7 @@ public async Task Should_call_fallbackAction_with_the_passed_context() .FallbackAsync(fallbackActionAsync, onFallbackAsync); await fallbackPolicy.Awaiting(p => p.ExecuteAsync(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrowAsync(); contextData.Should() @@ -423,7 +423,7 @@ public async Task Should_call_fallbackAction_with_the_passed_context_when_execut .FallbackAsync(fallbackActionAsync, onFallbackAsync); await fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrowAsync(); contextData.Should() diff --git a/test/Polly.Specs/Fallback/FallbackSpecs.cs b/test/Polly.Specs/Fallback/FallbackSpecs.cs index b577bef5b6c..de3b5ea4955 100644 --- a/test/Polly.Specs/Fallback/FallbackSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackSpecs.cs @@ -765,7 +765,7 @@ public void Should_call_onFallback_with_the_passed_context() .Fallback(fallbackAction, onFallback); fallbackPolicy.Invoking(p => p.Execute(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrow(); contextData.Should() @@ -787,7 +787,7 @@ public void Should_call_onFallback_with_the_passed_context_when_execute_and_capt .Fallback(fallbackAction, onFallback); fallbackPolicy.Invoking(p => p.ExecuteAndCapture(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrow(); contextData.Should() @@ -810,11 +810,11 @@ public void Should_call_onFallback_with_independent_context_for_independent_call .Fallback(fallbackAction, onFallback); fallbackPolicy.Invoking( - p => p.Execute(_ => throw new ArgumentNullException(), new { key = "value1" }.AsDictionary())) + p => p.Execute(_ => throw new ArgumentNullException(), CreateDictionary("key", "value1"))) .Should().NotThrow(); fallbackPolicy.Invoking( - p => p.Execute(_ => throw new DivideByZeroException(), new { key = "value2" }.AsDictionary())) + p => p.Execute(_ => throw new DivideByZeroException(), CreateDictionary("key", "value2"))) .Should().NotThrow(); contextData.Count.Should().Be(2); @@ -859,7 +859,7 @@ public void Should_call_fallbackAction_with_the_passed_context() .Fallback(fallbackAction, onFallback); fallbackPolicy.Invoking(p => p.Execute(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrow(); contextData.Should() @@ -881,7 +881,7 @@ public void Should_call_fallbackAction_with_the_passed_context_when_execute_and_ .Fallback(fallbackAction, onFallback); fallbackPolicy.Invoking(p => p.ExecuteAndCapture(_ => throw new ArgumentNullException(), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrow(); contextData.Should() diff --git a/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs b/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs index 75ddf817ed6..a06431b0e92 100644 --- a/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs @@ -344,7 +344,7 @@ public void Should_call_onFallback_with_the_passed_context() .FallbackAsync(fallbackAction, onFallbackAsync); fallbackPolicy.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Fault), - new { key1 = "value1", key2 = "value2" }.AsDictionary()) + CreateDictionary("key1", "value1", "key2", "value2")) .Result .Should().Be(ResultPrimitive.Substitute); @@ -367,7 +367,7 @@ public async Task Should_call_onFallback_with_the_passed_context_when_execute_an .FallbackAsync(fallbackAction, onFallbackAsync); (await fallbackPolicy.ExecuteAndCaptureAsync(_ => Task.FromResult(ResultPrimitive.Fault), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Result.Should().Be(ResultPrimitive.Substitute); contextData.Should() @@ -389,11 +389,11 @@ public void Should_call_onFallback_with_independent_context_for_independent_call .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction, onFallbackAsync); - fallbackPolicy.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Fault), new { key = "value1" }.AsDictionary()) + fallbackPolicy.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Fault), CreateDictionary("key", "value1")) .Result .Should().Be(ResultPrimitive.Substitute); - fallbackPolicy.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.FaultAgain), new { key = "value2" }.AsDictionary()) + fallbackPolicy.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.FaultAgain), CreateDictionary("key", "value2")) .Result .Should().Be(ResultPrimitive.Substitute); @@ -439,7 +439,7 @@ public void Should_call_fallbackAction_with_the_passed_context() .FallbackAsync(fallbackActionAsync, onFallbackAsync); fallbackPolicy.ExecuteAsync(_ => Task.FromResult(ResultPrimitive.Fault), - new { key1 = "value1", key2 = "value2" }.AsDictionary()) + CreateDictionary("key1", "value1", "key2", "value2")) .Result .Should().Be(ResultPrimitive.Substitute); @@ -462,7 +462,7 @@ public async Task Should_call_fallbackAction_with_the_passed_context_when_execut .FallbackAsync(fallbackActionAsync, onFallbackAsync); await fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(_ => Task.FromResult(ResultPrimitive.Fault), - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrowAsync(); contextData.Should() diff --git a/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs b/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs index 26719da3cea..6f51dd02bcd 100644 --- a/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs @@ -404,7 +404,7 @@ public void Should_call_onFallback_with_the_passed_context() .Fallback(fallbackAction, onFallback); fallbackPolicy.Execute(_ => ResultPrimitive.Fault, - new { key1 = "value1", key2 = "value2" }.AsDictionary()) + CreateDictionary("key1", "value1", "key2", "value2")) .Should().Be(ResultPrimitive.Substitute); contextData.Should() @@ -426,7 +426,7 @@ public void Should_call_onFallback_with_the_passed_context_when_execute_and_capt .Fallback(fallbackAction, onFallback); fallbackPolicy.ExecuteAndCapture(_ => ResultPrimitive.Fault, - new { key1 = "value1", key2 = "value2" }.AsDictionary()) + CreateDictionary("key1", "value1", "key2", "value2")) .Result.Should().Be(ResultPrimitive.Substitute); contextData.Should() @@ -448,10 +448,10 @@ public void Should_call_onFallback_with_independent_context_for_independent_call .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction, onFallback); - fallbackPolicy.Execute(_ => ResultPrimitive.Fault, new { key = "value1" }.AsDictionary()) + fallbackPolicy.Execute(_ => ResultPrimitive.Fault, CreateDictionary("key", "value1")) .Should().Be(ResultPrimitive.Substitute); - fallbackPolicy.Execute(_ => ResultPrimitive.FaultAgain, new { key = "value2" }.AsDictionary()) + fallbackPolicy.Execute(_ => ResultPrimitive.FaultAgain, CreateDictionary("key", "value2")) .Should().Be(ResultPrimitive.Substitute); contextData.Count.Should().Be(2); @@ -495,7 +495,7 @@ public void Should_call_fallbackAction_with_the_passed_context() .Fallback(fallbackAction, onFallback); fallbackPolicy.Execute(_ => ResultPrimitive.Fault, - new { key1 = "value1", key2 = "value2" }.AsDictionary()) + CreateDictionary("key1", "value1", "key2", "value2")) .Should().Be(ResultPrimitive.Substitute); contextData.Should() @@ -517,7 +517,7 @@ public void Should_call_fallbackAction_with_the_passed_context_when_execute_and_ .Fallback(fallbackAction, onFallback); fallbackPolicy.ExecuteAndCapture(_ => ResultPrimitive.Fault, - new { key1 = "value1", key2 = "value2" }.AsDictionary()) + CreateDictionary("key1", "value1", "key2", "value2")) .Result.Should().Be(ResultPrimitive.Substitute); contextData.Should() diff --git a/test/Polly.Specs/Helpers/ObjectExtensions.cs b/test/Polly.Specs/Helpers/ObjectExtensions.cs deleted file mode 100644 index f6974c9bbc1..00000000000 --- a/test/Polly.Specs/Helpers/ObjectExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Polly.Specs.Helpers; - -public static class ObjectExtensions -{ -#pragma warning disable S4225 - public static IDictionary AsDictionary(this object source) => -#pragma warning restore S4225 - source.GetType().GetRuntimeProperties().ToDictionary( - propInfo => propInfo.Name, - propInfo => propInfo.GetValue(source, null)!); -} diff --git a/test/Polly.Specs/Polly.Specs.csproj b/test/Polly.Specs/Polly.Specs.csproj index 1c8581a15bf..6550cca3845 100644 --- a/test/Polly.Specs/Polly.Specs.csproj +++ b/test/Polly.Specs/Polly.Specs.csproj @@ -18,6 +18,7 @@ + diff --git a/test/Polly.Specs/Retry/RetryAsyncSpecs.cs b/test/Polly.Specs/Retry/RetryAsyncSpecs.cs index dc451354cea..4cca60d4e35 100644 --- a/test/Polly.Specs/Retry/RetryAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/RetryAsyncSpecs.cs @@ -296,7 +296,7 @@ public void Should_call_onretry_with_the_passed_context() .RetryAsync((_, _, context) => contextData = context); policy.RaiseExceptionAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary()); + CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -313,7 +313,7 @@ public async Task Should_call_onretry_with_the_passed_context_when_execute_and_c .RetryAsync((_, _, context) => contextData = context); await policy.Awaiting(p => p.ExecuteAndCaptureAsync(_ => { throw new DivideByZeroException(); }, - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrowAsync(); contextData.Should() @@ -346,12 +346,12 @@ public void Should_create_new_context_for_each_call_to_execute() .RetryAsync((_, _, context) => contextValue = context["key"].ToString()); policy.RaiseExceptionAsync( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseExceptionAsync( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } @@ -366,13 +366,13 @@ public async Task Should_create_new_context_for_each_call_to_execute_and_capture .RetryAsync((_, _, context) => contextValue = context["key"].ToString()); await policy.Awaiting(p => p.ExecuteAndCaptureAsync(_ => throw new DivideByZeroException(), - new { key = "original_value" }.AsDictionary())) + CreateDictionary("key", "original_value"))) .Should().NotThrowAsync(); contextValue.Should().Be("original_value"); await policy.Awaiting(p => p.ExecuteAndCaptureAsync(_ => throw new DivideByZeroException(), - new { key = "new_value" }.AsDictionary())) + CreateDictionary("key", "new_value"))) .Should().NotThrowAsync(); contextValue.Should().Be("new_value"); diff --git a/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs b/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs index 2013ba6f21e..7c45c04293b 100644 --- a/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs @@ -162,7 +162,7 @@ public void Should_call_onretry_on_each_retry_with_the_passed_context() .RetryForeverAsync((_, context) => contextData = context); policy.RaiseExceptionAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary()); + CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -210,12 +210,12 @@ public void Should_create_new_context_for_each_call_to_execute() .RetryForeverAsync((_, context) => contextValue = context["key"].ToString()); policy.RaiseExceptionAsync( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseExceptionAsync( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/Retry/RetryForeverSpecs.cs b/test/Polly.Specs/Retry/RetryForeverSpecs.cs index 094d2f6de31..495bfe20d30 100644 --- a/test/Polly.Specs/Retry/RetryForeverSpecs.cs +++ b/test/Polly.Specs/Retry/RetryForeverSpecs.cs @@ -182,7 +182,7 @@ public void Should_call_onretry_on_each_retry_with_the_passed_context() .RetryForever((_, context) => contextData = context); policy.RaiseException( - new { key1 = "value1", key2 = "value2" }.AsDictionary()); + CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -231,12 +231,12 @@ public void Should_create_new_context_for_each_call_to_execute() .RetryForever((_, context) => contextValue = context["key"].ToString()); policy.RaiseException( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseException( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } diff --git a/test/Polly.Specs/Retry/RetrySpecs.cs b/test/Polly.Specs/Retry/RetrySpecs.cs index bbaab8199ac..352b40b2df7 100644 --- a/test/Polly.Specs/Retry/RetrySpecs.cs +++ b/test/Polly.Specs/Retry/RetrySpecs.cs @@ -387,7 +387,7 @@ public void Should_call_onretry_with_the_passed_context() .Retry((_, _, context) => contextData = context); policy.RaiseException( - new { key1 = "value1", key2 = "value2" }.AsDictionary()); + CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -404,7 +404,7 @@ public void Should_call_onretry_with_the_passed_context_when_execute_and_capture .Retry((_, _, context) => contextData = context); policy.Invoking(p => p.ExecuteAndCapture(_ => { throw new DivideByZeroException(); }, - new { key1 = "value1", key2 = "value2" }.AsDictionary())) + CreateDictionary("key1", "value1", "key2", "value2"))) .Should().NotThrow(); contextData.Should() @@ -437,12 +437,12 @@ public void Should_create_new_context_for_each_call_to_execute() .Retry((_, _, context) => contextValue = context["key"].ToString()); policy.RaiseException( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseException( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } @@ -457,13 +457,13 @@ public void Should_create_new_context_for_each_call_to_execute_and_capture() .Retry((_, _, context) => contextValue = context["key"].ToString()); policy.Invoking(p => p.ExecuteAndCapture(_ => throw new DivideByZeroException(), - new { key = "original_value" }.AsDictionary())) + CreateDictionary("key", "original_value"))) .Should().NotThrow(); contextValue.Should().Be("original_value"); policy.Invoking(p => p.ExecuteAndCapture(_ => throw new DivideByZeroException(), - new { key = "new_value" }.AsDictionary())) + CreateDictionary("key", "new_value"))) .Should().NotThrow(); contextValue.Should().Be("new_value"); diff --git a/test/Polly.Specs/Retry/RetryTResultAsyncSpecs.cs b/test/Polly.Specs/Retry/RetryTResultAsyncSpecs.cs index 5da67362344..863ea0b0149 100644 --- a/test/Polly.Specs/Retry/RetryTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/RetryTResultAsyncSpecs.cs @@ -287,7 +287,7 @@ public async Task Should_call_onretry_with_the_passed_context() .RetryAsync((_, _, context) => contextData = context); (await policy.RaiseResultSequenceAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary(), + CreateDictionary("key1", "value1", "key2", "value2"), ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Be(ResultPrimitive.Good); @@ -306,7 +306,7 @@ public async Task Should_call_onretry_with_the_passed_context_when_execute_and_c .RetryAsync((_, _, context) => contextData = context); PolicyResult result = await policy.RaiseResultSequenceOnExecuteAndCaptureAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary(), + CreateDictionary("key1", "value1", "key2", "value2"), ResultPrimitive.Fault, ResultPrimitive.Good); result.Should().BeEquivalentTo(new @@ -349,13 +349,13 @@ public async Task Should_create_new_context_for_each_call_to_execute() .RetryAsync((_, _, context) => contextValue = context["key"].ToString()); await policy.RaiseResultSequenceAsync( - new { key = "original_value" }.AsDictionary(), + CreateDictionary("key", "original_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("original_value"); await policy.RaiseResultSequenceAsync( - new { key = "new_value" }.AsDictionary(), + CreateDictionary("key", "new_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("new_value"); @@ -371,13 +371,13 @@ public async Task Should_create_new_context_for_each_call_to_execute_and_capture .RetryAsync((_, _, context) => contextValue = context["key"].ToString()); await policy.RaiseResultSequenceOnExecuteAndCaptureAsync( - new { key = "original_value" }.AsDictionary(), + CreateDictionary("key", "original_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("original_value"); await policy.RaiseResultSequenceOnExecuteAndCaptureAsync( - new { key = "new_value" }.AsDictionary(), + CreateDictionary("key", "new_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("new_value"); @@ -424,7 +424,7 @@ public async Task Should_not_call_onretry_when_retry_count_is_zero_with_context( .RetryAsync(0, onRetry); (await policy.RaiseResultSequenceAsync( - new { key = "value" }.AsDictionary(), + CreateDictionary("key", "value"), ResultPrimitive.Fault, ResultPrimitive.Good)).Should().Be(ResultPrimitive.Fault); retryInvoked.Should().BeFalse(); diff --git a/test/Polly.Specs/Retry/RetryTResultSpecs.cs b/test/Polly.Specs/Retry/RetryTResultSpecs.cs index 2fdf3151494..1b9a1a99b56 100644 --- a/test/Polly.Specs/Retry/RetryTResultSpecs.cs +++ b/test/Polly.Specs/Retry/RetryTResultSpecs.cs @@ -284,7 +284,7 @@ public void Should_call_onretry_with_the_passed_context() .Retry((_, _, context) => contextData = context); policy.RaiseResultSequence( - new { key1 = "value1", key2 = "value2" }.AsDictionary(), + CreateDictionary("key1", "value1", "key2", "value2"), ResultPrimitive.Fault, ResultPrimitive.Good) .Should().Be(ResultPrimitive.Good); @@ -303,7 +303,7 @@ public void Should_call_onretry_with_the_passed_context_when_execute_and_capture .Retry((_, _, context) => contextData = context); PolicyResult result = policy.RaiseResultSequenceOnExecuteAndCapture( - new { key1 = "value1", key2 = "value2" }.AsDictionary(), + CreateDictionary("key1", "value1", "key2", "value2"), ResultPrimitive.Fault, ResultPrimitive.Good); result.Should().BeEquivalentTo(new @@ -346,13 +346,13 @@ public void Should_create_new_context_for_each_call_to_execute() .Retry((_, _, context) => contextValue = context["key"].ToString()); policy.RaiseResultSequence( - new { key = "original_value" }.AsDictionary(), + CreateDictionary("key", "original_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("original_value"); policy.RaiseResultSequence( - new { key = "new_value" }.AsDictionary(), + CreateDictionary("key", "new_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("new_value"); @@ -368,13 +368,13 @@ public void Should_create_new_context_for_each_call_to_execute_and_capture() .Retry((_, _, context) => contextValue = context["key"].ToString()); policy.RaiseResultSequenceOnExecuteAndCapture( - new { key = "original_value" }.AsDictionary(), + CreateDictionary("key", "original_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("original_value"); policy.RaiseResultSequenceOnExecuteAndCapture( - new { key = "new_value" }.AsDictionary(), + CreateDictionary("key", "new_value"), ResultPrimitive.Fault, ResultPrimitive.Good); contextValue.Should().Be("new_value"); @@ -421,7 +421,7 @@ public void Should_not_call_onretry_when_retry_count_is_zero_with_context() .Retry(0, onRetry); policy.RaiseResultSequence( - new { key = "value" }.AsDictionary(), + CreateDictionary("key", "value"), ResultPrimitive.Fault, ResultPrimitive.Good).Should().Be(ResultPrimitive.Fault); retryInvoked.Should().BeFalse(); diff --git a/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs b/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs index 1f06fcaa2ca..9634a3eb186 100644 --- a/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs @@ -487,7 +487,7 @@ public async Task Should_call_onretry_with_the_passed_context() }, (_, _, context) => contextData = context); await policy.RaiseExceptionAsync( - new { key1 = "value1", key2 = "value2" }.AsDictionary()); + CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -527,12 +527,12 @@ public async Task Should_create_new_context_for_each_call_to_execute() (_, _, context) => contextValue = context["key"].ToString()); await policy.RaiseExceptionAsync( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); await policy.RaiseExceptionAsync( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } @@ -681,7 +681,7 @@ await policy.ExecuteAsync(async (context, _) => throw new DivideByZeroException(); } }, - new { RetryAfter = defaultRetryAfter }.AsDictionary(), // Can also set an initial value for RetryAfter, in the Context passed into the call. + CreateDictionary("RetryAfter", defaultRetryAfter), // Can also set an initial value for RetryAfter, in the Context passed into the call. CancellationToken.None); actualRetryDuration.Should().Be(expectedRetryDuration); diff --git a/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs b/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs index e18853fec86..e63fbf62870 100644 --- a/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs @@ -323,12 +323,12 @@ public void Should_create_new_context_for_each_call_to_policy() (_, _, context) => contextValue = context["key"].ToString()); policy.RaiseExceptionAsync( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseExceptionAsync( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } @@ -422,7 +422,7 @@ await policy.ExecuteAsync(async (context, _) => throw new DivideByZeroException(); } }, - new { RetryAfter = defaultRetryAfter }.AsDictionary(), // Can also set an initial value for RetryAfter, in the Context passed into the call. + CreateDictionary("RetryAfter", defaultRetryAfter), // Can also set an initial value for RetryAfter, in the Context passed into the call. CancellationToken.None); actualRetryDuration.Should().Be(expectedRetryDuration); diff --git a/test/Polly.Specs/Retry/WaitAndRetryForeverSpecs.cs b/test/Polly.Specs/Retry/WaitAndRetryForeverSpecs.cs index 40afad2bb9c..ae2a193c680 100644 --- a/test/Polly.Specs/Retry/WaitAndRetryForeverSpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetryForeverSpecs.cs @@ -251,12 +251,12 @@ public void Should_create_new_context_for_each_call_to_policy() (_, _, context) => contextValue = context["key"].ToString()); policy.RaiseException( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseException( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } @@ -344,7 +344,7 @@ public void Should_be_able_to_pass_retry_duration_from_execution_to_sleepDuratio throw new DivideByZeroException(); } }, - new { RetryAfter = defaultRetryAfter }.AsDictionary()); // Can also set an initial value for RetryAfter, in the Context passed into the call. + CreateDictionary("RetryAfter", defaultRetryAfter)); // Can also set an initial value for RetryAfter, in the Context passed into the call. actualRetryDuration.Should().Be(expectedRetryDuration); } diff --git a/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs b/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs index c4cb7000d10..f94f29facd0 100644 --- a/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs @@ -513,7 +513,7 @@ public void Should_call_onretry_with_the_passed_context() }, (_, _, context) => contextData = context); policy.RaiseException( - new { key1 = "value1", key2 = "value2" }.AsDictionary()); + CreateDictionary("key1", "value1", "key2", "value2")); contextData.Should() .ContainKeys("key1", "key2").And @@ -534,12 +534,12 @@ public void Should_create_new_context_for_each_call_to_execute() (_, _, context) => contextValue = context["key"].ToString()); policy.RaiseException( - new { key = "original_value" }.AsDictionary()); + CreateDictionary("key", "original_value")); contextValue.Should().Be("original_value"); policy.RaiseException( - new { key = "new_value" }.AsDictionary()); + CreateDictionary("key", "new_value")); contextValue.Should().Be("new_value"); } @@ -768,7 +768,7 @@ public void Should_be_able_to_pass_retry_duration_from_execution_to_sleepDuratio throw new DivideByZeroException(); } }, - new { RetryAfter = defaultRetryAfter }.AsDictionary()); // Can also set an initial value for RetryAfter, in the Context passed into the call. + CreateDictionary("RetryAfter", defaultRetryAfter)); // Can also set an initial value for RetryAfter, in the Context passed into the call. actualRetryDuration.Should().Be(expectedRetryDuration); }