I am using the Silverlight Unit Testing Framework to test some View Manager classes. Some tests require the PropertyChanged events to be fired.
I'm currently using a combination of EnqueueConditional and WaitHandles
Example 1
[TestMethod]
[Asynchronous]
[Timeout(1000)]
public void TestNotificationExample()
{
var manager = new UserManager();
var waitHandle = new ManualResetEvent(false);
manager.PropertyChanged += (sender, propChangArgs) =>
{
waitHandler.Set();
};
manager.DoTheThingThatTriggersNotification();
// The notification event fires aynshronously to this
EnqueueConditional (() => waitHandler.WaitOne(0));
// Enqueue other tests here....
EnqueueTestComplete();
}
This works. But I've got questions nagging at me:
Do I actually need to use a WaitHandle? Would it perform equally as well if I just used a bool?
Example 2
bool fHasFiredEvent = false;
manager.PropertyChanged += (sender, propChangeArgs) =>
{
fHasFiredEvent = true;
}
manager.DoTheThingThatTriggersNotification();
EnqueueConditional (() => fHasFiredEvent);
EnqueueTestComplete();
Or would it be better if I kept the WaitHandle, but lost the TimeoutAttribute and timed out on the Wait?
Example 3
[TestMethod]
[Asynchronous]
public void TestNotificationExample()
{
var manager = new UserManager();
var waitHandle = new ManualResetEvent(false);
manager.PropertyChanged += (sender, propChangArgs) =>
{
waitHandler.Set();
};
manager.DoTheThingThatTriggersNotification();
EnqueueCallback (() => Assert.IsTrue(waitHandler.WaitOne(1000));
EnqueueTestComplete();
}
So now I've written three examples, and they all work. So my final question is
- Which would have the best performance? (Even though the difference is negligible and it's purely academic yada yada yada. It's interesting for its own sake.)
- Do any of the three examples have fundamental flaws?