views:

56

answers:

1

Is there a way to unit test WaitHandle.WaitAll() when using Visual Studio's built-in unit testing solution. When I try and run a test that uses this function within Visual Studio the test fails and when examining the test results the following error is displayed:

WaitAll for multiple handles on a STA thread is not supported

I'd like to be able to unit test the use of WaitAll() since an increasing amount of my API's code base is now moving to an IAsyncResult pattern as opposed to other means of doing multi-threaded operations.

+1  A: 

You may have two problems. The first is the one you stated: You cannot wait on multiple Wait Handles in an STA thread (the MSTest thread apartment state). We can fix that with a manually created MTA thread.

var thread = new Thread(new ThreadStart(action));
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
thread.Join();

The environment also has a maximum wait handle limit. In .NET 2.0, it appears to be hard coded to 64. Waiting on more than the limit will produce a NotSupportedException. You can use an extension method to wait on all wait handles in chunks.

public static void WaitAll<T>(this List<T> list, TimeSpan timeout)
    where T : WaitHandle
{
    var position = 0;
    while (position <= list.Count)
    {
        var chunk = list.Skip(position).Take(MaxWaitHandles);
        WaitHandle.WaitAll(chunk.ToArray(), timeout);
        position += MaxWaitHandles;
    }
}
Anthony Mastrean