The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Making sure asynchronous unit tests don’t cause a deadlock

In my previous blog post I wrote about creating asynchronous unit tests for Silverlight using the Microsoft Silverlight Unit Test Framework. This asynchronous testing capability that the Microsoft Silverlight Unit Test Framework has in the box lets us create some pretty advanced testing capabilities.

The problem with asynchronous tests.

While the async test pattern is very powerful it also has a drawback. This drawback is that the EnqueueTestComplete() must be executed or the test will never complete and the whole test run will halt. And while the test runner will tell you which test is executing it will not tell you where it is waiting, all it does is just sit there and wait.

Take the following test for example. Because the EnqueueConditional() action always returns false it is never going to end.

[TestMethod]
[Asynchronous]
public void TestThatNeverFinishes()
{
    // Do some asynchronous work

    EnqueueConditional(() => false);
    EnqueueTestComplete();
}

Now a Timeout attribute telling the Microsoft Silverlight Unit Test Framework to abort the test if it takes longer than a predefined amount of time would be nice. In fact there is even a Timeout attribute as part of the Microsoft.VisualStudio.TestTools.UnitTesting namespace but adding the Timeout attribute to a SilverlightTest has no effect Sad

The solution

The solution is not very difficult but it does mean having to check for the timeout ourselves. In fact just adding an Assert to every EnqueueConditional() to make sure we don’t timeout will do the trick just fine. The only downside is that we have to do so in very check. If we change the test to the following the test will timeout, with an error, if it takes to long.

[TestMethod]
[Asynchronous]
public void TestThatNeverFinishes()
{
    DateTime startTime = DateTime.Now;

    // Do some asynchronous work

    EnqueueConditional(() =>
    {
        Assert.IsTrue((DateTime.Now - startTime) < TimeSpan.FromSeconds(10), 
            "Timeout on condition 'return false;'");
        return false;
    });

    EnqueueTestComplete();
}

The result is a “nice” error indicating the test took too long.

image

 

Nice, now if only the Microsoft Silverlight Unit Test Framework would support the Timeout attribute it would be even better.

Enjoy!

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Published Sun, Dec 28 2008 17:49 by Maurice
Filed under: , ,

Comments

# Keep Up The Power@ Monday, January 19, 2009 11:14 AM

I think you are thinking like sukrat, but I think you should cover the other side of the topic in the post too...

# rental cars@ Monday, June 22, 2009 8:02 AM

Man, that’s great…Thanks for providing such a good info

# Cash loans today@ Tuesday, June 23, 2009 4:40 PM

I’m new to the site and just purchased lots of items last night, and still have not received an email with the items.

How long does it normally take to get the items? I understood that as soon as I paid everything would be emailed

to me. Just wondering……

Thanks

# health insurance@ Wednesday, June 24, 2009 3:22 PM

Not that I’m impressed a lot, but this is a lot more than I expected when I found a link on Delicious telling that the info is awesome. Thanks.

# paris restaurants@ Thursday, June 25, 2009 1:02 AM

Ada yang bersedih,..tentunya ada yang bergembira dengan kekalahan perancis,…termasuk saya yang bergembira,…Bravo Italy

Slam kenal Bang :)

# Tylsmtoc@ Tuesday, July 14, 2009 2:47 AM

CsEAor