|
|
CallbackWaiter
The CallbackWaiter class is a generic async test harness. It can generate handlers on-demand to match the delegates that you need. And it will wait for one of the events to occur or a specified timeout.
The CallbackWaiter can generate mock event handlers which records all the events into a queue for later validation/checking.
Code sample
CallbackWaiter lets you write this kind of code, to test some code involving asynchronous callbacks (such as service.onProgress and service.onComplete).
public void TestFastService()
{
Service service = new AsyncService(); // your async service which you want to unittest
CallbackWaiter callback = new CallbackWaiter();
service.onProgress += callback.MakeHandler<EventHandler<EventArgs>>("onProgress"); // generate a handler with any type
service.onComplete += callback.MakeHandler<AnotherDelegateType>("onComplete"); // generate another handler with any type
service.DoAsyncStuff();
EventMessage eventMessage = callback.Wait(1000); // wait for a callback or a timeout, whichever comes first
// The EventMessage has the following information:
// - is it an event or a timeout (using .IsEvent())
// - which event was it, in case you wait on multiple events (using .EventName)
// - what were the arguments of the event (using [0], [1], etc. and casting appropriately)
Assert.IsTrue(eventMessage.IsEvent());
Assert.AreEqual("onProgress", eventMessage.EventName);
Assert.AreEqual(2, eventMessage.Length);
Assert.AreEqual(service, (Service)eventMessage[0]);
Assert.AreEqual(service.eventArgs, (EventArgs)eventMessage[1]);
}
Download
You can browse the code or check it out of SVN.
Sign in to add a comment
