Build/Test Issues

WaitHandle.WaitAll() on STA thread

Started by jamesmus on 8,769 views

It seems that NCrunch runs unit tests on an STA thread by default. This causes unit test failures where the code under test (or the unit test code for that matter) relies on WaitAll().

Is there a way to configure the threading model used for test threads?

Thanks

James.
Hi James,

The behaviour for this is likely to be test runner specific, as test runners will often implement different behaviour in this area underneath NCrunch's integration points. Which framework are you having problems with?

One way that comes to my mind would be to spawn your own thread inside the test and specifically flag it as MTA. Though I would also prefer to fix this properly..


Cheers,

Remco
The tests are written against NUnit. The tests pass when run from the NUnit UI, Resharper and NUnit console.
Thanks, I'll do some further testing around here and will see if I can get it sorted for the next release.
Here's a repo, in case it helps. This test will fail in NCrunch, but works elsewhere (eg. Resharper, other continuous testing tools):


[Test]
public void TryWaitAll()
{
    Func<TimeSpan, ManualResetEvent> runThread = t =>
    {
        var mre = new ManualResetEvent(false);
        var thread = new Thread(() =>
            {
                Thread.Sleep(t);
                mre.Set();
            });
        thread.Start();
        return mre;
    };
    
    WaitHandle.WaitAll(
        new WaitHandle[]
            {
                runThread(TimeSpan.FromSeconds(1)),
                runThread(TimeSpan.FromSeconds(2)),
            });
}

Edited

Post a reply

Log in to reply.