Daily Usage Issues

Is out of process nature of run failing my test?

Started by sammicrosoft on 5,112 views

Hi,

After installing NCrunch, bunch of my existing MSTest tests failed. Upon investigation, I found most of the test failed as the value of Thread.CurrentPrincipal.Identity.Name in the below product code is getting set to empty whereas it is set to correct value in domain\alias format when run under MSTest. As the default value for ownerAlias is empty, username becomes empty and hit the exception in the next line.

var username = String.IsNullOrWhiteSpace(ownerAlias) ? Thread.CurrentPrincipal.Identity.Name : ownerAlias;
var xx = Thread.CurrentPrincipal.Identity.Name; // This is to verify the value
ExceptionUtility.ThrowIfArgumentNullOrEmpty(username, "Could not find current username.");



Wondering, if this is because NCrunch uses its own process to run the test, so doesn't have a valid CurrentPrincipal value!

I have tried changing the property "Engine hosting strategy = HostInsideIDE (the default was x64SatelliteProcess)" under configuration with no success.

Has anyone encountered similar issue before and any suggestion on how to tackle this?

Appreciate your help.
Hi, thanks for sharing this issue.

NCrunch's support for MSTest is emulated (not integrated), so it's basically running the tests using it's own adapter. Through trial and error, the adapter has become quite closely aligned with the normal runtime behaviour of MSTest. But there are still edge cases where we have differences.

My guess would be that in this specific case, MSTest is deliberately setting the CurrentPrincipal on the thread, or it is otherwise being set indirectly by the framework.

I would need to investigate in detail before I could tell you whether it's possible for NCrunch to implement this kind of behaviour, but for the time being I would recommend finding an abstraction or hook-point that will let you work without it. Perhaps setting the property deliberately inside an AssemblyInitialize could do the trick? (assuming you have access to do this) - NCrunch will run AssemblyInitialize on the same thread as it runs the test.

Edited

Thanks Remco. To be specific, our unit tests are written in Visual Studio UnitTesting framework. The test that is failing due to the reason stated before is part of product code, not test code. The same test passes when running from Visual Studio or MSTest. Sorry for not being clear. Thread.CurrentPrincipal should be set automatically for the current user context ((- BROKEN LINK -). So, I was wondering if the code, when run under NCrunch behaves differently from how it supposed to be.
Sorry, I don't think I explained this very well.

MSTest is doing something that sets this property on the current thread by default. This is either done deliberately in MSTest, or somehow indirectly.

NCrunch doesn't actually run MSTest. It uses a custom built adapter designed to behave as close as possible to MSTest. NCrunch's adapter does not contain any code to deliberately set this property in the same way as MSTest. If the behaviour of MSTest is clear in this case and I can find a way to feasibly reproduce it in NCrunch, I will. Until then, as the property is being used by your production code, I suggest setting it deliberately to the expected value inside your test code. This should allow your production code to work around NCrunch's compatibility hole.

Post a reply

Log in to reply.