title: How accurate is Thread.Sleep(TimeSpan)?
body: <p>I've come across a unit test that is failing intermittently because the time elapsed isn't what I expect it to be.</p>
  
  <p>An example of what this test looks like is:</p>
  
  <pre><code>Stopwatch stopwatch = new Stopwatch();
  stopwatch.Start();
  
  TimeSpan oneSecond = new TimeSpan(0, 0, 1);
  
  for(int i=0; i&lt;3; i++)
  {
      Thread.Sleep(oneSecond);
  }
  
  stopwatch.Stop();
  
  Assert.GreaterOrEqual(stopwatch.ElapsedMilliseconds, 2999);
  </code></pre>
  
  <p>Most of the time this passes but it has failed on at least on one occasion failed because:</p>
  
  <p>Expected: greater than or equal to 2999
  But was: 2998</p>
  
  <p>I don't understand how it could possibly be less than 3 seconds. Is there an accuracy issue with Thread.Sleep or maybe Stopwatch that I'm not aware of?</p>
  
  <p>Just as an update to some of the questions below. The scenario that is being unit tested is a class that allow's one to call a method to perform some action and if it fails wait a second and recall that method. The test shown above is just an approximation of what is happening.</p>
  
  <p>Say I wanted to call a method DoSomething()...but in the event of an exception being thrown by DoSomething() I want to be able to retry calling it up to a maximum of 3 times but wait 1 second between each attempt. The aim of the unit test, in this case, is to verify that when we requested 3 retries with 1 second waits between each retry that the total time taken is greater than 3 seconds.</p>