1

I have some tests that are very slow and I want to set as timeout 15 minutes.

As test purpose I have this example:

[Test, Timeout(900000)]
public void Test1()
{
    Thread.Sleep(900001);
}

The test after some time stops without errors.

What's the correct way to do it?

Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
Enri
  • 21
  • 6
  • You say that the tests stops after some time, but is the maximum time exceeded? If it is, are you running your tests under a debugger? In that case, the `[Timeout]` attribute might be suppressed, see [here](http://nunit.org/index.php?p=timeout&r=2.6.3). – Anders Gustafsson Jun 05 '14 at 11:07
  • With a lower timeout i get the error... (Test exceeded Timeout value of 1000ms) So i think it's a problem of "big timeout" – Enri Jun 05 '14 at 14:25

1 Answers1

0

As you know, both Thread.Sleep and NUnit's TimeoutAttribute take time in milliseconds. Specifying times that are 1 millisecond off from each other isn't enough to guarantee a timeout due to thread scheduling and general timer accuracy. See this answer for a little more discussion about the accuracy of Thread.Sleep, and by extension NUnit's timeout thread's accuracy.

Try specifying a larger difference between the two numbers and I suspect you'll see it behaves as you'd expect. For instance, sleep for 900100 milliseconds and leave the timeout value as is. With a timeout of 15 minutes, you won't notice an extra tenth of a second waiting for it to timeout.

Community
  • 1
  • 1
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • @Enri It fails for me (that is, the test exceeds the timeout and the test has a failure status stating this) using Resharper's test runner. What runner are you using, and what version of NUnit? – Patrick Quirk Jun 09 '14 at 14:27
  • you are right with resharper. I suppose that the problem is in the nunit test adapter and in the nunit standalone test runner (nunit.exe) – Enri Jun 09 '14 at 15:16
  • I just verified it is also failing for me in the NUnit console and GUI runners, in both versions I have installed (2.5.10 and 2.6.2). – Patrick Quirk Jun 09 '14 at 15:19
  • With test explore (and nunit adapter) I get "Last Test Run Not Run(Total Run Time 0:15:01)". Very strange :( – Enri Jun 10 '14 at 15:21
  • I see what you see when I use the NUnit test adapter, so it could very possibly be a bug in that somehow. I was seeing other strange behavior trying to use it as well (it wouldn't run some tests to the end, failing them after 5 seconds), so I personally don't have a lot of confidence in it after that. – Patrick Quirk Jun 12 '14 at 13:40
  • Also, see [this issue](https://github.com/nunit/nunit-vs-adapter/issues/24) on NUnit's Github page, it seems you're not the first to encounter long-running test issues. It might be worth adding a comment to that issue or opening a new one since no one mentions the `Timeout` attribute there. – Patrick Quirk Jun 12 '14 at 14:13