Exception in multithreaded test and test still passes

Hello,

I have the following xUnit 2.0 test that throws a SynchronizationLockExceptionObject, however the Resharper 9  2.1.7 extension shows the test as passing with no indication that anything went wrong:
(Yes, the test has no asserts, it has been stripped down to a minimal working example)

[Fact]
public void TestLock()
{
    bool locker = false;
    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += (s, e) => { Monitor.Pulse(locker); };
    bw.RunWorkerAsync();
}


The problem maybe related to exceptions thrown in a multithreaded environment because the following test fails with the exception as expected:

[Fact]
public void TestLock()
{
    bool locker = false;
    Monitor.Pulse(locker);
}

Thanks,
Chris

4 comments
Comment actions Permalink

Have you tested this with the console and VS runners? What behaviour do they show?

0
Comment actions Permalink

I've just tried this with the VS and console runners, and all three (inc. ReSharper) behave the same - the async version on the BackgroundWorker, as you've shown it, passes. You mentioned (in email) that the console threw an exception. Can you verify that, please?

0
Comment actions Permalink

Hi Matt,

Indeed you are right, I'm having some trouble replicating the same behavour now in the console runner.
The console runner is now showing the test as passing. I have found one reason for this is becasue the test finishes before the background worker gets a chance to run. However, even when the background worker does run, the exception is no longer being displayed in the console unless I manually print it. See the example below.
As you have found out, all three runners; console, VS and Resharper xUnit extension, all show the test as passing, even though an exception has occured. Thus, it is not a Resharper extension issue only

object locker = new object();
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (s, e) =>
{
    Console.WriteLine("Running");
    Monitor.Pulse(locker);
        
};
bw.RunWorkerCompleted += (s, e) =>
{
    Console.WriteLine(e.Error);
    Console.WriteLine("Done");
} ;
bw.RunWorkerAsync();
Thread.Sleep(1000);



However, with a foreground Thread, the console runner will halt with an exception, whereas the VS runner and Resharper extension will show the test as passing:

object locker = new object();
Thread T = new Thread(() => { Monitor.Pulse(locker); } );
T.Start();
Thread.Sleep(1000);

Thanks.

0
Comment actions Permalink

Yeah, this looks like it's an xunit issue. I'm not sure why console would behave differently to VS and R# - might be that UI processes set up thread handling and unhandled exceptions differently. It might be worth following up with this xunit GitHub issue: https://github.com/xunit/xunit/issues/157

0

Please sign in to leave a comment.