-
Notifications
You must be signed in to change notification settings - Fork 291
Add UseSTASynchronizationContext to STATestMethod #7192
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull request overview
This PR adds a UseSTASynchronizationContext property to STATestMethodAttribute to enable preserving the STA thread context for async continuations. When enabled, the attribute creates a custom SynchronizationContext that ensures async continuations run on the same STA thread, which is useful for testing UI components that require STA threading.
Changes:
- Introduced
SingleThreadedSTASynchronizationContextclass that manages a dedicated STA thread and routes all operations to it - Added
UseSTASynchronizationContextproperty toSTATestMethodAttributewith default value offalse - Modified
STATestMethodAttribute.ExecuteAsyncto conditionally use the new synchronization context - Added tests to verify both default behavior (no sync context) and new behavior (with sync context)
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/TestFramework/TestFramework/Attributes/TestMethod/SingleThreadedSTASynchronizationContext.cs |
New internal class that implements a custom SynchronizationContext with a dedicated STA thread for routing operations |
src/TestFramework/TestFramework/Attributes/TestMethod/STATestMethodAttribute.cs |
Added UseSTASynchronizationContext property and modified ExecuteAsync to optionally use the new sync context |
src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt |
Added public API entries for the new property getter and setter |
test/UnitTests/MSTest.SelfRealExamples.UnitTests/STATestMethodSyncContext.cs |
Added integration tests verifying both default and new behavior |
src/TestFramework/TestFramework/Attributes/TestMethod/STATestMethodAttribute.cs
Show resolved
Hide resolved
...TestFramework/TestFramework/Attributes/TestMethod/SingleThreadedSTASynchronizationContext.cs
Show resolved
Hide resolved
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.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
...TestFramework/TestFramework/Attributes/TestMethod/SingleThreadedSTASynchronizationContext.cs
Show resolved
Hide resolved
| SetSynchronizationContext(this); | ||
| foreach (Action callback in _queue.GetConsumingEnumerable()) | ||
| { | ||
| callback(); |
Copilot
AI
Jan 12, 2026
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.
Exceptions thrown by the callback in the thread loop are not handled and will terminate the background thread. This could leave queued actions unexecuted and cause hangs in Send calls waiting for done.Set(). Consider wrapping the callback invocation in a try-catch block to handle and potentially log exceptions.
| callback(); | |
| try | |
| { | |
| callback(); | |
| } | |
| catch (Exception ex) | |
| { | |
| System.Diagnostics.Debug.WriteLine($"Exception in {nameof(SingleThreadedSTASynchronizationContext)} callback: {ex}"); | |
| } |
Fixes #5729