Waiting for an event to trigger in Silverlight module tests

I use the Silverlight Unit Testing Framework to test some View Manager classes. Some tests require PropertyChanged events to fire .

I am 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(); } 

It works. But I have questions that are above me:

Do I need to use WaitHandle? Will it work the same if I just used bool?

Example 2

 bool fHasFiredEvent = false; manager.PropertyChanged += (sender, propChangeArgs) => { fHasFiredEvent = true; } manager.DoTheThingThatTriggersNotification(); EnqueueConditional (() => fHasFiredEvent); EnqueueTestComplete(); 

Or would it be better if I saved WaitHandle but lost TimeoutAttribute and was confined to 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 have written three examples, and they all work. So my last question is:

  • What would be a better view? (Although the difference is not significant, and this is a purely academic yada yada yada. This is interesting for myself.)
  • Do any of the three examples have fundamental flaws?
+4
source share
1 answer

Without actually running the real code in the three examples, I don't know that I can give an authoritative answer, but my advice would be to use # 2, and well avoid # 1 and # 3.

I shot through the source for the Silverlight Unit Test for Jeff Wilcox, and as far as I remember, it uses a smart, but really terrible hack for EnqueueConditional, i.e. it repeatedly calls the predicate passed to EnqueueConditional () on the timer / background thread, each time checking to see if it returns true. (This is not what you want in the production code, but it is good enough for the test structure, it is logic, I suppose.)

So, if your test takes a couple of seconds, I expect your waitHandler.WaitOne () to either (a) be called many times, blocking each thread as it arrives; or (b) block the flow that other things are supposed to do. I believe a (c) is also possible, i.e. You may be lucky WaitOne () will not block anything important and will be called only once. But, of course, No. 2 is the "standard" way to use this test environment, and if you had no specific reason to introduce more complex WaitHandle logic, I would not try to push the test structure in this direction.

However, if someone wants to poke and give a more authoritative answer, I’m all ears :-).

+5
source

All Articles