开发者

How do I get JUnit to be consistent when timing out?

JUnit @Tests have the useful ability to specify a timeou开发者_JS百科t argument so that a poorly written program gets killed automatically if it takes longer than timeout seconds. The problem is this uses clock time instead of CPU time - so a test will actually run for different amounts of time between runs. For example, a test might run for 1.901 or 1.894 seconds for a 2 second timeout, depending on what other jobs are running on the CPU at the same time.

Can I specify a timeout or similar that would be consistent across runs? (Extensions to this question include: consistent across machines, etc)


I think your best approach is to set the JUnit timeout to ~2-3x your required timeout and do your own benchmarking using a ThreadMXBean to measure CPU time if available. You can then fail if you've exceeded your timeout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜