Skip to content

Commit

Permalink
Drop Task-based execute methods (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored May 18, 2023
1 parent a4343b7 commit ee2a4f5
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 481 deletions.
8 changes: 4 additions & 4 deletions src/Polly.Core.Benchmarks/Benchmarks/HedgingBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public void Setup()

[Benchmark(Baseline = true)]
public async ValueTask Hedging_Primary()
=> await _strategy!.ExecuteValueTaskAsync(static _ => new ValueTask<string>("primary")).ConfigureAwait(false);
=> await _strategy!.ExecuteAsync(static _ => new ValueTask<string>("primary")).ConfigureAwait(false);

[Benchmark]
public async ValueTask Hedging_Secondary()
=> await _strategy!.ExecuteValueTaskAsync(static _ => new ValueTask<string>(Helper.Failure)).ConfigureAwait(false);
=> await _strategy!.ExecuteAsync(static _ => new ValueTask<string>(Helper.Failure)).ConfigureAwait(false);

[Benchmark]
public async ValueTask Hedging_Primary_AsyncWork()
=> await _strategy!.ExecuteValueTaskAsync(
=> await _strategy!.ExecuteAsync(
static async _ =>
{
await Task.Yield();
Expand All @@ -33,7 +33,7 @@ public async ValueTask Hedging_Primary_AsyncWork()

[Benchmark]
public async ValueTask Hedging_Secondary_AsyncWork()
=> await _strategy!.ExecuteValueTaskAsync(
=> await _strategy!.ExecuteAsync(
static async _ =>
{
await Task.Yield();
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core.Benchmarks/Internals/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static async ValueTask ExecuteAsync(this object obj, PollyVersion version
await ((IAsyncPolicy<int>)obj).ExecuteAsync(static _ => Task.FromResult(999), CancellationToken.None).ConfigureAwait(false);
return;
case PollyVersion.V8:
await ((ResilienceStrategy)obj).ExecuteValueTaskAsync(static _ => new ValueTask<int>(999), CancellationToken.None).ConfigureAwait(false);
await ((ResilienceStrategy)obj).ExecuteAsync(static _ => new ValueTask<int>(999), CancellationToken.None).ConfigureAwait(false);
return;
}

Expand Down
34 changes: 17 additions & 17 deletions src/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task Execute_CancellationRequested_Throws()
var strategy = Create();
_cts.Cancel();

await strategy.Invoking(s => s.ExecuteAsync(_ => Task.FromResult(Success), _cts.Token))
await strategy.Invoking(s => s.ExecuteAsync(_ => new ValueTask<string>(Success), _cts.Token).AsTask())
.Should()
.ThrowAsync<OperationCanceledException>();
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public async Task ExecuteAsync_EnsureOriginalCancellationTokenRestored()
var strategy = Create();

// act
var result = await strategy.ExecuteAsync((_, _) => Task.FromResult("primary"), primaryContext, "dummy");
var result = await strategy.ExecuteAsync((_, _) => new ValueTask<string>("primary"), primaryContext, "dummy");

// assert
primaryContext.CancellationToken.Should().Be(cancellationSource.Token);
Expand Down Expand Up @@ -349,7 +349,7 @@ public async Task ExecuteAsync_EnsurePropertiesConsistency(bool primaryFails)
return primaryFails ? Failure : Success;
},
primaryContext,
"state");
"state").AsTask();

// assert

Expand Down Expand Up @@ -394,7 +394,7 @@ public async Task ExecuteAsync_Primary_CustomPropertiesAvailable()
primaryContext.Properties.TryGetValue(key2, out var val).Should().BeTrue();
val.Should().Be("my-value-2");
context.Properties.Set(key, "my-value");
return Task.FromResult("primary");
return new ValueTask<string>("primary");
},
primaryContext, "dummy");

Expand Down Expand Up @@ -426,7 +426,7 @@ public async Task ExecuteAsync_Secondary_CustomPropertiesAvailable()
var strategy = Create();

// act
var result = await strategy.ExecuteAsync((_, _) => Task.FromResult(Failure), primaryContext, "state");
var result = await strategy.ExecuteAsync((_, _) => new ValueTask<string>(Failure), primaryContext, "state");

// assert
result.Should().Be(Success);
Expand Down Expand Up @@ -513,7 +513,7 @@ public async Task ExecuteAsync_EnsureBackgroundWorkInSuccessfulCallNotCancelled(

cts.Cancel();

async Task<string> SlowTask(CancellationToken cancellationToken)
async ValueTask<string> SlowTask(CancellationToken cancellationToken)
{
var delay = Task.Delay(TimeSpan.FromDays(1), cancellationToken);
backgroundTasks.Add(delay);
Expand All @@ -535,7 +535,7 @@ public async void ExecuteAsync_ZeroHedgingDelay_EnsureAllTasksSpawnedAtOnce()
// arrange
int executions = 0;
using var allExecutionsReached = new ManualResetEvent(false);
ConfigureHedging(context => Execute(context.CancellationToken));
ConfigureHedging(context => Execute(context.CancellationToken).AsTask());
_options.HedgingDelay = TimeSpan.Zero;

// act
Expand All @@ -546,7 +546,7 @@ public async void ExecuteAsync_ZeroHedgingDelay_EnsureAllTasksSpawnedAtOnce()
_timeProvider.Advance(LongDelay);
await task;

async Task<string> Execute(CancellationToken token)
async ValueTask<string> Execute(CancellationToken token)
{
if (Interlocked.Increment(ref executions) == _options.MaxHedgedAttempts)
{
Expand All @@ -565,15 +565,15 @@ public void ExecuteAsync_InfiniteHedgingDelay_EnsureNoConcurrentExecutions()
bool executing = false;
int executions = 0;
using var allExecutions = new ManualResetEvent(true);
ConfigureHedging(context => Execute(context.CancellationToken));
ConfigureHedging(context => Execute(context.CancellationToken).AsTask());

// act
var pending = Create().ExecuteAsync(Execute, _cts.Token);

// assert
Assert.True(allExecutions.WaitOne(AssertTimeout));

async Task<string> Execute(CancellationToken token)
async ValueTask<string> Execute(CancellationToken token)
{
if (executing)
{
Expand Down Expand Up @@ -625,7 +625,7 @@ public async Task ExecuteAsync_ExceptionsHandled_ShouldThrowAnyException()
});

var strategy = Create();
await strategy.Invoking(s => s.ExecuteAsync<string>(_ => throw new InvalidCastException())).Should().ThrowAsync<BadImageFormatException>();
await strategy.Invoking(s => s.ExecuteAsync<string>(_ => throw new InvalidCastException()).AsTask()).Should().ThrowAsync<BadImageFormatException>();
attempts.Should().Be(3);
}

Expand Down Expand Up @@ -656,7 +656,7 @@ public async Task ExecuteAsync_ExceptionsHandled_ShouldThrowLastException()
});

var strategy = Create();
await strategy.Invoking(s => s.ExecuteAsync<string>(_ => throw new InvalidCastException())).Should().ThrowAsync<InvalidCastException>();
await strategy.Invoking(s => s.ExecuteAsync<string>(_ => throw new InvalidCastException()).AsTask()).Should().ThrowAsync<InvalidCastException>();
attempts.Should().Be(3);
}

Expand All @@ -676,7 +676,7 @@ public async Task ExecuteAsync_PrimaryExceptionNotHandled_Rethrow()
});

var strategy = Create();
await strategy.Invoking(s => s.ExecuteAsync<string>(_ => throw new InvalidCastException())).Should().ThrowAsync<InvalidCastException>();
await strategy.Invoking(s => s.ExecuteAsync<string>(_ => throw new InvalidCastException()).AsTask()).Should().ThrowAsync<InvalidCastException>();
attempts.Should().Be(0);
}

Expand Down Expand Up @@ -752,12 +752,12 @@ public async Task ExecuteAsync_EnsureExceptionStackTracePreserved()

var strategy = Create();

var exception = await strategy.Invoking(s => s.ExecuteAsync(PrimaryTaskThatThrowsError)).Should().ThrowAsync<InvalidOperationException>();
var exception = await strategy.Invoking(s => s.ExecuteAsync(PrimaryTaskThatThrowsError).AsTask()).Should().ThrowAsync<InvalidOperationException>();

exception.WithMessage("Forced Error");
exception.And.StackTrace.Should().Contain(nameof(PrimaryTaskThatThrowsError));

static Task<string> PrimaryTaskThatThrowsError(CancellationToken cancellationToken) => throw new InvalidOperationException("Forced Error");
static ValueTask<string> PrimaryTaskThatThrowsError(CancellationToken cancellationToken) => throw new InvalidOperationException("Forced Error");
}

[Fact]
Expand All @@ -776,7 +776,7 @@ public async Task ExecuteAsync_EnsureOnHedgingCalled()
});

var strategy = Create();
await strategy.ExecuteAsync(_ => Task.FromResult(Failure));
await strategy.ExecuteAsync(_ => new ValueTask<string>(Failure));

attempts.Should().HaveCount(_options.MaxHedgedAttempts);
attempts.Should().BeInAscendingOrder();
Expand All @@ -795,7 +795,7 @@ public async Task ExecuteAsync_EnsureOnHedgingTelemetry()
});

var strategy = Create();
await strategy.ExecuteAsync((_, _) => Task.FromResult(Failure), context, "state");
await strategy.ExecuteAsync((_, _) => new ValueTask<string>(Failure), context, "state");

context.ResilienceEvents.Should().HaveCount(_options.MaxHedgedAttempts);
context.ResilienceEvents.Select(v => v.EventName).Distinct().Should().ContainSingle("OnHedging");
Expand Down
8 changes: 4 additions & 4 deletions src/Polly.Core.Tests/Hedging/PrimaryStringTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ internal class PrimaryStringTasks

public PrimaryStringTasks(TimeProvider timeProvider) => _timeProvider = timeProvider;

public static Task<string> InstantTask()
public static ValueTask<string> InstantTask()
{
return Task.FromResult(InstantTaskResult);
return new ValueTask<string>(InstantTaskResult);
}

public async Task<string> FastTask(CancellationToken token)
public async ValueTask<string> FastTask(CancellationToken token)
{
await _timeProvider.Delay(TimeSpan.FromMilliseconds(10), token);
return FastTaskResult;
}

public async Task<string> SlowTask(CancellationToken token)
public async ValueTask<string> SlowTask(CancellationToken token)
{
await _timeProvider.Delay(TimeSpan.FromDays(1), token);
return SlowTaskResult;
Expand Down
82 changes: 0 additions & 82 deletions src/Polly.Core.Tests/ResilienceStrategyTests.Async.Task.cs

This file was deleted.

90 changes: 0 additions & 90 deletions src/Polly.Core.Tests/ResilienceStrategyTests.Async.TaskT.cs

This file was deleted.

Loading

0 comments on commit ee2a4f5

Please sign in to comment.