Skip to content

Commit

Permalink
Fix S4225 warning (#2253)
Browse files Browse the repository at this point in the history
Refactor specs to avoid S4225 warning.
  • Loading branch information
sukreshmanda authored Jul 30, 2024
1 parent 411cfdc commit 8a8f3d8
Show file tree
Hide file tree
Showing 29 changed files with 130 additions and 128 deletions.
2 changes: 2 additions & 0 deletions test/Polly.Core.Tests/Retry/RetryHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class RetryHelperTests
public static TheoryData<int> Attempts()
{
#pragma warning disable IDE0028
#pragma warning disable IDE0022 // Use expression body for method
return new()
{
1,
Expand All @@ -23,6 +24,7 @@ public static TheoryData<int> Attempts()
1_024,
1_025,
};
#pragma warning restore IDE0022 // Use expression body for method
#pragma warning restore IDE0028
}

Expand Down
8 changes: 4 additions & 4 deletions test/Polly.Specs/Caching/CacheAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,10 @@ public async Task Should_allow_custom_FuncCacheKeyStrategy()
bool funcExecuted = false;
Func<Context, Task<object>> 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();
}

Expand All @@ -493,10 +493,10 @@ public async Task Should_allow_custom_ICacheKeyStrategy()
bool funcExecuted = false;
Func<Context, Task<object>> 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();
}

Expand Down
8 changes: 4 additions & 4 deletions test/Polly.Specs/Caching/CacheSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ public void Should_allow_custom_FuncCacheKeyStrategy()
bool funcExecuted = false;
Func<Context, object> 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();
}

Expand All @@ -492,10 +492,10 @@ public void Should_allow_custom_ICacheKeyStrategy()
bool funcExecuted = false;
Func<Context, object> 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();
}

Expand Down
8 changes: 4 additions & 4 deletions test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ public async Task Should_allow_custom_FuncCacheKeyStrategy()
bool funcExecuted = false;
Func<Context, Task<ResultClass>> 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();
}

Expand All @@ -329,10 +329,10 @@ public async Task Should_allow_custom_ICacheKeyStrategy()
bool funcExecuted = false;
Func<Context, Task<ResultClass>> 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();
}

Expand Down
8 changes: 4 additions & 4 deletions test/Polly.Specs/Caching/CacheTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,10 +1240,10 @@ public void Should_allow_custom_FuncICacheKeyStrategy()
bool funcExecuted = false;
Func<Context, ResultClass> 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();
}

Expand All @@ -1265,10 +1265,10 @@ public void Should_allow_custom_ICacheKeyStrategy()
bool funcExecuted = false;
Func<Context, ResultClass> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
breaker.CircuitState.Should().Be(CircuitState.Closed);

await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(
new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().ThrowAsync<DivideByZeroException>();
CreateDictionary("key1", "value1", "key2", "value2"))).Should().ThrowAsync<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);

contextData.Should()
Expand Down Expand Up @@ -2745,7 +2745,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
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()
Expand All @@ -2756,7 +2756,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
[Fact]
public async Task Context_should_be_empty_if_execute_not_called_with_any_context_data()
{
IDictionary<string, object> contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary();
IDictionary<string, object> contextData = CreateDictionary("key1", "value1", "key2", "value2");

Action<Exception, TimeSpan, Context> onBreak = (_, _, context) => { contextData = context; };
Action<Context> onReset = _ => { };
Expand Down Expand Up @@ -2833,7 +2833,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Closed);

await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(new { key = "original_value" }.AsDictionary()))
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(CreateDictionary("key", "original_value")))
.Should().ThrowAsync<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);
contextValue.Should().Be("original_value");
Expand All @@ -2846,7 +2846,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(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");
Expand Down
10 changes: 5 additions & 5 deletions test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ public void Should_call_onbreak_with_the_passed_context()
breaker.CircuitState.Should().Be(CircuitState.Closed);

breaker.Invoking(x => x.RaiseException<DivideByZeroException>(
new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().Throw<DivideByZeroException>();
CreateDictionary("key1", "value1", "key2", "value2"))).Should().Throw<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);

contextData.Should()
Expand Down Expand Up @@ -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()
Expand All @@ -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<string, object> contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary();
IDictionary<string, object> contextData = CreateDictionary("key1", "value1", "key2", "value2");

Action<Exception, TimeSpan, Context> onBreak = (_, _, context) => { contextData = context; };
Action<Context> onReset = _ => { };
Expand Down Expand Up @@ -2672,7 +2672,7 @@ public void Should_create_new_context_for_each_call_to_execute()
.Should().Throw<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Closed);

breaker.Invoking(x => x.RaiseException<DivideByZeroException>(new { key = "original_value" }.AsDictionary()))
breaker.Invoking(x => x.RaiseException<DivideByZeroException>(CreateDictionary("key", "original_value")))
.Should().Throw<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);
contextValue.Should().Be("original_value");
Expand All @@ -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");
}
Expand Down
10 changes: 5 additions & 5 deletions test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();

await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(
new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().ThrowAsync<DivideByZeroException>();
CreateDictionary("key1", "value1", "key2", "value2"))).Should().ThrowAsync<DivideByZeroException>();

breaker.CircuitState.Should().Be(CircuitState.Open);

Expand Down Expand Up @@ -1266,7 +1266,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
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
Expand All @@ -1276,7 +1276,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
[Fact]
public async Task Context_should_be_empty_if_execute_not_called_with_any_context_data()
{
IDictionary<string, object> contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary();
IDictionary<string, object> contextData = CreateDictionary("key1", "value1", "key2", "value2");

Action<Exception, TimeSpan, Context> onBreak = (_, _, context) => { contextData = context; };
Action<Context> onReset = _ => { };
Expand Down Expand Up @@ -1317,7 +1317,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();

// 2 exception raised, circuit is now open
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(new { key = "original_value" }.AsDictionary()))
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(CreateDictionary("key", "original_value")))
.Should().ThrowAsync<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);
contextValue.Should().Be("original_value");
Expand All @@ -1330,7 +1330,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>(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");
}
Expand Down
10 changes: 5 additions & 5 deletions test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ public void Should_call_onbreak_with_the_passed_context()
.Should().Throw<DivideByZeroException>();

breaker.Invoking(x => x.RaiseException<DivideByZeroException>(
new { key1 = "value1", key2 = "value2" }.AsDictionary())).Should().Throw<DivideByZeroException>();
CreateDictionary("key1", "value1", "key2", "value2"))).Should().Throw<DivideByZeroException>();

breaker.CircuitState.Should().Be(CircuitState.Open);

Expand Down Expand Up @@ -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
Expand All @@ -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<string, object> contextData = new { key1 = "value1", key2 = "value2" }.AsDictionary();
IDictionary<string, object> contextData = CreateDictionary("key1", "value1", "key2", "value2");

Action<Exception, TimeSpan, Context> onBreak = (_, _, context) => { contextData = context; };
Action<Context> onReset = _ => { };
Expand Down Expand Up @@ -1311,7 +1311,7 @@ public void Should_create_new_context_for_each_call_to_execute()
.Should().Throw<DivideByZeroException>();

// 2 exception raised, circuit is now open
breaker.Invoking(x => x.RaiseException<DivideByZeroException>(new { key = "original_value" }.AsDictionary()))
breaker.Invoking(x => x.RaiseException<DivideByZeroException>(CreateDictionary("key", "original_value")))
.Should().Throw<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);
contextValue.Should().Be("original_value");
Expand All @@ -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");
}
Expand Down
Loading

0 comments on commit 8a8f3d8

Please sign in to comment.