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

Apartment(ApartmentState.STA) doesn't works together with Timeout #4119

Open
reejk opened this issue May 30, 2022 · 4 comments
Open

Apartment(ApartmentState.STA) doesn't works together with Timeout #4119

reejk opened this issue May 30, 2022 · 4 comments
Labels

Comments

@reejk
Copy link

reejk commented May 30, 2022

Apartment(ApartmentState.STA) doesn't works together with Timeout

I'm currently updating projects from .NET Framework (NUnit 3.10.1) to .NET 6 (NUnit 3.13.3) and found this problem. Real tests do some WPF relates stuff, so it requires STA apartment. The Timeout is used to prevent the test from running indefinitely when something goes wrong.

An example of minimalistic tests:

[TestFixture]
public class ApartmentTests
{
   [Test, Apartment(ApartmentState.STA), Timeout(1000)]
   public void SomeTestWithTimeout()
   {
      Console.WriteLine("SomeTestWithTimeout {0} {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.GetApartmentState());
      Assert.Pass();
   }
   
   [Test, Apartment(ApartmentState.STA)]
   public void SomeTestWithoutTimeout()
   {
      Console.WriteLine("SomeTestWithoutTimeout {0} {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.GetApartmentState());
      Assert.Pass();
   }
}

nunit3-console output:

NUnit Console 3.15.0 (Release)
Copyright (c) 2022 Charlie Poole, Rob Prouse
30 мая 2022 г. 21:21:19

Runtime Environment
   OS Version: Microsoft Windows 10.0.18362
  Runtime: .NET 6.0.4

Test Files
    .\TestProject1.dll

SomeTestWithoutTimeout 6 STA
SomeTestWithTimeout 8 MTA
@manfred-brands
Copy link
Member

Timeout targeting anything other than .NETFramework is not safe (see #4021) and we have not found a solution for this.
The library has to create a new thread to run the test in and hope for the best as it cannot stop it (MS dropped Thread.Abort). This doesn't work with STA components created in a SetUp method. I suggest to refactor your test that they don't need Timeout at all.

@reejk
Copy link
Author

reejk commented May 31, 2022

The library has to create a new thread to run the test in and hope for the best as it cannot stop it

But why does it create a thread without a specified Apartment?
In my opinion, if Timeout can't really work on newer .NETs, it shouldn't break other things like Apartment.

Updated docs about this limitation and some runtime or compile-time checks would also be acceptable.

@TomEdwardsEnscape
Copy link

Timeout also breaks SingleThreaded for the same underlying reason: it always creates a new thread.

@OsirisTerje
Copy link
Member

See comment here: #4658 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants