urasandesu / Prig

Prig is a lightweight framework for test indirections in .NET Framework.
Other
117 stars 21 forks source link

Error while faking System.Diagnostics.Process #80

Closed xkjyeah closed 7 years ago

xkjyeah commented 7 years ago

I get the following error:

System.IO.FileLoadException: Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)
   at System.Diagnostics.Process.TryPrigDispose(Process , Boolean )
   at System.Diagnostics.Process.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Finalize()
   at System.Diagnostics.Process.TryPrigDispose(Process , Boolean )
   at System.Diagnostics.Process.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Finalize()
========== Run test finished: 1 run (0:00:04.3713273) ==========

The error seems to occur at a line with:

var process = new System.Diagnostics.Process();

The code in the test leading up to it was:

            using (new IndirectionsContext())
            {
                PProcess.BeginErrorReadLine().Body = (Process p) => { };
                PProcess.BeginOutputReadLine().Body = (Process p) => { };
                PProcess.PriorityClassSetProcessPriorityClass().Body = (Process p, ProcessPriorityClass c) =>
                {
                    Assert.AreEqual(c, config.priorityClass);
                };
                PProcess.Start().Body = (System.Diagnostics.Process process) =>
                {

It seems as though Process is still trying to set up something internally. How do I shim over the entire process class?

urasandesu commented 7 years ago

I tried to reproduce the exception, but I couldn't. Hmm...

I guess the above setup code might not be the real cause because the stack trace indicates the location that is NOT in the code(System.Diagnostics.Process.TryPrigDispose(Process, Boolean) is generated automatically by Prig when you setup the Indirection Stub Setting of System.Diagnostics.Process.Dispose(Boolean) method).

Could you try the following steps to find what is the real cause?

  1. Remove the Indirection Stub Setting of System.Diagnostics.Process.Dispose(Boolean) method from System.v4.0.30319.v4.0.0.0.prig.
  2. Enclose Process object by using statement.
  3. Call Process.WaitForExit method explicitly.
xkjyeah commented 7 years ago

Hm fixed it after a bit of mucking around. Can't reproduce it consistently myself.