-
-
Notifications
You must be signed in to change notification settings - Fork 138
fix: preserve concurrent Assert.Multiple failures #6730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/TUnit.Assertions.Tests/ConcurrentAssertMultipleTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| namespace TUnit.Assertions.Tests; | ||
|
|
||
| public class ConcurrentAssertMultipleTests | ||
| { | ||
| [Test] | ||
| [Arguments(false)] | ||
| [Arguments(true)] | ||
| public async Task ConcurrentFailuresAreAllRetained(bool nestedScopes) | ||
| { | ||
| const int workers = 8; | ||
| const int failuresPerWorker = 2_000; | ||
| var start = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| var exception = await Assert.That(async () => | ||
| { | ||
| using (Assert.Multiple()) | ||
| { | ||
| var tasks = Enumerable.Range(0, workers).Select(worker => Task.Run(async () => | ||
| { | ||
| await start.Task; | ||
| using var nested = nestedScopes ? Assert.Multiple() : null; | ||
| for (var i = 0; i < failuresPerWorker; i++) | ||
| { | ||
| Assert.Fail($"Worker {worker}, failure {i}"); | ||
| } | ||
| })).ToArray(); | ||
|
|
||
| start.SetResult(); | ||
| await Task.WhenAll(tasks); | ||
| } | ||
| }).Throws<AssertionException>(); | ||
|
|
||
| var failures = ((AggregateException)exception!.InnerException!).InnerExceptions; | ||
| await Assert.That(failures.Count).IsEqualTo(workers * failuresPerWorker); | ||
| await Assert.That(failures.Select(failure => failure.Message).Distinct().Count()) | ||
| .IsEqualTo(workers * failuresPerWorker); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task PassingOrChainDoesNotConsumeConcurrentFailure() | ||
| { | ||
| var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| var exception = await Assert.That(async () => | ||
| { | ||
| using (Assert.Multiple()) | ||
| { | ||
| async Task RunChainAsync() | ||
| { | ||
| await Assert.That(async () => | ||
| { | ||
| started.SetResult(); | ||
| await release.Task; | ||
| return 1; | ||
| }).IsEqualTo(1).Or.IsEqualTo(2); | ||
| } | ||
|
|
||
| var chain = RunChainAsync(); | ||
|
|
||
| await started.Task; | ||
| Assert.Fail("Independent failure"); | ||
| release.SetResult(); | ||
| await chain; | ||
| } | ||
| }).Throws<AssertionException>(); | ||
|
|
||
| await Assert.That(exception!.Message).IsEqualTo("Independent failure"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task AndChainStillChecksSecondAssertionAfterConcurrentFailure() | ||
| { | ||
| var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| var exception = await Assert.That(async () => | ||
| { | ||
| using (Assert.Multiple()) | ||
| { | ||
| async Task RunChainAsync() | ||
| { | ||
| await Assert.That(async () => | ||
| { | ||
| started.SetResult(); | ||
| await release.Task; | ||
| return 1; | ||
| }).IsEqualTo(1).And.IsEqualTo(2); | ||
| } | ||
|
|
||
| var chain = RunChainAsync(); | ||
|
|
||
| await started.Task; | ||
| Assert.Fail("Independent failure"); | ||
| release.SetResult(); | ||
| await chain; | ||
| } | ||
| }).Throws<AssertionException>(); | ||
|
|
||
| var failures = ((AggregateException)exception!.InnerException!).InnerExceptions; | ||
| await Assert.That(failures.Count).IsEqualTo(2); | ||
| await Assert.That(failures[0].Message).IsEqualTo("Independent failure"); | ||
| await Assert.That(failures[1].Message).Contains("and to be 2"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task SuccessfulPreWorkDoesNotSkipItemAssertionAfterConcurrentFailure() | ||
| { | ||
| var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| var exception = await Assert.That(async () => | ||
| { | ||
| using (Assert.Multiple()) | ||
| { | ||
| async Task RunChainAsync() | ||
| { | ||
| await Assert.That(async () => | ||
| { | ||
| started.SetResult(); | ||
| await release.Task; | ||
| return (IEnumerable<int>)[1]; | ||
| }).HasSingleItem().Item.IsEqualTo(2); | ||
| } | ||
|
|
||
| var chain = RunChainAsync(); | ||
| await started.Task; | ||
| Assert.Fail("Independent failure"); | ||
| release.SetResult(); | ||
| await chain; | ||
| } | ||
| }).Throws<AssertionException>(); | ||
|
|
||
| var failures = ((AggregateException)exception!.InnerException!).InnerExceptions; | ||
| await Assert.That(failures.Count).IsEqualTo(2); | ||
| await Assert.That(failures[0].Message).IsEqualTo("Independent failure"); | ||
| await Assert.That(failures[1].Message).Contains("Expected to be 2"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge thomhurst/TUnit /tmp/coderabbit-repo-knowledge/thomhurst-tunit-199e8fbe/conventions /tmp/coderabbit-repo-knowledge/thomhurst-tunit-199e8fbe/learningsLength of output: 3346
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 6316
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 50371
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 11885
Pass the test cancellation token to the coordination waits.
These four test methods do not accept
CancellationToken. TheirTaskCompletionSourceandTask.WhenAllwaits cannot observe test cancellation. Add a finalCancellationToken cancellationTokenparameter and use.WaitAsync(cancellationToken)forstart,started,release,chain, andTask.WhenAll(tasks).📍 Affects 1 file
tests/TUnit.Assertions.Tests/ConcurrentAssertMultipleTests.cs#L8-L8(this comment)tests/TUnit.Assertions.Tests/ConcurrentAssertMultipleTests.cs#L40-L40tests/TUnit.Assertions.Tests/ConcurrentAssertMultipleTests.cs#L72-L72tests/TUnit.Assertions.Tests/ConcurrentAssertMultipleTests.cs#L107-L107🤖 Prompt for AI Agents
Source: Coding guidelines