views:

71

answers:

2

I have a bunch of unit tests that work on a class which executes some tasks asynchronously. Currently I set up the class, then execute the function in question. I then go into a blocking wait until the execution is completed, then test the data I need to. Is there a better way that I can do this?

So far my tests look similar to this:

    vp.Reset(); //vp is my virtual machine
    bool wait = true;
    Dictionary<short, Command> commands = new Dictionary<short, Command>();
    commands.Add(0, CommandFactory.CreateInputCommand(0, 0));
    commands.Add(1, CommandFactory.CreateHaltCommand());
    vp.OnHalted += () =>
    {
        wait = false;
    };
    vp.InputChannels[0] = () => { return 2; };
    vp.CurrentProgram = commands;
    vp.ExecuteTillHalt();//asynchronous execution of program. There's also a way to make it execute synchronously
    while (wait) ;
    Assert.AreEqual(vp.Registers[0], 2);
+2  A: 

I see two tests, one to test that an asychronous action is launched (which can be done with a mock) and the second to test that the action that is run asychronously performs correctly when run and you can do the latter synchronously.

Preet Sangha
What happens is i have a virtual processor which executes code asynchronously via a repeating timer, which is turned off when the execution is complete. I am testing specific aspects of the instruction set for my processor by building simple programs, loading my virtual machine with them one at a time, then running the asynchronus execution and testing the results once it returns. I suppose i could force it to run synchronusly and separate out the asynchronus part out into a separate test...
RCIX
That would also speed up your testing as you're timing loops should be alterable by configuration (of some sort).
Preet Sangha
A: 

Is there a way to break this up into two tests?

You can test the asynchronous aspect via the block and then test the data as a separate testable function that doesn't rely on how it's called.

Shaun F