Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

WaitHandle.WaitAll() on STA thread
jamesmus
#1 Posted : Monday, March 12, 2012 11:25:53 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/12/2012(UTC)
Posts: 2

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.
Remco
#2 Posted : Monday, March 12, 2012 8:28:25 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
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
jamesmus
#3 Posted : Monday, March 12, 2012 8:48:02 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/12/2012(UTC)
Posts: 2

The tests are written against NUnit. The tests pass when run from the NUnit UI, Resharper and NUnit console.
Remco
#4 Posted : Monday, March 12, 2012 9:23:16 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Thanks, I'll do some further testing around here and will see if I can get it sorted for the next release.
whirly
#5 Posted : Thursday, March 15, 2012 9:49:47 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/9/2012(UTC)
Posts: 5
Location: London

Was thanked: 3 time(s) in 3 post(s)
Here's a repo, in case it helps. This test will fail in NCrunch, but works elsewhere (eg. Resharper, other continuous testing tools):

Code:

[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)),
            });
}
1 user thanked whirly for this useful post.
Remco on 3/15/2012(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.039 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download