Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Task-based execute methods #1194

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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