Skip to content

Commit

Permalink
Merge branch 'main' into remove-some-redundant-async-state-machines-
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 7, 2023
2 parents 10d02e2 + c874285 commit 27777d0
Show file tree
Hide file tree
Showing 85 changed files with 311 additions and 313 deletions.
4 changes: 2 additions & 2 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public MemoryCacheProvider(IMemoryCache memoryCache)
_cache = memoryCache;
}

public (bool, object) TryGet(string key)
public (bool, object?) TryGet(string key)
{
bool cacheHit = _cache.TryGetValue(key, out var value);
return (cacheHit, value);
Expand Down Expand Up @@ -96,7 +96,7 @@ public void Put(string key, object value, Ttl ttl)
_cache.Set(key, value, options);
}

public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task<(bool, object?)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Benchmarks/Polly.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(BenchmarkFromNuGet)' != 'True' ">
<ProjectReference Include="..\Polly\Polly.csproj" />
Expand Down
22 changes: 12 additions & 10 deletions src/Polly.Benchmarks/Workloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ internal static async Task ActionInfiniteAsync(CancellationToken cancellationTok
}
}

internal static T Func<T>() => default;
internal static T Func<T>()
where T : struct
=> default;

internal static Task<T> FuncAsync<T>() => Task.FromResult<T>(default);
internal static Task<T> FuncAsync<T>()
where T : struct
=> Task.FromResult<T>(default);

internal static Task<T> FuncAsync<T>(CancellationToken cancellationToken) => Task.FromResult<T>(default);
internal static Task<T> FuncAsync<T>(CancellationToken cancellationToken)
where T : struct
=> Task.FromResult<T>(default);

internal static TResult FuncThrows<TResult, TException>()
where TException : Exception, new()
{
throw new TException();
}
=> throw new TException();

internal static Task<TResult> FuncThrowsAsync<TResult, TException>()
where TException : Exception, new()
{
throw new TException();
}
}
=> throw new TException();
}
2 changes: 1 addition & 1 deletion src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task Should_call_onBulkheadRejected_with_passed_context()
Func<Context, Task> onRejectedAsync = async ctx => { contextPassedToOnRejected = ctx; await TaskHelper.EmptyTask; };

using (var bulkhead = Policy.BulkheadAsync<int>(1, onRejectedAsync))
{
{
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
using (CancellationTokenSource cancellationSource = new CancellationTokenSource())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void Should_call_onBulkheadRejected_with_passed_context()
Action<Context> onRejected = ctx => { contextPassedToOnRejected = ctx; };

using (BulkheadPolicy<int> bulkhead = Policy.Bulkhead<int>(1, onRejected))
{
{
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
using (CancellationTokenSource cancellationSource = new CancellationTokenSource())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Polly.Specs.Caching;

public class AsyncSerializingCacheProviderSpecs
{
#region Object-to-TSerialized serializer
#region Object-to-TSerialized serializer

[Fact]
public void Single_generic_constructor_should_throw_on_no_wrapped_cache_provider()
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Specs/Caching/SerializingCacheProviderSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Polly.Specs.Caching;

public class SerializingCacheProviderSpecs
{
#region Object-to-TSerialized serializer
#region Object-to-TSerialized serializer

[Fact]
public void Single_generic_constructor_should_throw_on_no_wrapped_cache_provider()
Expand Down
46 changes: 23 additions & 23 deletions src/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public void Should_throw_if_timeslice_duration_is_less_than_resolution_of_circui
Action action = () => Policy
.Handle<DivideByZeroException>()
.AdvancedCircuitBreaker(
0.5,
TimeSpan.FromMilliseconds(20).Add(TimeSpan.FromTicks(-1)),
4,
0.5,
TimeSpan.FromMilliseconds(20).Add(TimeSpan.FromTicks(-1)),
4,
TimeSpan.FromSeconds(30));

action.Should().Throw<ArgumentOutOfRangeException>()
Expand Down Expand Up @@ -164,8 +164,8 @@ public void Should_initialise_to_closed_state()

#region Tests that are independent from health metrics implementation

// Tests on the AdvancedCircuitBreaker operation typically use a breaker:
// - with a failure threshold of >=50%,
// Tests on the AdvancedCircuitBreaker operation typically use a breaker:
// - with a failure threshold of >=50%,
// - and a throughput threshold of 4
// - across a ten-second period.
// These provide easy values for testing for failure and throughput thresholds each being met and non-met, in combination.
Expand Down Expand Up @@ -244,8 +244,8 @@ public void Should_not_open_circuit_if_exceptions_raised_are_not_one_of_the_spec

#region With sample duration higher than 199 ms so that multiple windows are used

// Tests on the AdvancedCircuitBreaker operation typically use a breaker:
// - with a failure threshold of >=50%,
// Tests on the AdvancedCircuitBreaker operation typically use a breaker:
// - with a failure threshold of >=50%,
// - and a throughput threshold of 4
// - across a ten-second period.
// These provide easy values for testing for failure and throughput thresholds each being met and non-met, in combination.
Expand Down Expand Up @@ -838,8 +838,8 @@ public void Should_not_open_circuit_if_failures_at_end_of_last_timeslice_and_fai
// Setting the time to just barely into the new timeslice
SystemClock.UtcNow = () => time.Add(samplingDuration);

// A third failure occurs just at the beginning of the new timeslice making
// the number of failures above the failure threshold. However, the throughput is
// A third failure occurs just at the beginning of the new timeslice making
// the number of failures above the failure threshold. However, the throughput is
// below the minimum threshold as to open the circuit.
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();
Expand Down Expand Up @@ -898,8 +898,8 @@ public void Should_open_circuit_if_failures_in_second_window_of_last_timeslice_a

#region With sample duration at 199 ms so that only a single window is used

// These tests on AdvancedCircuitBreaker operation typically use a breaker:
// - with a failure threshold of >=50%,
// These tests on AdvancedCircuitBreaker operation typically use a breaker:
// - with a failure threshold of >=50%,
// - and a throughput threshold of 4
// - across a 199ms period.
// These provide easy values for testing for failure and throughput thresholds each being met and non-met, in combination.
Expand Down Expand Up @@ -1258,7 +1258,7 @@ public void Should_not_open_circuit_if_failures_at_end_of_last_timeslice_below_f
// Setting the time to just barely into the new timeslice
SystemClock.UtcNow = () => time.Add(samplingDuration);

// This failure does not open the circuit, because a new duration should have
// This failure does not open the circuit, because a new duration should have
// started and with such low sampling duration, windows should not be used.
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();
Expand Down Expand Up @@ -1413,7 +1413,7 @@ public void Should_open_circuit_again_after_the_specified_duration_has_passed_if
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();
breaker.CircuitState.Should().Be(CircuitState.Open);

}

[Fact]
Expand Down Expand Up @@ -1483,7 +1483,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand Down Expand Up @@ -1522,7 +1522,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand Down Expand Up @@ -1569,7 +1569,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();

// exceptions raised, circuit is now open.
// exceptions raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand All @@ -1582,7 +1582,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_
TimeSpan testTimeoutToExposeDeadlocks = TimeSpan.FromSeconds(5);
using (ManualResetEvent permitSecondExecutionAttempt = new ManualResetEvent(false))
using (ManualResetEvent permitFirstExecutionEnd = new ManualResetEvent(false))
{
{
bool? firstDelegateExecutedInHalfOpenState = null;
bool? secondDelegateExecutedInHalfOpenState = null;
bool? secondDelegateRejectedInHalfOpenState = null;
Expand Down Expand Up @@ -1642,7 +1642,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_
firstExecution.Status.Should().Be(TaskStatus.RanToCompletion);
secondExecution.Status.Should().Be(TaskStatus.RanToCompletion);

// Assert:
// Assert:
// - First execution should have been permitted and executed under a HalfOpen state
// - Second overlapping execution in halfopen state should not have been permitted.
// - Second execution attempt should have been rejected with HalfOpen state as cause.
Expand Down Expand Up @@ -1674,7 +1674,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand All @@ -1688,7 +1688,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__
TimeSpan testTimeoutToExposeDeadlocks = TimeSpan.FromSeconds(5);
using (ManualResetEvent permitSecondExecutionAttempt = new ManualResetEvent(false))
using (ManualResetEvent permitFirstExecutionEnd = new ManualResetEvent(false))
{
{
bool? firstDelegateExecutedInHalfOpenState = null;
bool? secondDelegateExecutedInHalfOpenState = null;
bool? secondDelegateRejectedInHalfOpenState = null;
Expand Down Expand Up @@ -1750,7 +1750,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__
firstExecution.Status.Should().Be(TaskStatus.RanToCompletion);
secondExecution.Status.Should().Be(TaskStatus.RanToCompletion);

// Assert:
// Assert:
// - First execution should have been permitted and executed under a HalfOpen state
// - Second overlapping execution in halfopen state should have been permitted, one breakDuration later.
firstDelegateExecutedInHalfOpenState.Should().BeTrue();
Expand Down Expand Up @@ -1997,7 +1997,7 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub

onBreakCalled.Should().Be(1);

// call through circuit when already broken - should not retrigger onBreak
// call through circuit when already broken - should not retrigger onBreak
breaker.Invoking(x => x.RaiseException<DivideByZeroException>())
.Should().Throw<BrokenCircuitException>();

Expand Down Expand Up @@ -2491,7 +2491,7 @@ public void Should_open_circuit_with_timespan_maxvalue_if_manual_override_open()

var time = 1.January(2000);
SystemClock.UtcNow = () => time;

CircuitBreakerPolicy breaker = Policy
.Handle<DivideByZeroException>()
.AdvancedCircuitBreaker(
Expand Down
16 changes: 8 additions & 8 deletions src/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public async Task Should_only_allow_single_execution_on_first_entering_halfopen_
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand Down Expand Up @@ -355,13 +355,13 @@ public async Task Should_allow_single_execution_per_break_duration_in_halfopen_s
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
SystemClock.UtcNow = () => time.Add(durationOfBreak);
breaker.CircuitState.Should().Be(CircuitState.HalfOpen);


// OnActionPreExecute() should permit first execution.
breaker._breakerController.Invoking(c => c.OnActionPreExecute()).Should().NotThrow();
Expand Down Expand Up @@ -394,7 +394,7 @@ public async Task Should_only_allow_single_execution_on_first_entering_halfopen_
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand Down Expand Up @@ -469,7 +469,7 @@ await breaker.ExecuteAsync(async () =>
firstExecution.Status.Should().Be(TaskStatus.RanToCompletion);
secondExecution.Status.Should().Be(TaskStatus.RanToCompletion);

// Assert:
// Assert:
// - First execution should have been permitted and executed under a HalfOpen state
// - Second overlapping execution in halfopen state should not have been permitted.
// - Second execution attempt should have been rejected with HalfOpen state as cause.
Expand All @@ -493,7 +493,7 @@ public async Task Should_allow_single_execution_per_break_duration_in_halfopen_s
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<DivideByZeroException>();

// exception raised, circuit is now open.
// exception raised, circuit is now open.
breaker.CircuitState.Should().Be(CircuitState.Open);

// break duration passes, circuit now half open
Expand Down Expand Up @@ -571,7 +571,7 @@ await breaker.ExecuteAsync(async () =>
firstExecution.Status.Should().Be(TaskStatus.RanToCompletion);
secondExecution.Status.Should().Be(TaskStatus.RanToCompletion);

// Assert:
// Assert:
// - First execution should have been permitted and executed under a HalfOpen state
// - Second overlapping execution in halfopen state should have been permitted, one breakDuration later.
firstDelegateExecutedInHalfOpenState.Should().BeTrue();
Expand Down Expand Up @@ -772,7 +772,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
breaker.CircuitState.Should().Be(CircuitState.Open);
onBreakCalled.Should().Be(1);

// call through circuit when already broken - should not retrigger onBreak
// call through circuit when already broken - should not retrigger onBreak
await breaker.Awaiting(x => x.RaiseExceptionAsync<DivideByZeroException>())
.Should().ThrowAsync<BrokenCircuitException>();

Expand Down
Loading

0 comments on commit 27777d0

Please sign in to comment.