Skip to content

Commit

Permalink
Add Async suffix to some APIs (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Apr 14, 2023
1 parent e5916bc commit 1296fe1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RetryResilienceStrategyBuilderExtensionsTests
{
var args = new RetryDelayArguments(ResilienceContext.Get(), 8, TimeSpan.Zero);
strategy.DelayGenerator!.Generate(new Polly.Strategy.Outcome<bool>(new InvalidOperationException()), args).Result.Should().Be(TimeSpan.FromMilliseconds(8));
strategy.DelayGenerator!.GenerateAsync(new Polly.Strategy.Outcome<bool>(new InvalidOperationException()), args).Result.Should().Be(TimeSpan.FromMilliseconds(8));
});
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core.Tests/Strategy/OutcomeEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ public void Register_DifferentResultType_NotInvoked()

private static void InvokeHandler<T>(OutcomeEvent<TestArguments> sut, Outcome<T> outcome)
{
sut.CreateHandler()!.Handle(outcome, new TestArguments()).AsTask().Wait();
sut.CreateHandler()!.HandleAsync(outcome, new TestArguments()).AsTask().Wait();
}
}
2 changes: 1 addition & 1 deletion src/Polly.Core.Tests/Strategy/OutcomeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void AddResultHandlers_DifferentResultType_NotInvoked()

private static void InvokeHandler<T>(OutcomeGenerator<TestArguments, GeneratedValue> sut, Outcome<T> outcome, GeneratedValue expectedResult)
{
CreateHandler(sut)!.Generate(outcome, new TestArguments()).AsTask().Result.Should().Be(expectedResult);
CreateHandler(sut)!.GenerateAsync(outcome, new TestArguments()).AsTask().Result.Should().Be(expectedResult);
}

private static OutcomeGenerator<TestArguments, GeneratedValue>.Handler? CreateHandler(OutcomeGenerator<TestArguments, GeneratedValue> generator)
Expand Down
10 changes: 5 additions & 5 deletions src/Polly.Core.Tests/Strategy/OutcomePredicateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,25 +316,25 @@ public void AddResultHandlers_StopOnTrue()
private static void InvokeHandler<TResult>(OutcomePredicate<TestArguments> sut, Exception exception, bool expectedResult)
{
var args = new TestArguments();
sut.CreateHandler()!.ShouldHandle(new Outcome<TResult>(exception), args).AsTask().Result.Should().Be(expectedResult);
sut.CreateHandler()!.ShouldHandleAsync(new Outcome<TResult>(exception), args).AsTask().Result.Should().Be(expectedResult);
}

private static void InvokeHandler(OutcomePredicate<TestArguments> sut, Exception exception, bool expectedResult)
{
var args = new TestArguments();

sut.CreateHandler()!.ShouldHandle(new Outcome<object>(exception), args).AsTask().Result.Should().Be(expectedResult);
sut.CreateHandler()!.ShouldHandleAsync(new Outcome<object>(exception), args).AsTask().Result.Should().Be(expectedResult);

// again with void result
sut.CreateHandler()!.ShouldHandle(new Outcome<VoidResult>(exception), args).AsTask().Result.Should().Be(expectedResult);
sut.CreateHandler()!.ShouldHandleAsync(new Outcome<VoidResult>(exception), args).AsTask().Result.Should().Be(expectedResult);

// again with result
sut.HandleResult(12345);
sut.CreateHandler()!.ShouldHandle(new Outcome<int>(10), args).AsTask().Result.Should().Be(false);
sut.CreateHandler()!.ShouldHandleAsync(new Outcome<int>(10), args).AsTask().Result.Should().Be(false);
}

private static void InvokeResultHandler<T>(OutcomePredicate<TestArguments> sut, T result, bool expectedResult)
{
sut.CreateHandler()!.ShouldHandle<T>(new Outcome<T>(result), new TestArguments()).AsTask().Result.Should().Be(expectedResult);
sut.CreateHandler()!.ShouldHandleAsync<T>(new Outcome<T>(result), new TestArguments()).AsTask().Result.Should().Be(expectedResult);
}
}
8 changes: 4 additions & 4 deletions src/Polly.Core/Retry/RetryResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public RetryResilienceStrategy(RetryStrategyOptions options, TimeProvider timePr
var result = await callback(context, state).ConfigureAwait(context.ContinueOnCapturedContext);
outcome = new Outcome<TResult>(result);

if (IsLastAttempt(attempt) || !await ShouldRetry.ShouldHandle(outcome, new ShouldRetryArguments(context, attempt)).ConfigureAwait(context.ContinueOnCapturedContext))
if (IsLastAttempt(attempt) || !await ShouldRetry.ShouldHandleAsync(outcome, new ShouldRetryArguments(context, attempt)).ConfigureAwait(context.ContinueOnCapturedContext))
{
return result;
}
Expand All @@ -63,7 +63,7 @@ public RetryResilienceStrategy(RetryStrategyOptions options, TimeProvider timePr
{
outcome = new Outcome<TResult>(e);

if (IsLastAttempt(attempt) || !await ShouldRetry.ShouldHandle(outcome, new ShouldRetryArguments(context, attempt)).ConfigureAwait(context.ContinueOnCapturedContext))
if (IsLastAttempt(attempt) || !await ShouldRetry.ShouldHandleAsync(outcome, new ShouldRetryArguments(context, attempt)).ConfigureAwait(context.ContinueOnCapturedContext))
{
throw;
}
Expand All @@ -72,7 +72,7 @@ public RetryResilienceStrategy(RetryStrategyOptions options, TimeProvider timePr
var delay = RetryHelper.GetRetryDelay(BackoffType, attempt, BaseDelay);
if (DelayGenerator != null)
{
var newDelay = await DelayGenerator.Generate(outcome, new RetryDelayArguments(context, attempt, delay)).ConfigureAwait(false);
var newDelay = await DelayGenerator.GenerateAsync(outcome, new RetryDelayArguments(context, attempt, delay)).ConfigureAwait(false);
if (RetryHelper.IsValidDelay(newDelay))
{
delay = newDelay;
Expand All @@ -85,7 +85,7 @@ public RetryResilienceStrategy(RetryStrategyOptions options, TimeProvider timePr

if (OnRetry != null)
{
await OnRetry.Handle(outcome, args).ConfigureAwait(context.ContinueOnCapturedContext);
await OnRetry.HandleAsync(outcome, args).ConfigureAwait(context.ContinueOnCapturedContext);
}

if (delay > TimeSpan.Zero)
Expand Down
8 changes: 4 additions & 4 deletions src/Polly.Core/Strategy/OutcomeEvent.Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private protected Handler()
/// <param name="outcome">The operation outcome.</param>
/// <param name="args">The arguments passed to the registered callbacks.</param>
/// <returns>The <see cref="ValueTask"/>.</returns>
public abstract ValueTask Handle<TResult>(Outcome<TResult> outcome, TArgs args);
public abstract ValueTask HandleAsync<TResult>(Outcome<TResult> outcome, TArgs args);
}

private sealed class TypeHandler : Handler
Expand All @@ -36,7 +36,7 @@ public TypeHandler(Type type, List<object> callbacks)
_callbacks = callbacks.ToArray();
}

public override ValueTask Handle<TResult>(Outcome<TResult> outcome, TArgs args)
public override ValueTask HandleAsync<TResult>(Outcome<TResult> outcome, TArgs args)
{
if (typeof(TResult) != _type)
{
Expand Down Expand Up @@ -76,11 +76,11 @@ private sealed class TypesHandler : Handler
public TypesHandler(IEnumerable<KeyValuePair<Type, List<object>>> callbacks)
=> _handlers = callbacks.ToDictionary(v => v.Key, v => new TypeHandler(v.Key, v.Value));

public override ValueTask Handle<TResult>(Outcome<TResult> outcome, TArgs args)
public override ValueTask HandleAsync<TResult>(Outcome<TResult> outcome, TArgs args)
{
if (_handlers.TryGetValue(typeof(TResult), out var handler))
{
return handler.Handle(outcome, args);
return handler.HandleAsync(outcome, args);
}

return default;
Expand Down
10 changes: 5 additions & 5 deletions src/Polly.Core/Strategy/OutcomeGenerator.Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private protected Handler(TValue defaultValue, Predicate<TValue> isValid)
/// <param name="outcome">The operation outcome.</param>
/// <param name="args">The arguments.</param>
/// <returns>The result of the handle operation.</returns>
public abstract ValueTask<TValue> Generate<TResult>(Outcome<TResult> outcome, TArgs args);
public abstract ValueTask<TValue> GenerateAsync<TResult>(Outcome<TResult> outcome, TArgs args);
}

private sealed class TypeHandler : Handler
Expand All @@ -47,7 +47,7 @@ private sealed class TypeHandler : Handler
_generator = generator;
}

public override async ValueTask<TValue> Generate<TResult>(Outcome<TResult> outcome, TArgs args)
public override async ValueTask<TValue> GenerateAsync<TResult>(Outcome<TResult> outcome, TArgs args)
{
TValue value = DefaultValue;

Expand Down Expand Up @@ -80,11 +80,11 @@ private sealed class TypesHandler : Handler
: base(defaultValue, isValid)
=> _generators = generators.ToDictionary(v => v.Key, v => new TypeHandler(v.Key, v.Value, defaultValue, isValid));

public override async ValueTask<TValue> Generate<TResult>(Outcome<TResult> outcome, TArgs args)
public override async ValueTask<TValue> GenerateAsync<TResult>(Outcome<TResult> outcome, TArgs args)
{
if (_generators.TryGetValue(typeof(TResult), out var handler))
{
var value = await handler.Generate(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext);
var value = await handler.GenerateAsync(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext);
if (IsValid(value))
{
return value;
Expand All @@ -93,7 +93,7 @@ public override async ValueTask<TValue> Generate<TResult>(Outcome<TResult> outco

if (_generators.TryGetValue(typeof(AnyResult), out handler))
{
return await handler.Generate(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext);
return await handler.GenerateAsync(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext);
}

return DefaultValue;
Expand Down
10 changes: 5 additions & 5 deletions src/Polly.Core/Strategy/OutcomePredicate.Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private protected Handler()
/// <param name="outcome">The operation outcome.</param>
/// <param name="args">The arguments.</param>
/// <returns>The result of the handle operation.</returns>
public abstract ValueTask<bool> ShouldHandle<TResult>(Outcome<TResult> outcome, TArgs args);
public abstract ValueTask<bool> ShouldHandleAsync<TResult>(Outcome<TResult> outcome, TArgs args);
}

private sealed class TypeHandler : Handler
Expand All @@ -36,7 +36,7 @@ public TypeHandler(Type type, object predicate)
_predicate = predicate;
}

public override ValueTask<bool> ShouldHandle<TResult>(Outcome<TResult> outcome, TArgs args)
public override ValueTask<bool> ShouldHandleAsync<TResult>(Outcome<TResult> outcome, TArgs args)
{
// special case for exception-based callbacks
if (!outcome.HasResult && _type == typeof(ExceptionOutcome))
Expand Down Expand Up @@ -67,16 +67,16 @@ private sealed class TypesHandler : Handler
public TypesHandler(IEnumerable<KeyValuePair<Type, object>> predicates)
=> _predicates = predicates.ToDictionary(v => v.Key, v => new TypeHandler(v.Key, v.Value));

public override async ValueTask<bool> ShouldHandle<TResult>(Outcome<TResult> outcome, TArgs args)
public override async ValueTask<bool> ShouldHandleAsync<TResult>(Outcome<TResult> outcome, TArgs args)
{
if (_predicates.TryGetValue(typeof(TResult), out var handler) && await handler.ShouldHandle(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext))
if (_predicates.TryGetValue(typeof(TResult), out var handler) && await handler.ShouldHandleAsync(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext))
{
return true;
}

if (!outcome.HasResult && _predicates.TryGetValue(typeof(ExceptionOutcome), out var exceptionHandler))
{
return await exceptionHandler.ShouldHandle(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext);
return await exceptionHandler.ShouldHandleAsync(outcome, args).ConfigureAwait(args.Context.ContinueOnCapturedContext);
}

return false;
Expand Down

0 comments on commit 1296fe1

Please sign in to comment.