Daily Usage Issues

Error running test that requires IsolatedStorage

Started by MatthewSteeples on 3,200 views

We have some tests that are throwing a SecurityException when trying to save an OpenXml Excel file. It looks like it's trying to use IsolatedStorage (because it's an 8mb file and it saves it to disk instead of in memory). I'm sure I used to know how to solve this kind of problem but I can't work out where to begin with NCrunch!

Has anyone had anything like this before?
Hi, thanks for posting.

NCrunch provides no special handling for IsolatedStorage, so test processes behave the same way as any console application without any user impersonation or configuration. Personally, I have no experience in working with IsolatedStorage, so I can't really advise on how best to proceed here. It may be that some preparation is necessary in your test setup code.
Hi Remco, does NCrunch load and execute the tests in an AppDomain at all? If so, it's potentially looking like a lack of "evidence" could be the problem. It looks like Isolated Storage uses the identity of the AppDomain to determine where to read and write data, so if this isn't set (as I believe it's optional) then it fails
MatthewSteeples wrote:Hi Remco, does NCrunch load and execute the tests in an AppDomain at all? If so, it's potentially looking like a lack of "evidence" could be the problem. It looks like Isolated Storage uses the identity of the AppDomain to determine where to read and write data, so if this isn't set (as I believe it's optional) then it fails


Yes, we do. I wonder if this could be an issue. Here is what we do at the moment:


var setup = new AppDomainSetup
			            	{
			            		ApplicationName = "nCrunch.TestRunner_" + externalProcessid,
			            		ApplicationBase = appDomainBaseDirectory,
			            		ConfigurationFile = configFilePath
			            	};

			var evidence = new Evidence();
			evidence.AddHost(new Zone(SecurityZone.MyComputer));
			evidence.AddHost(new Url(typeof(TaskEnvironment).Assembly.CodeBase));

			var appDomain = AppDomain.CreateDomain(
				"nCrunch.TestRunner.AppDomain_" + externalProcessid, 
				evidence, 
				setup,
				new PermissionSet(PermissionState.Unrestricted)
				);


Do you see any issues here? I wonder if an option would be for you to create a new AppDomain in your test with the parameters that will allow the code to work.
That looks fine from what I can tell. It might be (because of continuous crunching) that it's run out of space or something like that instead. The line about adding the MyComputer Zone to the evidence is what's recommended. I'll keep digging.
MatthewSteeples wrote:That looks fine from what I can tell. It might be (because of continuous crunching) that it's run out of space or something like that instead. The line about adding the MyComputer Zone to the evidence is what's recommended. I'll keep digging.


Thanks. Sorry that I can't be of more help in this. Let me know if you find anything in the runner that needs adjustment for this.
Managed to do some digging with this and stepping into framework source / spelunking through ILSpy. On the plus side, this is very easy to reproduce!

https://ledgerscope.sharepoint.com/:i:/s/MMBTesting/ER5k2vTM7cVOgUW6HPS4ProB2aHdrk2JoUe1xG5Qpe-3JQ?e=8qjBDC - StackTrace - You can see there's a FileSystemWatcher in here which is what I think is the cause of the problem. It could be (just a guess) that NCrunch is trying to access the IsolatedStorage file without going through the proper procedure
https://ledgerscope.sharepoint.com/:u:/s/MMBTesting/ETGirIk4UNFLgj6ALcs1dUkBUOk6ZUjRdinpfbEvXTLCaQ?e=gJIyTG - Project with test to reproduce the issue

Happy hunting :)

Let me know if there's anything else I can send.
Ahh ... now THIS is making sense.

NCrunch tracks the files accessed during a test run using an API hook, so that it knows which tests to mark as impacted if a resource file is changed. Apparently this API hook doesn't work properly when isolated storage is involved.

Switch off the Track file dependencies setting for the test project and the issue will hopefully be gone.

Edited

Yep, turning off that option fixes our tests. Just to give you a bigger picture of this, it started affecting us because Microsoft's library for generating Excel files starts using Isolated Storage as a temp files location either when the file gets over a certain size or the memory in use by the process reaches a certain limit.

If you end up putting a fix in for it we're happy to test it out for you.

Thanks
Thanks for confirming. I've scoped a task to look at this issue to see if we can properly fix it. Keeping that setting adjusted is a sensible long term workaround, it's a feature most people never notice anyway and it actually already doesn't work for .NET Core..

Post a reply

Log in to reply.