Skip to content

Commit 24165be

Browse files
committed
Tidy async tests
Tests were using async test helpers which ran synchronously. Best practice nevertheless to await all awaitables.
1 parent fab36a3 commit 24165be

5 files changed

Lines changed: 29 additions & 29 deletions

File tree

src/Polly.SharedSpecs/Retry/RetryAsyncSpecs.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_
166166
}
167167

168168
[Fact]
169-
public void Should_call_onretry_on_each_retry_with_the_current_retry_count()
169+
public async Task Should_call_onretry_on_each_retry_with_the_current_retry_count()
170170
{
171171
var expectedRetryCounts = new[] { 1, 2, 3 };
172172
var retryCounts = new List<int>();
@@ -175,14 +175,14 @@ public void Should_call_onretry_on_each_retry_with_the_current_retry_count()
175175
.Handle<DivideByZeroException>()
176176
.RetryAsync(3, (_, retryCount) => retryCounts.Add(retryCount));
177177

178-
policy.RaiseExceptionAsync<DivideByZeroException>(3);
178+
await policy.RaiseExceptionAsync<DivideByZeroException>(3);
179179

180180
retryCounts.Should()
181181
.ContainInOrder(expectedRetryCounts);
182182
}
183183

184184
[Fact]
185-
public void Should_call_onretry_on_each_retry_with_the_current_exception()
185+
public async Task Should_call_onretry_on_each_retry_with_the_current_exception()
186186
{
187187
var expectedExceptions = new object[] { "Exception #1", "Exception #2", "Exception #3" };
188188
var retryExceptions = new List<Exception>();
@@ -191,7 +191,7 @@ public void Should_call_onretry_on_each_retry_with_the_current_exception()
191191
.Handle<DivideByZeroException>()
192192
.RetryAsync(3, (exception, _) => retryExceptions.Add(exception));
193193

194-
policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
194+
await policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
195195

196196
retryExceptions
197197
.Select(x => x.HelpLink)
@@ -274,7 +274,7 @@ public void Context_should_be_empty_if_execute_not_called_with_any_data()
274274
.Handle<DivideByZeroException>()
275275
.RetryAsync((_, __, context) => capturedContext = context);
276276

277-
policy.RaiseExceptionAsync<DivideByZeroException>();
277+
policy.Awaiting(async x => await x.RaiseExceptionAsync<DivideByZeroException>()).ShouldNotThrow();
278278

279279
capturedContext.Should()
280280
.BeEmpty();

src/Polly.SharedSpecs/Retry/RetryForeverAsyncSpecs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_
108108
}
109109

110110
[Fact]
111-
public void Should_call_onretry_on_each_retry_with_the_current_exception()
111+
public async Task Should_call_onretry_on_each_retry_with_the_current_exception()
112112
{
113113
var expectedExceptions = new object[] {"Exception #1", "Exception #2", "Exception #3"};
114114
var retryExceptions = new List<Exception>();
@@ -117,7 +117,7 @@ public void Should_call_onretry_on_each_retry_with_the_current_exception()
117117
.Handle<DivideByZeroException>()
118118
.RetryForeverAsync(exception => retryExceptions.Add(exception));
119119

120-
policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
120+
await policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
121121

122122
retryExceptions
123123
.Select(x => x.HelpLink)
@@ -152,7 +152,7 @@ public void Context_should_be_empty_if_execute_not_called_with_any_data()
152152
.Handle<DivideByZeroException>()
153153
.RetryForeverAsync((_, context) => capturedContext = context);
154154

155-
policy.RaiseExceptionAsync<DivideByZeroException>();
155+
policy.Awaiting(async x => await x.RaiseExceptionAsync<DivideByZeroException>()).ShouldNotThrow();
156156

157157
capturedContext.Should()
158158
.BeEmpty();

src/Polly.SharedSpecs/Retry/WaitAndRetryAsyncSpecs.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public void Should_sleep_for_the_specified_duration_each_retry_when_specified_ex
275275
}
276276

277277
[Fact]
278-
public void Should_sleep_for_the_specified_duration_each_retry_when_specified_exception_thrown_less_number_of_times_than_there_are_sleep_durations()
278+
public async Task Should_sleep_for_the_specified_duration_each_retry_when_specified_exception_thrown_less_number_of_times_than_there_are_sleep_durations()
279279
{
280280
var totalTimeSlept = 0;
281281

@@ -294,7 +294,7 @@ public void Should_sleep_for_the_specified_duration_each_retry_when_specified_ex
294294
return TaskHelper.EmptyTask;
295295
};
296296

297-
policy.RaiseExceptionAsync<DivideByZeroException>(2);
297+
await policy.RaiseExceptionAsync<DivideByZeroException>(2);
298298

299299
totalTimeSlept.Should()
300300
.Be(1 + 2);
@@ -323,7 +323,7 @@ public void Should_not_sleep_if_no_retries()
323323
}
324324

325325
[Fact]
326-
public void Should_call_onretry_on_each_retry_with_the_current_timespan()
326+
public async Task Should_call_onretry_on_each_retry_with_the_current_timespan()
327327
{
328328
var expectedRetryWaits = new []
329329
{
@@ -343,14 +343,14 @@ public void Should_call_onretry_on_each_retry_with_the_current_timespan()
343343
3.Seconds()
344344
}, (_, timeSpan) => actualRetryWaits.Add(timeSpan));
345345

346-
policy.RaiseExceptionAsync<DivideByZeroException>(3);
346+
await policy.RaiseExceptionAsync<DivideByZeroException>(3);
347347

348348
actualRetryWaits.Should()
349349
.ContainInOrder(expectedRetryWaits);
350350
}
351351

352352
[Fact]
353-
public void Should_call_onretry_on_each_retry_with_the_current_exception()
353+
public async Task Should_call_onretry_on_each_retry_with_the_current_exception()
354354
{
355355
var expectedExceptions = new object[] { "Exception #1", "Exception #2", "Exception #3" };
356356
var retryExceptions = new List<Exception>();
@@ -364,7 +364,7 @@ public void Should_call_onretry_on_each_retry_with_the_current_exception()
364364
3.Seconds()
365365
}, (exception, _) => retryExceptions.Add(exception));
366366

367-
policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
367+
await policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
368368

369369
retryExceptions
370370
.Select(x => x.HelpLink)
@@ -373,7 +373,7 @@ public void Should_call_onretry_on_each_retry_with_the_current_exception()
373373
}
374374

375375
[Fact]
376-
public void Should_call_onretry_on_each_retry_with_the_current_retry_count()
376+
public async Task Should_call_onretry_on_each_retry_with_the_current_retry_count()
377377
{
378378
var expectedRetryCounts = new[] { 1, 2, 3 };
379379
var retryCounts = new List<int>();
@@ -387,7 +387,7 @@ public void Should_call_onretry_on_each_retry_with_the_current_retry_count()
387387
3.Seconds()
388388
}, (_, __, retryCount, ___) => retryCounts.Add(retryCount));
389389

390-
policy.RaiseExceptionAsync<DivideByZeroException>(3);
390+
await policy.RaiseExceptionAsync<DivideByZeroException>(3);
391391

392392
retryCounts.Should()
393393
.ContainInOrder(expectedRetryCounts);
@@ -426,7 +426,7 @@ public void Should_create_new_state_for_each_call_to_policy()
426426
}
427427

428428
[Fact]
429-
public void Should_call_onretry_with_the_passed_context()
429+
public async Task Should_call_onretry_with_the_passed_context()
430430
{
431431
IDictionary<string, object> contextData = null;
432432

@@ -439,7 +439,7 @@ public void Should_call_onretry_with_the_passed_context()
439439
3.Seconds()
440440
}, (_, __, context) => contextData = context);
441441

442-
policy.RaiseExceptionAsync<DivideByZeroException>(
442+
await policy.RaiseExceptionAsync<DivideByZeroException>(
443443
new { key1 = "value1", key2 = "value2" }.AsDictionary()
444444
);
445445

@@ -461,14 +461,14 @@ public void Context_should_be_empty_if_execute_not_called_with_any_data()
461461
2.Seconds(),
462462
3.Seconds()
463463
}, (_, __, context) => capturedContext = context);
464-
policy.RaiseExceptionAsync<DivideByZeroException>();
464+
policy.Awaiting(async x => await x.RaiseExceptionAsync<DivideByZeroException>()).ShouldNotThrow();
465465

466466
capturedContext.Should()
467467
.BeEmpty();
468468
}
469469

470470
[Fact]
471-
public void Should_create_new_context_for_each_call_to_execute()
471+
public async Task Should_create_new_context_for_each_call_to_execute()
472472
{
473473
string contextValue = null;
474474

@@ -480,13 +480,13 @@ public void Should_create_new_context_for_each_call_to_execute()
480480
},
481481
(_, __, context) => contextValue = context["key"].ToString());
482482

483-
policy.RaiseExceptionAsync<DivideByZeroException>(
483+
await policy.RaiseExceptionAsync<DivideByZeroException>(
484484
new { key = "original_value" }.AsDictionary()
485485
);
486486

487487
contextValue.Should().Be("original_value");
488488

489-
policy.RaiseExceptionAsync<DivideByZeroException>(
489+
await policy.RaiseExceptionAsync<DivideByZeroException>(
490490
new { key = "new_value" }.AsDictionary()
491491
);
492492

@@ -533,7 +533,7 @@ public void Should_throw_when_onretry_action_is_null_without_context_when_using_
533533
}
534534

535535
[Fact]
536-
public void Should_calculate_retry_timespans_from_current_retry_attempt_and_timespan_provider()
536+
public async Task Should_calculate_retry_timespans_from_current_retry_attempt_and_timespan_provider()
537537
{
538538
var expectedRetryWaits = new[]
539539
{
@@ -553,7 +553,7 @@ public void Should_calculate_retry_timespans_from_current_retry_attempt_and_time
553553
(_, timeSpan) => actualRetryWaits.Add(timeSpan)
554554
);
555555

556-
policy.RaiseExceptionAsync<DivideByZeroException>(5);
556+
await policy.RaiseExceptionAsync<DivideByZeroException>(5);
557557

558558
actualRetryWaits.Should()
559559
.ContainInOrder(expectedRetryWaits);

src/Polly.SharedSpecs/Retry/WaitAndRetryForeverAsyncSpecs.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void Should_not_sleep_if_no_retries()
199199
}
200200

201201
[Fact]
202-
public void Should_call_onretry_on_each_retry_with_the_current_exception()
202+
public async Task Should_call_onretry_on_each_retry_with_the_current_exception()
203203
{
204204
var expectedExceptions = new object[] { "Exception #1", "Exception #2", "Exception #3" };
205205
var retryExceptions = new List<Exception>();
@@ -209,7 +209,7 @@ public void Should_call_onretry_on_each_retry_with_the_current_exception()
209209
.Handle<DivideByZeroException>()
210210
.WaitAndRetryForeverAsync(provider, (exception, _) => retryExceptions.Add(exception));
211211

212-
policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
212+
await policy.RaiseExceptionAsync<DivideByZeroException>(3, (e, i) => e.HelpLink = "Exception #" + i);
213213

214214
retryExceptions
215215
.Select(x => x.HelpLink)
@@ -260,7 +260,7 @@ public void Should_create_new_context_for_each_call_to_policy()
260260
}
261261

262262
[Fact]
263-
public void Should_calculate_retry_timespans_from_current_retry_attempt_and_timespan_provider()
263+
public async Task Should_calculate_retry_timespans_from_current_retry_attempt_and_timespan_provider()
264264
{
265265
var expectedRetryWaits = new[]
266266
{
@@ -280,7 +280,7 @@ public void Should_calculate_retry_timespans_from_current_retry_attempt_and_time
280280
(_, timeSpan) => actualRetryWaits.Add(timeSpan)
281281
);
282282

283-
policy.RaiseExceptionAsync<DivideByZeroException>(5);
283+
await policy.RaiseExceptionAsync<DivideByZeroException>(5);
284284

285285
actualRetryWaits.Should()
286286
.ContainInOrder(expectedRetryWaits);

src/Polly.SharedSpecs/Wrap/PolicyWrapSpecsAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public async Task Outermost_policy_handling_exception_should_report_as_PolicyWra
326326
PolicyResult executeAndCaptureResultOnPolicyWrap =
327327
await wrap.ExecuteAndCaptureAsync(() => { throw new ArgumentNullException(); });
328328

329-
executeAndCaptureResultOnPolicyWrap.Outcome.Should().Be(OutcomeType.Failure);
329+
executeAndCaptureResultOnPolicyWrap.Outcome.Should().Be(OutcomeType.Failure);
330330
executeAndCaptureResultOnPolicyWrap.FinalException.Should().BeOfType<ArgumentNullException>();
331331
executeAndCaptureResultOnPolicyWrap.ExceptionType.Should().Be(ExceptionType.HandledByThisPolicy);
332332
}

0 commit comments

Comments
 (0)