Skip to content

Commit

Permalink
expression body members
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Dec 13, 2022
1 parent d05f172 commit a6f49c7
Show file tree
Hide file tree
Showing 38 changed files with 154 additions and 316 deletions.
6 changes: 2 additions & 4 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ 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)
{
return Task.FromResult(TryGet(key));
}
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
Task.FromResult(TryGet(key));

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Expand Down
8 changes: 2 additions & 6 deletions src/Polly.Benchmarks/Workloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ internal static async Task ActionInfiniteAsync(CancellationToken cancellationTok
internal static Task<T> FuncAsync<T>(CancellationToken cancellationToken) => Task.FromResult<T>(default);

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

internal static Task<TResult> FuncThrowsAsync<TResult, TException>()
where TException : Exception, new()
{
where TException : Exception, new() =>
throw new TException();
}
}
12 changes: 4 additions & 8 deletions src/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,11 @@ public void Should_call_onBulkheadRejected_with_passed_context()

#region Bulkhead behaviour

protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions)
{
return Policy.BulkheadAsync(maxParallelization, maxQueuingActions);
}
protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions) =>
Policy.BulkheadAsync(maxParallelization, maxQueuingActions);

protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action)
{
return action.ExecuteOnBulkheadAsync((AsyncBulkheadPolicy)bulkhead);
}
protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action) =>
action.ExecuteOnBulkheadAsync((AsyncBulkheadPolicy)bulkhead);

#endregion

Expand Down
12 changes: 4 additions & 8 deletions src/Polly.Specs/Bulkhead/BulkheadSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,11 @@ public void Should_call_onBulkheadRejected_with_passed_context()

#region Bulkhead behaviour

protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions)
{
return Policy.Bulkhead(maxParallelization, maxQueuingActions);
}
protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions) =>
Policy.Bulkhead(maxParallelization, maxQueuingActions);

protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action)
{
return action.ExecuteOnBulkhead((BulkheadPolicy) bulkhead);
}
protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action) =>
action.ExecuteOnBulkhead((BulkheadPolicy) bulkhead);

#endregion

Expand Down
13 changes: 4 additions & 9 deletions src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public void Should_call_onBulkheadRejected_with_passed_context()
contextPassedToOnRejected.Should().NotBeNull();
contextPassedToOnRejected.OperationKey.Should().Be(operationKey);
contextPassedToOnRejected.Should().BeSameAs(contextPassedToExecute);

}
}

Expand All @@ -94,15 +93,11 @@ public void Should_call_onBulkheadRejected_with_passed_context()

#region Bulkhead behaviour

protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions)
{
return Policy.BulkheadAsync<ResultPrimitive>(maxParallelization, maxQueuingActions);
}
protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions) =>
Policy.BulkheadAsync<ResultPrimitive>(maxParallelization, maxQueuingActions);

protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action)
{
return action.ExecuteOnBulkheadAsync<ResultPrimitive>((AsyncBulkheadPolicy<ResultPrimitive>)bulkhead);
}
protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action) =>
action.ExecuteOnBulkheadAsync<ResultPrimitive>((AsyncBulkheadPolicy<ResultPrimitive>)bulkhead);

#endregion

Expand Down
12 changes: 4 additions & 8 deletions src/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ public void Should_call_onBulkheadRejected_with_passed_context()

#region Bulkhead behaviour

protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions)
{
return Policy.Bulkhead<ResultPrimitive>(maxParallelization, maxQueuingActions);
}
protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQueuingActions) =>
Policy.Bulkhead<ResultPrimitive>(maxParallelization, maxQueuingActions);

protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action)
{
return action.ExecuteOnBulkhead<ResultPrimitive>((BulkheadPolicy<ResultPrimitive>) bulkhead);
}
protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action) =>
action.ExecuteOnBulkhead<ResultPrimitive>((BulkheadPolicy<ResultPrimitive>) bulkhead);

#endregion
}
2 changes: 1 addition & 1 deletion src/Polly.Specs/Helpers/Bulkhead/TraceableAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TraceableAction : IDisposable

public TraceableActionStatus Status
{
get { return _status; }
get => _status;
set
{
_status = value;
Expand Down
10 changes: 3 additions & 7 deletions src/Polly.Specs/Helpers/Caching/StubCacheKeyStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ internal class StubCacheKeyStrategy : ICacheKeyStrategy
{
private readonly Func<Context, string> strategy;

public StubCacheKeyStrategy(Func<Context, string> strategy)
{
public StubCacheKeyStrategy(Func<Context, string> strategy) =>
this.strategy = strategy;
}

public string GetCacheKey(Context context)
{
return strategy(context);
}
public string GetCacheKey(Context context) =>
strategy(context);
}
12 changes: 4 additions & 8 deletions src/Polly.Specs/Helpers/Caching/StubCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ public CacheItem(object value, Ttl ttl)
return (false, null);
}

public void Put(string key, object value, Ttl ttl)
{
cachedValues[key] = new CacheItem(value, ttl);
}
public void Put(string key, object value, Ttl ttl) =>
cachedValues[key] = new(value, ttl);

#region Naive async-over-sync implementation

// Intentionally naive async-over-sync implementation. Its purpose is to be the simplest thing to support tests of the CachePolicyAsync and CacheEngineAsync, not to be a usable implementation of IAsyncCacheProvider.
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
Task.FromResult(TryGet(key));

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Polly.Specs/Helpers/Caching/StubErroringCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public void Put(string key, object value, Ttl ttl)
#region Naive async-over-sync implementation

// Intentionally naive async-over-sync implementation. Its purpose is to be the simplest thing to support tests of the CachePolicyAsync and CacheEngineAsync, not to be a usable implementation of IAsyncCacheProvider.
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
Task.FromResult(TryGet(key));

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Expand Down
7 changes: 2 additions & 5 deletions src/Polly.Specs/Helpers/ContextualPolicyExtensionsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public static class ContextualPolicyExtensionsAsync
}, contextData, cancellationToken);
}

public static Task RaiseExceptionAsync<TException>(this AsyncPolicy policy, IDictionary<string, object> contextData, Action<TException, int> configureException = null, CancellationToken cancellationToken = default) where TException : Exception, new()
{
return policy.RaiseExceptionAsync(1, contextData, configureException, cancellationToken);
}

public static Task RaiseExceptionAsync<TException>(this AsyncPolicy policy, IDictionary<string, object> contextData, Action<TException, int> configureException = null, CancellationToken cancellationToken = default) where TException : Exception, new() =>
policy.RaiseExceptionAsync(1, contextData, configureException, cancellationToken);
}
12 changes: 4 additions & 8 deletions src/Polly.Specs/Helpers/ContextualPolicyTResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ public static class ContextualPolicyTResultExtensions
{
public static TResult RaiseResultSequence<TResult>(this Policy<TResult> policy,
IDictionary<string, object> contextData,
params TResult[] resultsToRaise)
{
return policy.RaiseResultSequence(contextData, resultsToRaise.ToList());
}
params TResult[] resultsToRaise) =>
policy.RaiseResultSequence(contextData, resultsToRaise.ToList());

public static TResult RaiseResultSequence<TResult>(this Policy<TResult> policy,
IDictionary<string, object> contextData,
Expand All @@ -32,10 +30,8 @@ public static TResult RaiseResultSequence<TResult>(this Policy<TResult> policy,

public static PolicyResult<TResult> RaiseResultSequenceOnExecuteAndCapture<TResult>(this Policy<TResult> policy,
IDictionary<string, object> contextData,
params TResult[] resultsToRaise)
{
return policy.RaiseResultSequenceOnExecuteAndCapture(contextData, resultsToRaise.ToList());
}
params TResult[] resultsToRaise) =>
policy.RaiseResultSequenceOnExecuteAndCapture(contextData, resultsToRaise.ToList());

public static PolicyResult<TResult> RaiseResultSequenceOnExecuteAndCapture<TResult>(this Policy<TResult> policy,
IDictionary<string, object> contextData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ public static class ContextualPolicyTResultExtensionsAsync

public static Task<TResult> RaiseResultSequenceAsync<TResult>(this AsyncPolicy<TResult> policy,
IDictionary<string, object> contextData,
params TResult[] resultsToRaise)
{
return policy.RaiseResultSequenceAsync(contextData, CancellationToken.None, resultsToRaise.ToList());
}
params TResult[] resultsToRaise) =>
policy.RaiseResultSequenceAsync(contextData, CancellationToken.None, resultsToRaise.ToList());

public static Task<TResult> RaiseResultSequenceAsync<TResult>(this AsyncPolicy<TResult> policy, IDictionary<string, object> contextData, CancellationToken cancellationToken, IEnumerable<TResult> resultsToRaise)
{
Expand All @@ -31,10 +29,8 @@ public static Task<TResult> RaiseResultSequenceAsync<TResult>(this AsyncPolicy<T
}, contextData, cancellationToken);
}

public static Task<PolicyResult<TResult>> RaiseResultSequenceOnExecuteAndCaptureAsync<TResult>(this AsyncPolicy<TResult> policy, IDictionary<string, object> contextData, params TResult[] resultsToRaise)
{
return policy.RaiseResultSequenceOnExecuteAndCaptureAsync(contextData, resultsToRaise.ToList());
}
public static Task<PolicyResult<TResult>> RaiseResultSequenceOnExecuteAndCaptureAsync<TResult>(this AsyncPolicy<TResult> policy, IDictionary<string, object> contextData, params TResult[] resultsToRaise) =>
policy.RaiseResultSequenceOnExecuteAndCaptureAsync(contextData, resultsToRaise.ToList());

public static Task<PolicyResult<TResult>> RaiseResultSequenceOnExecuteAndCaptureAsync<TResult>(this AsyncPolicy<TResult> policy, IDictionary<string, object> contextData, IEnumerable<TResult> resultsToRaise)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ internal AddBehaviourIfHandlePolicy(
_behaviourIfHandle = behaviourIfHandle ?? throw new ArgumentNullException(nameof(behaviourIfHandle));
}

protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
return AddBehaviourIfHandleEngine.Implementation(
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
AddBehaviourIfHandleEngine.Implementation(
ExceptionPredicates,
ResultPredicates,
_behaviourIfHandle,
action,
context,
cancellationToken
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ internal class AsyncAddBehaviourIfHandlePolicy<TResult> : AsyncPolicy<TResult>
internal AsyncAddBehaviourIfHandlePolicy(
Func<DelegateResult<TResult>, Task> behaviourIfHandle,
PolicyBuilder<TResult> policyBuilder)
: base(policyBuilder)
{
: base(policyBuilder) =>
_behaviourIfHandle = behaviourIfHandle ?? throw new ArgumentNullException(nameof(behaviourIfHandle));

}

protected override Task<TResult> ImplementationAsync(Func<Context, System.Threading.CancellationToken, Task<TResult>> action, Context context, System.Threading.CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
return AsyncAddBehaviourIfHandleEngine.ImplementationAsync(
bool continueOnCapturedContext) =>
AsyncAddBehaviourIfHandleEngine.ImplementationAsync(
ExceptionPredicates,
ResultPredicates,
_behaviourIfHandle,
Expand All @@ -55,5 +51,4 @@ protected override Task<TResult> ImplementationAsync(Func<Context, System.Thread
cancellationToken,
continueOnCapturedContext
);
}
}
32 changes: 10 additions & 22 deletions src/Polly.Specs/Helpers/Custom/PreExecute/AsyncPreExecutePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,28 @@ internal class AsyncPreExecutePolicy : AsyncPolicy
{
private Func<Task> _preExecute;

public static AsyncPreExecutePolicy CreateAsync(Func<Task> preExecute)
{
return new AsyncPreExecutePolicy(preExecute);
}
public static AsyncPreExecutePolicy CreateAsync(Func<Task> preExecute) =>
new(preExecute);

internal AsyncPreExecutePolicy(Func<Task> preExecute)
{
internal AsyncPreExecutePolicy(Func<Task> preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
}

protected override Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
return AsyncPreExecuteEngine.ImplementationAsync(_preExecute, action, context, cancellationToken, continueOnCapturedContext);
}
bool continueOnCapturedContext) =>
AsyncPreExecuteEngine.ImplementationAsync(_preExecute, action, context, cancellationToken, continueOnCapturedContext);
}

internal class AsyncPreExecutePolicy<TResult> : AsyncPolicy<TResult>
{
private Func<Task> _preExecute;

public static AsyncPreExecutePolicy<TResult> CreateAsync(Func<Task> preExecute)
{
return new AsyncPreExecutePolicy<TResult>(preExecute);
}
public static AsyncPreExecutePolicy<TResult> CreateAsync(Func<Task> preExecute) =>
new AsyncPreExecutePolicy<TResult>(preExecute);

internal AsyncPreExecutePolicy(Func<Task> preExecute)
{
internal AsyncPreExecutePolicy(Func<Task> preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
}

protected override Task<TResult> ImplementationAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
return AsyncPreExecuteEngine.ImplementationAsync(_preExecute, action, context, cancellationToken, continueOnCapturedContext);
}
bool continueOnCapturedContext) =>
AsyncPreExecuteEngine.ImplementationAsync(_preExecute, action, context, cancellationToken, continueOnCapturedContext);
}
32 changes: 10 additions & 22 deletions src/Polly.Specs/Helpers/Custom/PreExecute/PreExecutePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,26 @@ internal class PreExecutePolicy : Policy
{
private Action _preExecute;

public static PreExecutePolicy Create(Action preExecute)
{
return new PreExecutePolicy(preExecute);
}
public static PreExecutePolicy Create(Action preExecute) =>
new(preExecute);

internal PreExecutePolicy(Action preExecute)
{
internal PreExecutePolicy(Action preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
}

protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
return PreExecuteEngine.Implementation(_preExecute, action, context, cancellationToken);
}
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
PreExecuteEngine.Implementation(_preExecute, action, context, cancellationToken);
}

internal class PreExecutePolicy<TResult> : Policy<TResult>
{
private Action _preExecute;

public static PreExecutePolicy<TResult> Create(Action preExecute)
{
return new PreExecutePolicy<TResult>(preExecute);
}
public static PreExecutePolicy<TResult> Create(Action preExecute) =>
new PreExecutePolicy<TResult>(preExecute);

internal PreExecutePolicy(Action preExecute)
{
internal PreExecutePolicy(Action preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
}

protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
return PreExecuteEngine.Implementation(_preExecute, action, context, cancellationToken);
}
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
PreExecuteEngine.Implementation(_preExecute, action, context, cancellationToken);
}
Loading

0 comments on commit a6f49c7

Please sign in to comment.