Skip to content

Commit 32e80e8

Browse files
committed
Cleanup. Improve test coverage.
1 parent a346758 commit 32e80e8

File tree

9 files changed

+101
-235
lines changed

9 files changed

+101
-235
lines changed

src/Polly.Core/ResiliencePipeline.Sync.cs

Lines changed: 28 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,31 @@ public partial class ResiliencePipeline
1313
/// <param name="context">The context associated with the callback.</param>
1414
/// <param name="state">The state associated with the callback.</param>
1515
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
16-
public void Execute<TState>(
17-
Action<ResilienceContext, TState> callback,
18-
ResilienceContext context,
19-
TState state)
20-
{
21-
Guard.NotNull(callback);
22-
Guard.NotNull(context);
23-
24-
InitializeSyncContext(context);
25-
26-
Component.ExecuteCoreSync(
27-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
16+
public void Execute<TState>(Action<ResilienceContext, TState> callback, ResilienceContext context, TState state)
17+
=> Execute(
18+
static (context, state) =>
2819
{
29-
try
30-
{
31-
state.callback(context, state.state);
32-
return Outcome.Void;
33-
}
34-
catch (Exception e)
35-
{
36-
return Outcome.FromException(e);
37-
}
20+
state.callback(context, state.state);
21+
return VoidResult.Instance;
3822
},
3923
context,
40-
(callback, state)).GetResultOrRethrow();
41-
}
24+
(callback: Guard.NotNull(callback), state));
4225

4326
/// <summary>
4427
/// Executes the specified callback.
4528
/// </summary>
4629
/// <param name="callback">The user-provided callback.</param>
4730
/// <param name="context">The context associated with the callback.</param>
4831
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
49-
public void Execute(
50-
Action<ResilienceContext> callback,
51-
ResilienceContext context)
52-
{
53-
Guard.NotNull(callback);
54-
Guard.NotNull(context);
55-
56-
InitializeSyncContext(context);
57-
58-
Component.ExecuteCoreSync(
59-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
32+
public void Execute(Action<ResilienceContext> callback, ResilienceContext context)
33+
=> Execute(
34+
static (context, state) =>
6035
{
61-
try
62-
{
63-
state(context);
64-
return Outcome.Void;
65-
}
66-
catch (Exception e)
67-
{
68-
return Outcome.FromException(e);
69-
}
36+
state(context);
37+
return VoidResult.Instance;
7038
},
7139
context,
72-
callback).GetResultOrRethrow();
73-
}
40+
Guard.NotNull(callback));
7441

7542
/// <summary>
7643
/// Executes the specified callback.
@@ -92,20 +59,13 @@ public void Execute<TState>(
9259
try
9360
{
9461
Component.ExecuteCoreSync(
95-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
62+
static (context, state) =>
9663
{
97-
try
98-
{
99-
state.callback(state.state, context.CancellationToken);
100-
return Outcome.Void;
101-
}
102-
catch (Exception e)
103-
{
104-
return Outcome.FromException(e);
105-
}
64+
state.callback(state.state, context.CancellationToken);
65+
return VoidResult.Instance;
10666
},
10767
context,
108-
(callback, state)).GetResultOrRethrow();
68+
(callback, state));
10969
}
11070
finally
11171
{
@@ -130,20 +90,13 @@ public void Execute(
13090
try
13191
{
13292
Component.ExecuteCoreSync(
133-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
93+
static (context, state) =>
13494
{
135-
try
136-
{
137-
state(context.CancellationToken);
138-
return Outcome.Void;
139-
}
140-
catch (Exception e)
141-
{
142-
return Outcome.FromException(e);
143-
}
95+
state(context.CancellationToken);
96+
return VoidResult.Instance;
14497
},
14598
context,
146-
callback).GetResultOrRethrow();
99+
callback);
147100
}
148101
finally
149102
{
@@ -169,20 +122,13 @@ public void Execute<TState>(
169122
try
170123
{
171124
Component.ExecuteCoreSync(
172-
[DebuggerDisableUserUnhandledExceptions] static (_, state) =>
125+
static (_, state) =>
173126
{
174-
try
175-
{
176-
state.callback(state.state);
177-
return Outcome.Void;
178-
}
179-
catch (Exception e)
180-
{
181-
return Outcome.FromException(e);
182-
}
127+
state.callback(state.state);
128+
return VoidResult.Instance;
183129
},
184130
context,
185-
(callback, state)).GetResultOrRethrow();
131+
(callback, state));
186132
}
187133
finally
188134
{
@@ -204,20 +150,13 @@ public void Execute(Action callback)
204150
try
205151
{
206152
Component.ExecuteCoreSync(
207-
[DebuggerDisableUserUnhandledExceptions] static (_, state) =>
153+
static (_, state) =>
208154
{
209-
try
210-
{
211-
state();
212-
return Outcome.Void;
213-
}
214-
catch (Exception e)
215-
{
216-
return Outcome.FromException(e);
217-
}
155+
state();
156+
return VoidResult.Instance;
218157
},
219158
context,
220-
callback).GetResultOrRethrow();
159+
callback);
221160
}
222161
finally
223162
{
@@ -226,6 +165,4 @@ [DebuggerDisableUserUnhandledExceptions] static (_, state) =>
226165
}
227166

228167
private ResilienceContext GetSyncContext(CancellationToken cancellationToken) => GetSyncContext<VoidResult>(cancellationToken);
229-
230-
private void InitializeSyncContext(ResilienceContext context) => InitializeSyncContext<VoidResult>(context);
231168
}

src/Polly.Core/ResiliencePipeline.SyncT.cs

Lines changed: 11 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,7 @@ public TResult Execute<TResult, TState>(
2525

2626
InitializeSyncContext<TResult>(context);
2727

28-
return Component.ExecuteCoreSync(
29-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
30-
{
31-
try
32-
{
33-
var result = state.callback(context, state.state);
34-
return Outcome.FromResult(result);
35-
}
36-
catch (Exception e)
37-
{
38-
return Outcome.FromException<TResult>(e);
39-
}
40-
},
41-
context,
42-
(callback, state)).GetResultOrRethrow();
28+
return Component.ExecuteCoreSync(callback, context, state);
4329
}
4430

4531
/// <summary>
@@ -50,31 +36,8 @@ [DebuggerDisableUserUnhandledExceptions] static (context, state) =>
5036
/// <param name="context">The context associated with the callback.</param>
5137
/// <returns>An instance of <see cref="ValueTask"/> that represents the asynchronous execution.</returns>
5238
/// <exception cref="ArgumentNullException">Thrown when <paramref name="callback"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
53-
public TResult Execute<TResult>(
54-
Func<ResilienceContext, TResult> callback,
55-
ResilienceContext context)
56-
{
57-
Guard.NotNull(callback);
58-
Guard.NotNull(context);
59-
60-
InitializeSyncContext<TResult>(context);
61-
62-
return Component.ExecuteCoreSync(
63-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
64-
{
65-
try
66-
{
67-
var result = state(context);
68-
return Outcome.FromResult(result);
69-
}
70-
catch (Exception e)
71-
{
72-
return Outcome.FromException<TResult>(e);
73-
}
74-
},
75-
context,
76-
callback).GetResultOrRethrow();
77-
}
39+
public TResult Execute<TResult>(Func<ResilienceContext, TResult> callback, ResilienceContext context)
40+
=> Execute(static (context, state) => state(context), context, Guard.NotNull(callback));
7841

7942
/// <summary>
8043
/// Executes the specified callback.
@@ -95,19 +58,9 @@ public TResult Execute<TResult>(
9558
try
9659
{
9760
return Component.ExecuteCoreSync(
98-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
99-
{
100-
try
101-
{
102-
return Outcome.FromResult(state(context.CancellationToken));
103-
}
104-
catch (Exception e)
105-
{
106-
return Outcome.FromException<TResult>(e);
107-
}
108-
},
61+
static (context, state) => state(context.CancellationToken),
10962
context,
110-
callback).GetResultOrRethrow();
63+
callback);
11164
}
11265
finally
11366
{
@@ -131,19 +84,9 @@ public TResult Execute<TResult>(Func<TResult> callback)
13184
try
13285
{
13386
return Component.ExecuteCoreSync(
134-
[DebuggerDisableUserUnhandledExceptions] static (_, state) =>
135-
{
136-
try
137-
{
138-
return Outcome.FromResult(state());
139-
}
140-
catch (Exception e)
141-
{
142-
return Outcome.FromException<TResult>(e);
143-
}
144-
},
87+
static (_, state) => state(),
14588
context,
146-
callback).GetResultOrRethrow();
89+
callback);
14790
}
14891
finally
14992
{
@@ -169,19 +112,9 @@ public TResult Execute<TResult, TState>(Func<TState, TResult> callback, TState s
169112
try
170113
{
171114
return Component.ExecuteCoreSync(
172-
[DebuggerDisableUserUnhandledExceptions] static (_, state) =>
173-
{
174-
try
175-
{
176-
return Outcome.FromResult(state.callback(state.state));
177-
}
178-
catch (Exception e)
179-
{
180-
return Outcome.FromException<TResult>(e);
181-
}
182-
},
115+
static (_, state) => state.callback(state.state),
183116
context,
184-
(callback, state)).GetResultOrRethrow();
117+
(callback, state));
185118
}
186119
finally
187120
{
@@ -211,19 +144,9 @@ public TResult Execute<TResult, TState>(
211144
try
212145
{
213146
return Component.ExecuteCoreSync(
214-
[DebuggerDisableUserUnhandledExceptions] static (context, state) =>
215-
{
216-
try
217-
{
218-
return Outcome.FromResult(state.callback(state.state, context.CancellationToken));
219-
}
220-
catch (Exception e)
221-
{
222-
return Outcome.FromException<TResult>(e);
223-
}
224-
},
147+
static (context, state) => state.callback(state.state, context.CancellationToken),
225148
context,
226-
(callback, state)).GetResultOrRethrow();
149+
(callback, state));
227150
}
228151
finally
229152
{

src/Polly.Core/Utils/Pipeline/BridgeComponentBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ protected static Outcome<TTo> ConvertOutcome<TFrom, TTo>(Outcome<TFrom> outcome)
2727
return new(outcome.ExceptionDispatchInfo);
2828
}
2929

30-
return outcome.Result is null ? default : new((TTo)(object)outcome.Result);
30+
return new((TTo?)(object?)outcome.Result);
3131
}
3232
}

src/Polly.Core/Utils/Pipeline/PipelineComponent.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ internal abstract ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>(
1717
ResilienceContext context,
1818
TState state);
1919

20-
internal Outcome<TResult> ExecuteCoreSync<TResult, TState>(
21-
Func<ResilienceContext, TState, Outcome<TResult>> callback,
20+
internal TResult ExecuteCoreSync<TResult, TState>(
21+
Func<ResilienceContext, TState, TResult> callback,
2222
ResilienceContext context,
2323
TState state)
24-
=> ExecuteCore(
25-
static (context, state) => new ValueTask<Outcome<TResult>>(state.callbackMethod(context, state.stateObject)),
26-
context,
27-
(callbackMethod: callback, stateObject: state))
28-
.GetResult();
24+
=> ExecuteCore([DebuggerDisableUserUnhandledExceptions] static (context, state) =>
25+
{
26+
try
27+
{
28+
return new ValueTask<Outcome<TResult>>(new Outcome<TResult>(state.callback(context, state.state)));
29+
}
30+
#pragma warning disable CA1031 // Do not catch general exception types
31+
catch (Exception e)
32+
{
33+
return new ValueTask<Outcome<TResult>>(new Outcome<TResult>(e));
34+
}
35+
},
36+
context, (callback, state)).GetResult().GetResultOrRethrow();
2937

3038
public abstract ValueTask DisposeAsync();
3139

src/Polly.Core/Utils/TaskHelper.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@
55

66
internal static class TaskHelper
77
{
8-
public static void GetResult(this ValueTask<VoidResult> task)
9-
{
10-
Debug.Assert(
11-
task.IsCompleted,
12-
"The value task should be already completed at this point. If not, it's an indication that the strategy does not respect the ResilienceContext.IsSynchronous value.");
13-
14-
// Stryker disable once boolean : no means to test this
15-
if (task.IsCompleted)
16-
{
17-
_ = task.Result;
18-
return;
19-
}
20-
21-
task.Preserve().GetAwaiter().GetResult();
22-
}
23-
248
public static TResult GetResult<TResult>(this ValueTask<TResult> task)
259
{
2610
Debug.Assert(

0 commit comments

Comments
 (0)