Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

NCrunch and AppDomains
CodeAdze
#1 Posted : Friday, March 11, 2022 10:16:12 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2014(UTC)
Posts: 29
Location: Australia

Thanks: 4 times
Was thanked: 3 time(s) in 3 post(s)
Hi,
I was wondering if a test being run under NCrunch and the code that's being tested are in the same appdomain?
In my test I am invoking AppDomain.CurrentDomain.SetThreadPrincipal() but my test is failing and it appears to be because when I'm invoking Thread.CurrentPrincipal.IsInRole in my code under test CurrentPrinciple is null.

Could this be a different appdomain issue? or could the thread that's executing my code under test not be getting it's Principle set by the test because of how NCrunch uses threads?? I tried setting <Apartment(ApartmentState.STA)> (clutching at straws in the dark!) but that didn't help.

I am hoping someone can point me in the right direction.

Thanks in advance.
Remco
#2 Posted : Friday, March 11, 2022 10:58:05 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for posting.

Under .NET Framework, the NCrunch task runner builds its own application domain inside the test process and uses this to execute the test and underlying code. The appdomain is tied to the lifespan of the test process.

Under .NET Core and .NET 5+, NCrunch does not build an appdomain and simply uses the default application domain of the task runner, which is essentially a console process.

The behaviour in regards to threading is controlled by the test framework. Threads are created by the test framework and their lifetime is managed by the framework. Depending on which test framework you are using, they may have attributes available for controlling the apartment state of the execution thread.

Note that neither NCrunch nor the test framework have any reliable way to distinguish between test code and production code. It's all just code as far as the system is concerned. It will just call the test method with the execution thread and what happens from there is up to you. It's possible to build an application domain boundary between your test code and production code, but it would be up to you to build this and NCrunch won't try to do it for you.

I have no personal experience in trying to change the CurrentPrincipal on an executing thread, as this isn't something I've had to do myself. Perhaps there is something fundamental inside the runtime that is causing unexpected behaviour here.
1 user thanked Remco for this useful post.
CodeAdze on 3/12/2022(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.028 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download