Skip to content

Commit

Permalink
Use Stopwatch.StartNew
Browse files Browse the repository at this point in the history
Use `Stopwatch.StartNew()` instead of creating one then manually starting it.
  • Loading branch information
martincostello committed Jan 19, 2025
1 parent 9882803 commit 7124fb6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
3 changes: 1 addition & 2 deletions test/Polly.Specs/Retry/WaitAndRetrySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,8 +1048,7 @@ public void Should_honour_cancellation_immediately_during_wait_phase_of_waitandr
int attemptsInvoked = 0;
Action onExecute = () => attemptsInvoked++;

Stopwatch watch = new Stopwatch();
watch.Start();
var watch = Stopwatch.StartNew();

PolicyExtensions.ExceptionAndOrCancellationScenario scenario = new PolicyExtensions.ExceptionAndOrCancellationScenario
{
Expand Down
8 changes: 2 additions & 6 deletions test/Polly.Specs/Timeout/TimeoutSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,12 @@ public void Should_not_throw_when_timeout_is_greater_than_execution_duration__pe
[Fact]
public void Should_throw_timeout_after_correct_duration__pessimistic()
{
Stopwatch watch = new Stopwatch();

TimeSpan timeout = TimeSpan.FromSeconds(1);
var policy = Policy.Timeout(timeout, TimeoutStrategy.Pessimistic);

TimeSpan tolerance = TimeSpan.FromSeconds(3); // Consider increasing tolerance, if test fails transiently in different test/build environments.

watch.Start();
var watch = Stopwatch.StartNew();
Should.Throw<TimeoutRejectedException>(() => policy.Execute(() => SystemClock.Sleep(TimeSpan.FromSeconds(10), CancellationToken)));
watch.Stop();

Expand Down Expand Up @@ -410,15 +408,13 @@ public void Should_not_throw_when_timeout_is_greater_than_execution_duration__op
[Fact]
public void Should_throw_timeout_after_correct_duration__optimistic()
{
Stopwatch watch = new Stopwatch();

TimeSpan timeout = TimeSpan.FromSeconds(1);
var policy = Policy.Timeout(timeout);
var userCancellationToken = CancellationToken;

TimeSpan tolerance = TimeSpan.FromSeconds(3); // Consider increasing tolerance, if test fails transiently in different test/build environments.

watch.Start();
var watch = Stopwatch.StartNew();

// Delegate observes cancellation token, so permitting optimistic cancellation.
Should.Throw<TimeoutRejectedException>(() => policy.Execute(ct => SystemClock.Sleep(TimeSpan.FromSeconds(10), ct), userCancellationToken));
Expand Down

0 comments on commit 7124fb6

Please sign in to comment.