How accurate is Thread.Sleep(TimeSpan)?
I've come across a unit test that is failing intermittently because the time elapsed isn't what I expect it to be. An example of what this test looks like is: Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TimeSpan oneSecond = new TimeSpan(0, 0, 1); for(int i=0; i<3; i++) { Thread.Sleep(oneSecond); } stopwatch.Stop(); Assert.GreaterOrEqual(stopwatch.ElapsedMilliseconds, 2999); Most of the time this passes but it has failed on at least on one occasion failed because: Expected: greater than or equal to 2999 But was: 2998 I don't understand how it could possibly be less than 3 seconds.