Windows Vista Beta | WinVistaBeta.com - Message | Creating Unit Test for Function which uses recursive dispatcher

September 07, 2008  
Subject: Creating Unit Test for Function which uses recursive dispatcher
Group: microsoft.public.windows.developer.winfx.avalon
Date: 4/9/2008 1:42:00 AM
From: =?Utf-8?B?TWlrZSBE?= [Email Address Protection]

Hi,

I'm having difficulty creating a unit test - perhaps some of you more
experienced WPF guys out there can help out a bit. The method I'm testing
(let's call it fadeVolume) calls itself recursively with a Dispatcher 60
times, sleeping for 50 milliseconds on each iteration (this is for a volume
fade over the course of 3 seconds).

In my unit test file,

1) I invoke fadeVolume (a private method) by an accessor class which I have
created.

VolumeFade_Accessor = new VolumeFade_Accessor();
accessor.volumeFade(); // should take 3 seconds
performEvents(10000); // should wait while the previous statement iterates

2) performEvents does what the algorithm below shows. The wait time of 10
seconds is there just for the sake of giving my 3 second fade all the time it
needs to execute.

private void performEvents (int wait Time)
{
System.Threading.Thread.Sleep(waitTime);
DispatcherFrame frame = new DispatcherFrame();

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new
DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}


So the problem lies here.... even though I'm waiting for 10 seconds after I
call volumeFade from my unit test file, my volumeFade method never iterates
60 times. Actually, it recursively calls itself just once, which seems very
slow to me, considering that there's only a 50 millisecond sleep for each
iteration.

I know this method works normally, as the program normally performs as
expected. It only goes wrong in this test case scenario. Can anyone give me
any advice about unit testing a function which calls a dispatcher
recursively?? Thanks for any help in advance.

Cheers,
Mike

--
Mike D

Back