使用nUnit测试死锁 [英] Testing for a deadlock with nUnit

查看:120
本文介绍了使用nUnit测试死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试和nUnit(2.48)的新手。我想写一个测试方法,其中失败的案例是死锁。这可能吗?显然,nUnit默认情况下不知道该方法需要执行多长时间,因此,如果我花费的时间超过我定义的时间,我是否必须编写代码来在单独的线程上进行工作,然后中止并抛出异常?有没有更好的方法?

I'm new to unit testing and nUnit (2.48). I'd like to write a test method where the failure case is that it deadlocks. Is this possible? Obviously nUnit doesn't know by default how long the method should take to execute, so would I have to write code to do the work on a separate thread and then abort it and throw and exception if it took longer than some time I define? Is there a better way to do this?

谢谢

推荐答案

有可能,但可能并非最佳选择。单元测试并不是真正适合测试并发行为,不幸的是,没有多少合适的测试方法。

It is possible but it might not be the best thing to do. Unit tests aren't really suitable for testing concurrency behaviour, unfortunately there aren't many test-methods that are suitable.

NUnit对线程不做任何事情。您可以编写开始多个线程的测试,然后测试它们的交互。

NUnit doesnt do anything with threads. You could write tests that start several threads and then test their interaction. But these would start to look more like integration tests than unit tests.

另一个问题是,死锁行为通常取决于线程的调度顺序。因此,这将很难编写结论性测试以测试某个死锁问题,因为您对线程调度没有任何控制权,这是由操作系统完成的。您可能会得到有时在多核处理器上失败但在单核处理器上总是成功的测试。

Another problem is that deadlock behaviour usually depends on the order threads are scheduled in. So it would be hard to write a conclusive test to test a certain deadlock issue because you don't have any control over the thread scheduling, this is done by the OS. You could end up with tests that sometimes fail on multicore processors but always succeed on single core processors.

这篇关于使用nUnit测试死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆