Alex Reece
Alex Reece

Reputation: 2026

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

JUnit @Tests have the useful ability to specify a timeout 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)

Upvotes: 2

Views: 281

Answers (1)

Jon Freedman
Jon Freedman

Reputation: 9707

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.

Upvotes: 2

Related Questions