Skip to content
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

Revert change for timeout test execution #1675

Merged
merged 1 commit into from
May 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,39 @@ public class ThreadOperations : IThreadOperations
/// <returns>Returns true if the action executed before the timeout. returns false otherwise.</returns>
public bool Execute(Action action, int timeout, CancellationToken cancelToken)
{
#if NETFRAMEWORK
if (cancelToken.IsCancellationRequested)
{
return false;
}

Thread executionThread = new(new ThreadStart(action))
{
IsBackground = true,
Name = "MSTest Test Execution Thread",
};

var isUserCancelled = false;
cancelToken.Register(() => isUserCancelled = true);

// This is not ideal and should be replaced with a mechanism that use STA thread only for tests that need it
// (based on user configuration). We are currently relying on testhost to be STA or MTA and will run ALL tests
// in this mode which is not ideal for different hosting mechanism not for performance.
executionThread.SetApartmentState(Thread.CurrentThread.GetApartmentState());
executionThread.Start();

if (executionThread.Join(timeout))
{
if (isUserCancelled)
{
return false;
}

return true;
}

return false;
#else
try
{
var executionTask = Task.Run(action, cancelToken);
Expand All @@ -41,5 +74,6 @@ public bool Execute(Action action, int timeout, CancellationToken cancelToken)
// Task execution canceled.
return false;
}
#endif
}
}