Skip to content

Commit

Permalink
Fix SA1649 warnings (#2173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zombach authored Jul 4, 2024
1 parent e622a7d commit c542b0e
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,151 +202,3 @@ await retry.ExecuteAsync<int>(async _ =>
}
#endregion
}

public class PolicyTResultKeyAsyncSpecs
{
#region Configuration

[Fact]
public void Should_be_able_fluently_to_configure_the_policy_key()
{
var policy = Policy.HandleResult<int>(0).RetryAsync().WithPolicyKey(Guid.NewGuid().ToString());

policy.Should().BeAssignableTo<AsyncPolicy<int>>();
}

[Fact]
public void Should_be_able_fluently_to_configure_the_policy_key_via_interface()
{
IAsyncPolicy<int> policyAsInterface = Policy.HandleResult<int>(0).RetryAsync();
var policyAsInterfaceAfterWithPolicyKey = policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString());

policyAsInterfaceAfterWithPolicyKey.Should().BeAssignableTo<IAsyncPolicy<int>>();
}

[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<ArgumentException>().And.ParamName.Should().Be("policyKey");
}

[Fact]
public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once_via_interface()
{
IAsyncPolicy<int> policyAsInterface = Policy.HandleResult(0).RetryAsync();
Action configure = () => policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString());

configure.Should().NotThrow();

configure.Should().Throw<ArgumentException>().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<ArgumentException>().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<DelegateResult<ResultPrimitive>, 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<DelegateResult<ResultPrimitive>, 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(0).Retry().WithPolicyKey(Guid.NewGuid().ToString());

policy.Should().BeAssignableTo<Policy<int>>();
}

[Fact]
public void Should_be_able_fluently_to_configure_the_policy_key_via_interface()
{
ISyncPolicy<int> policyAsInterface = Policy.HandleResult<int>(0).Retry();
var policyAsInterfaceAfterWithPolicyKey = policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString());

policyAsInterfaceAfterWithPolicyKey.Should().BeAssignableTo<ISyncPolicy<int>>();
}

[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<ArgumentException>().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<ArgumentException>().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<DelegateResult<ResultPrimitive>, 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<DelegateResult<ResultPrimitive>, 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
}
Loading

0 comments on commit c542b0e

Please sign in to comment.