diff --git a/test/Polly.Specs/Caching/SerializingCacheProviderAsyncSpecs.cs b/test/Polly.Specs/Caching/AsyncSerializingCacheProviderSpecs.cs similarity index 100% rename from test/Polly.Specs/Caching/SerializingCacheProviderAsyncSpecs.cs rename to test/Polly.Specs/Caching/AsyncSerializingCacheProviderSpecs.cs diff --git a/test/Polly.Specs/CircuitBreaker/ICircuitBreakerPolicyTResultSpecs.cs b/test/Polly.Specs/CircuitBreaker/ICircuitBreakerTResultPolicySpecs.cs similarity index 100% rename from test/Polly.Specs/CircuitBreaker/ICircuitBreakerPolicyTResultSpecs.cs rename to test/Polly.Specs/CircuitBreaker/ICircuitBreakerTResultPolicySpecs.cs diff --git a/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs b/test/Polly.Specs/PolicyKeyAsyncSpecs.cs similarity index 57% rename from test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs rename to test/Polly.Specs/PolicyKeyAsyncSpecs.cs index 2fad80e08cd..697875429cc 100644 --- a/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs +++ b/test/Polly.Specs/PolicyKeyAsyncSpecs.cs @@ -202,151 +202,3 @@ await retry.ExecuteAsync(async _ => } #endregion } - -public class PolicyTResultKeyAsyncSpecs -{ - #region Configuration - - [Fact] - public void Should_be_able_fluently_to_configure_the_policy_key() - { - var policy = Policy.HandleResult(0).RetryAsync().WithPolicyKey(Guid.NewGuid().ToString()); - - policy.Should().BeAssignableTo>(); - } - - [Fact] - public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() - { - IAsyncPolicy policyAsInterface = Policy.HandleResult(0).RetryAsync(); - var policyAsInterfaceAfterWithPolicyKey = policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); - - policyAsInterfaceAfterWithPolicyKey.Should().BeAssignableTo>(); - } - - [Fact] - public void PolicyKey_property_should_be_the_fluently_configured_policy_key() - { - const string Key = "SomePolicyKey"; - - var policy = Policy.HandleResult(0).RetryAsync().WithPolicyKey(Key); - - policy.PolicyKey.Should().Be(Key); - } - - [Fact] - public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once() - { - var policy = Policy.HandleResult(0).RetryAsync(); - - Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); - - configure.Should().NotThrow(); - - configure.Should().Throw().And.ParamName.Should().Be("policyKey"); - } - - [Fact] - public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once_via_interface() - { - IAsyncPolicy policyAsInterface = Policy.HandleResult(0).RetryAsync(); - Action configure = () => policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); - - configure.Should().NotThrow(); - - configure.Should().Throw().And.ParamName.Should().Be("policyKey"); - } - - [Fact] - public void PolicyKey_property_should_be_non_null_or_empty_if_not_explicitly_configured() - { - var policy = Policy.HandleResult(0).RetryAsync(); - - policy.PolicyKey.Should().NotBeNullOrEmpty(); - } - - [Fact] - public void PolicyKey_property_should_start_with_policy_type_if_not_explicitly_configured() - { - var policy = Policy.HandleResult(0).RetryAsync(); - - policy.PolicyKey.Should().StartWith("AsyncRetry"); - } - - [Fact] - public void PolicyKey_property_should_be_unique_for_different_instances_if_not_explicitly_configured() - { - var policy1 = Policy.HandleResult(0).RetryAsync(); - var policy2 = Policy.HandleResult(0).RetryAsync(); - - policy1.PolicyKey.Should().NotBe(policy2.PolicyKey); - } - - [Fact] - public void PolicyKey_property_should_return_consistent_value_for_same_policy_instance_if_not_explicitly_configured() - { - var policy = Policy.HandleResult(0).RetryAsync(); - - var keyRetrievedFirst = policy.PolicyKey; - var keyRetrievedSecond = policy.PolicyKey; - - keyRetrievedSecond.Should().Be(keyRetrievedFirst); - } - - [Fact] - public void Should_not_be_able_to_configure_the_policy_key_explicitly_after_retrieving_default_value() - { - var policy = Policy.HandleResult(0).RetryAsync(); - - string retrieveKeyWhenNotExplicitlyConfigured = policy.PolicyKey; - - Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); - - configure.Should().Throw().And.ParamName.Should().Be("policyKey"); - } - - #endregion - - #region PolicyKey and execution Context tests - - [Fact] - public async Task Should_pass_PolicyKey_to_execution_context() - { - string policyKey = Guid.NewGuid().ToString(); - - string? policyKeySetOnExecutionContext = null; - Action, int, Context> onRetry = (_, _, context) => { policyKeySetOnExecutionContext = context.PolicyKey; }; - var retry = Policy.HandleResult(ResultPrimitive.Fault).RetryAsync(1, onRetry).WithPolicyKey(policyKey); - - await retry.RaiseResultSequenceAsync(ResultPrimitive.Fault, ResultPrimitive.Good); - - policyKeySetOnExecutionContext.Should().Be(policyKey); - } - - [Fact] - public async Task Should_pass_OperationKey_to_execution_context() - { - string operationKey = "SomeKey"; - - string? operationKeySetOnContext = null; - Action, int, Context> onRetry = (_, _, context) => { operationKeySetOnContext = context.OperationKey; }; - var retry = Policy.HandleResult(ResultPrimitive.Fault).RetryAsync(1, onRetry); - - bool firstExecution = true; - await retry.ExecuteAsync(async _ => - { - await TaskHelper.EmptyTask; - if (firstExecution) - { - firstExecution = false; - return ResultPrimitive.Fault; - } - - return ResultPrimitive.Good; - }, new Context(operationKey)); - - operationKeySetOnContext.Should().Be(operationKey); - } - - #endregion -} diff --git a/test/Polly.Specs/PolicyContextAndKeySpecs.cs b/test/Polly.Specs/PolicyKeySpecs.cs similarity index 56% rename from test/Polly.Specs/PolicyContextAndKeySpecs.cs rename to test/Polly.Specs/PolicyKeySpecs.cs index 4c17f00c718..a729d0ee30c 100644 --- a/test/Polly.Specs/PolicyContextAndKeySpecs.cs +++ b/test/Polly.Specs/PolicyKeySpecs.cs @@ -180,139 +180,3 @@ public void Should_pass_OperationKey_to_execution_context_in_generic_execution_o } #endregion } - -public class PolicyTResultKeySpecs -{ - #region Configuration - - [Fact] - public void Should_be_able_fluently_to_configure_the_policy_key() - { - var policy = Policy.HandleResult(0).Retry().WithPolicyKey(Guid.NewGuid().ToString()); - - policy.Should().BeAssignableTo>(); - } - - [Fact] - public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() - { - ISyncPolicy policyAsInterface = Policy.HandleResult(0).Retry(); - var policyAsInterfaceAfterWithPolicyKey = policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); - - policyAsInterfaceAfterWithPolicyKey.Should().BeAssignableTo>(); - } - - [Fact] - public void PolicyKey_property_should_be_the_fluently_configured_policy_key() - { - const string Key = "SomePolicyKey"; - - var policy = Policy.HandleResult(0).Retry().WithPolicyKey(Key); - - policy.PolicyKey.Should().Be(Key); - } - - [Fact] - public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once() - { - var policy = Policy.HandleResult(0).Retry(); - - Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); - - configure.Should().NotThrow(); - - configure.Should().Throw().And.ParamName.Should().Be("policyKey"); - } - - [Fact] - public void PolicyKey_property_should_be_non_null_or_empty_if_not_explicitly_configured() - { - var policy = Policy.HandleResult(0).Retry(); - - policy.PolicyKey.Should().NotBeNullOrEmpty(); - } - - [Fact] - public void PolicyKey_property_should_start_with_policy_type_if_not_explicitly_configured() - { - var policy = Policy.HandleResult(0).Retry(); - - policy.PolicyKey.Should().StartWith("Retry"); - } - - [Fact] - public void PolicyKey_property_should_be_unique_for_different_instances_if_not_explicitly_configured() - { - var policy1 = Policy.HandleResult(0).Retry(); - var policy2 = Policy.HandleResult(0).Retry(); - - policy1.PolicyKey.Should().NotBe(policy2.PolicyKey); - } - - [Fact] - public void PolicyKey_property_should_return_consistent_value_for_same_policy_instance_if_not_explicitly_configured() - { - var policy = Policy.HandleResult(0).Retry(); - - var keyRetrievedFirst = policy.PolicyKey; - var keyRetrievedSecond = policy.PolicyKey; - - keyRetrievedSecond.Should().Be(keyRetrievedFirst); - } - - [Fact] - public void Should_not_be_able_to_configure_the_policy_key_explicitly_after_retrieving_default_value() - { - var policy = Policy.HandleResult(0).Retry(); - - string retrieveKeyWhenNotExplicitlyConfigured = policy.PolicyKey; - - Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); - - configure.Should().Throw().And.ParamName.Should().Be("policyKey"); - } - - #endregion - - #region PolicyKey and execution Context tests - - [Fact] - public void Should_pass_PolicyKey_to_execution_context() - { - string policyKey = Guid.NewGuid().ToString(); - - string? policyKeySetOnExecutionContext = null; - Action, int, Context> onRetry = (_, _, context) => { policyKeySetOnExecutionContext = context.PolicyKey; }; - var retry = Policy.HandleResult(ResultPrimitive.Fault).Retry(1, onRetry).WithPolicyKey(policyKey); - - retry.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good); - - policyKeySetOnExecutionContext.Should().Be(policyKey); - } - - [Fact] - public void Should_pass_OperationKey_to_execution_context() - { - string operationKey = "SomeKey"; - - string? operationKeySetOnContext = null; - Action, int, Context> onRetry = (_, _, context) => { operationKeySetOnContext = context.OperationKey; }; - var retry = Policy.HandleResult(ResultPrimitive.Fault).Retry(1, onRetry); - - bool firstExecution = true; - retry.Execute(_ => - { - if (firstExecution) - { - firstExecution = false; - return ResultPrimitive.Fault; - } - - return ResultPrimitive.Good; - }, new Context(operationKey)); - - operationKeySetOnContext.Should().Be(operationKey); - } - - #endregion -} diff --git a/test/Polly.Specs/PolicyTResultKeyAsyncSpecs.cs b/test/Polly.Specs/PolicyTResultKeyAsyncSpecs.cs new file mode 100644 index 00000000000..fee237aee53 --- /dev/null +++ b/test/Polly.Specs/PolicyTResultKeyAsyncSpecs.cs @@ -0,0 +1,149 @@ +namespace Polly.Specs; + +public class PolicyTResultKeyAsyncSpecs +{ + #region Configuration + + [Fact] + public void Should_be_able_fluently_to_configure_the_policy_key() + { + var policy = Policy.HandleResult(0).RetryAsync().WithPolicyKey(Guid.NewGuid().ToString()); + + policy.Should().BeAssignableTo>(); + } + + [Fact] + public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() + { + IAsyncPolicy policyAsInterface = Policy.HandleResult(0).RetryAsync(); + var policyAsInterfaceAfterWithPolicyKey = policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); + + policyAsInterfaceAfterWithPolicyKey.Should().BeAssignableTo>(); + } + + [Fact] + public void PolicyKey_property_should_be_the_fluently_configured_policy_key() + { + const string Key = "SomePolicyKey"; + + var policy = Policy.HandleResult(0).RetryAsync().WithPolicyKey(Key); + + policy.PolicyKey.Should().Be(Key); + } + + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once() + { + var policy = Policy.HandleResult(0).RetryAsync(); + + Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().NotThrow(); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once_via_interface() + { + IAsyncPolicy policyAsInterface = Policy.HandleResult(0).RetryAsync(); + Action configure = () => policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().NotThrow(); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + + [Fact] + public void PolicyKey_property_should_be_non_null_or_empty_if_not_explicitly_configured() + { + var policy = Policy.HandleResult(0).RetryAsync(); + + policy.PolicyKey.Should().NotBeNullOrEmpty(); + } + + [Fact] + public void PolicyKey_property_should_start_with_policy_type_if_not_explicitly_configured() + { + var policy = Policy.HandleResult(0).RetryAsync(); + + policy.PolicyKey.Should().StartWith("AsyncRetry"); + } + + [Fact] + public void PolicyKey_property_should_be_unique_for_different_instances_if_not_explicitly_configured() + { + var policy1 = Policy.HandleResult(0).RetryAsync(); + var policy2 = Policy.HandleResult(0).RetryAsync(); + + policy1.PolicyKey.Should().NotBe(policy2.PolicyKey); + } + + [Fact] + public void PolicyKey_property_should_return_consistent_value_for_same_policy_instance_if_not_explicitly_configured() + { + var policy = Policy.HandleResult(0).RetryAsync(); + + var keyRetrievedFirst = policy.PolicyKey; + var keyRetrievedSecond = policy.PolicyKey; + + keyRetrievedSecond.Should().Be(keyRetrievedFirst); + } + + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_after_retrieving_default_value() + { + var policy = Policy.HandleResult(0).RetryAsync(); + + string retrieveKeyWhenNotExplicitlyConfigured = policy.PolicyKey; + + Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + + #endregion + + #region PolicyKey and execution Context tests + + [Fact] + public async Task Should_pass_PolicyKey_to_execution_context() + { + string policyKey = Guid.NewGuid().ToString(); + + string? policyKeySetOnExecutionContext = null; + Action, int, Context> onRetry = (_, _, context) => { policyKeySetOnExecutionContext = context.PolicyKey; }; + var retry = Policy.HandleResult(ResultPrimitive.Fault).RetryAsync(1, onRetry).WithPolicyKey(policyKey); + + await retry.RaiseResultSequenceAsync(ResultPrimitive.Fault, ResultPrimitive.Good); + + policyKeySetOnExecutionContext.Should().Be(policyKey); + } + + [Fact] + public async Task Should_pass_OperationKey_to_execution_context() + { + string operationKey = "SomeKey"; + + string? operationKeySetOnContext = null; + Action, int, Context> onRetry = (_, _, context) => { operationKeySetOnContext = context.OperationKey; }; + var retry = Policy.HandleResult(ResultPrimitive.Fault).RetryAsync(1, onRetry); + + bool firstExecution = true; + await retry.ExecuteAsync(async _ => + { + await TaskHelper.EmptyTask; + if (firstExecution) + { + firstExecution = false; + return ResultPrimitive.Fault; + } + + return ResultPrimitive.Good; + }, new Context(operationKey)); + + operationKeySetOnContext.Should().Be(operationKey); + } + + #endregion +} diff --git a/test/Polly.Specs/PolicyTResultKeySpecs.cs b/test/Polly.Specs/PolicyTResultKeySpecs.cs new file mode 100644 index 00000000000..6f464bc42aa --- /dev/null +++ b/test/Polly.Specs/PolicyTResultKeySpecs.cs @@ -0,0 +1,137 @@ +namespace Polly.Specs; + +public class PolicyTResultKeySpecs +{ + #region Configuration + + [Fact] + public void Should_be_able_fluently_to_configure_the_policy_key() + { + var policy = Policy.HandleResult(0).Retry().WithPolicyKey(Guid.NewGuid().ToString()); + + policy.Should().BeAssignableTo>(); + } + + [Fact] + public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() + { + ISyncPolicy policyAsInterface = Policy.HandleResult(0).Retry(); + var policyAsInterfaceAfterWithPolicyKey = policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); + + policyAsInterfaceAfterWithPolicyKey.Should().BeAssignableTo>(); + } + + [Fact] + public void PolicyKey_property_should_be_the_fluently_configured_policy_key() + { + const string Key = "SomePolicyKey"; + + var policy = Policy.HandleResult(0).Retry().WithPolicyKey(Key); + + policy.PolicyKey.Should().Be(Key); + } + + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once() + { + var policy = Policy.HandleResult(0).Retry(); + + Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().NotThrow(); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + + [Fact] + public void PolicyKey_property_should_be_non_null_or_empty_if_not_explicitly_configured() + { + var policy = Policy.HandleResult(0).Retry(); + + policy.PolicyKey.Should().NotBeNullOrEmpty(); + } + + [Fact] + public void PolicyKey_property_should_start_with_policy_type_if_not_explicitly_configured() + { + var policy = Policy.HandleResult(0).Retry(); + + policy.PolicyKey.Should().StartWith("Retry"); + } + + [Fact] + public void PolicyKey_property_should_be_unique_for_different_instances_if_not_explicitly_configured() + { + var policy1 = Policy.HandleResult(0).Retry(); + var policy2 = Policy.HandleResult(0).Retry(); + + policy1.PolicyKey.Should().NotBe(policy2.PolicyKey); + } + + [Fact] + public void PolicyKey_property_should_return_consistent_value_for_same_policy_instance_if_not_explicitly_configured() + { + var policy = Policy.HandleResult(0).Retry(); + + var keyRetrievedFirst = policy.PolicyKey; + var keyRetrievedSecond = policy.PolicyKey; + + keyRetrievedSecond.Should().Be(keyRetrievedFirst); + } + + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_after_retrieving_default_value() + { + var policy = Policy.HandleResult(0).Retry(); + + string retrieveKeyWhenNotExplicitlyConfigured = policy.PolicyKey; + + Action configure = () => policy.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + + #endregion + + #region PolicyKey and execution Context tests + + [Fact] + public void Should_pass_PolicyKey_to_execution_context() + { + string policyKey = Guid.NewGuid().ToString(); + + string? policyKeySetOnExecutionContext = null; + Action, int, Context> onRetry = (_, _, context) => { policyKeySetOnExecutionContext = context.PolicyKey; }; + var retry = Policy.HandleResult(ResultPrimitive.Fault).Retry(1, onRetry).WithPolicyKey(policyKey); + + retry.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good); + + policyKeySetOnExecutionContext.Should().Be(policyKey); + } + + [Fact] + public void Should_pass_OperationKey_to_execution_context() + { + string operationKey = "SomeKey"; + + string? operationKeySetOnContext = null; + Action, int, Context> onRetry = (_, _, context) => { operationKeySetOnContext = context.OperationKey; }; + var retry = Policy.HandleResult(ResultPrimitive.Fault).Retry(1, onRetry); + + bool firstExecution = true; + retry.Execute(_ => + { + if (firstExecution) + { + firstExecution = false; + return ResultPrimitive.Fault; + } + + return ResultPrimitive.Good; + }, new Context(operationKey)); + + operationKeySetOnContext.Should().Be(operationKey); + } + + #endregion +} diff --git a/test/Polly.Specs/Polly.Specs.csproj b/test/Polly.Specs/Polly.Specs.csproj index 242d8be268a..31ec59be592 100644 --- a/test/Polly.Specs/Polly.Specs.csproj +++ b/test/Polly.Specs/Polly.Specs.csproj @@ -10,7 +10,7 @@ true $(NoWarn);CA1030;CA1031;CA2008;CA2201 $(NoWarn);S103;S104;S2184;S3878;S6966 - $(NoWarn);SA1204;SA1402;SA1600;SA1649 + $(NoWarn);SA1204;SA1402;SA1600