1
我有一堆单元测试工作在一个异步执行某些任务的类上。目前我设置了这个类,然后执行这个函数。然后我进入阻塞状态,等待执行完成,然后测试我需要的数据。有没有更好的方法可以做到这一点?测试一个使用异步执行的类
到目前为止,我的测试类似于此:
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);
发生什么事情是我有一个虚拟处理器,通过重复计时器异步执行代码,当执行完成时关闭。我正在测试我的处理器指令集的特定方面,方法是构建简单的程序,逐个加载我的虚拟机,然后运行异步执行并在返回时测试结果。我想我可以强制它同步运行,并将asynchronus部分分离出来作为单独的测试... – RCIX 2009-10-05 02:14:01
这也会加速您的测试,因为您的时序循环应该可以通过配置(某种类型)进行更改。 – 2009-10-05 03:07:35