Skip to content

test: cancel background task#500

Merged
vbreuss merged 1 commit intomainfrom
topic/fix-test
Feb 28, 2026
Merged

test: cancel background task#500
vbreuss merged 1 commit intomainfrom
topic/fix-test

Conversation

@vbreuss
Copy link
Copy Markdown
Contributor

@vbreuss vbreuss commented Feb 28, 2026

Adjusts async verification tests to properly track and shut down background producer tasks, preventing lingering work after assertions complete.

Changes:

  • Capture the Task.Run(...) task instead of discarding it.
  • Cancel the CancellationTokenSource and await the background task to ensure it completes.

@vbreuss vbreuss self-assigned this Feb 28, 2026
Copilot AI review requested due to automatic review settings February 28, 2026 19:34
@vbreuss vbreuss added the coverage Additional tests for edge cases or improvement of test coverage label Feb 28, 2026
@vbreuss vbreuss changed the title tests: cancel background task test: cancel background task Feb 28, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts async verification tests to properly track and shut down background producer tasks, preventing lingering work after assertions complete.

Changes:

  • Capture the Task.Run(...) task instead of discarding it.
  • Cancel the CancellationTokenSource and await the background task to ensure it completes.
Comments suppressed due to low confidence (2)

Tests/Mockolate.Tests/Verify/VerificationResultTests.AsyncTests.cs:69

  • Cancellation/await of the background Task only happens after the assertion succeeds. If VerifyAsync/That throws, the loop keeps running and can leak work into other tests (and potentially prolong the test run). Consider wrapping the verification in try/finally so you always cancel the CTS and await the task.
			await That(((IAsyncVerificationResult)result).VerifyAsync(l => l.Length > 20)).IsTrue();
			cts.Cancel();
			await backgroundTask;

Tests/Mockolate.Tests/Verify/VerificationResultTests.AsyncTests.cs:165

  • Cancellation/await of the background Task happens after the verification call. If AtLeastOnce throws, the task will keep running (up to ~10s) and can bleed activity into subsequent tests. Consider using try/finally to always cancel and await the task even when the verification fails.
			Stopwatch sw = Stopwatch.StartNew();
			sut.VerifyMock.Invoked.Dispense(Match.AnyParameters()).Within(TimeSpan.FromMilliseconds(500))
				.AtLeastOnce();
			sw.Stop();
			cts.Cancel();
			await backgroundTask;

@vbreuss vbreuss enabled auto-merge (squash) February 28, 2026 19:37
@vbreuss vbreuss disabled auto-merge February 28, 2026 19:37
@vbreuss vbreuss enabled auto-merge (squash) February 28, 2026 19:37
@vbreuss vbreuss disabled auto-merge February 28, 2026 19:39
@vbreuss vbreuss merged commit 2b46f32 into main Feb 28, 2026
8 checks passed
@vbreuss vbreuss deleted the topic/fix-test branch February 28, 2026 19:39
@sonarqubecloud
Copy link
Copy Markdown

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 28, 2026

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.3 LTS (Noble Numbat)
AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.103
[Host] : .NET 10.0.3 (10.0.3, 10.0.326.7603), X64 RyuJIT x86-64-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean Error StdDev Gen0 Gen1 Allocated
Simple_Mockolate 1.561 μs 0.0137 μs 0.0128 μs 0.2460 - 4.02 KB
Simple_Moq 175.325 μs 1.5382 μs 1.3636 μs 0.4883 - 14.55 KB
Simple_NSubstitute 5.749 μs 0.0302 μs 0.0268 μs 0.5569 0.0076 9.14 KB
Simple_FakeItEasy 6.572 μs 0.0396 μs 0.0351 μs 0.4959 - 8.11 KB

@github-actions
Copy link
Copy Markdown

This is addressed in release v1.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coverage Additional tests for edge cases or improvement of test coverage state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants