Thread.Sleep in Tests Is Evil

Thread.Sleep in Tests Is Evil

Do you have calls to Thread.Sleep in your test code? If you’re not sure, you can easily find out by opening up the project in Visual Studio and running Find in Files (ctrl-shift F):


Thread.Sleep will, not surprisingly, dramatically slow down your test suite. The primary reason is that it’s waiting longer than it has to, every time. Consider an example. You have an integration test that depends on an API call completing successfully. Usually the call takes a hundred milliseconds or so, but sometimes it takes a couple of seconds. You don’t want to have your test fail just because the call was a little slow, so you make sure it only fails when something is really wrong by adding

This falls into the “better safe than sorry” category and at least lets you avoid having a test that intermittently fails for no apparent reason. But it makes your test suite slow. If the call usually takes 100ms and you are waiting 5000ms every time, this test is now 50 times slower than it should be in the typical case. Instead of forcing your test to always wait a certain amount of time, you should write the test so that it waits up to that amount of time, but if the thing you’re waiting for happens, stop waiting!

Using ManualResetEvent

The ManualResetEvent lets you communicate between your asynchronous or multi-threaded code and your unit test. There are three simple steps to using ManualResetEvent in your test code.

  1. Create the ManualResetEvent; pass false to its constructor – this indicates the signal you’re waiting for hasn’t yet been sent.
  2. Trigger the event when your async code completes by calling .Set()
  3. Wait to Assert in your test until the signal (Set is called) by calling WaitOne(). You can optionally supply a timeout.

As a simple example, consider a case in which your test code needs to trigger an event that occurs on a separate thread, and then wait to ensure the callback occurs. One approach to this would be to trigger the event, then Thread.Sleep, but as I’ve already pointed out this is an evil antipattern. Instead, look at the following code, which tests the MemoryCache object in ASP.NET Core:

The above test provides a lambda expression for the callback that has access to the ManualResetEvent, pause. Calling its Set method will immediately cause the call to WaitOne to return true. If the timeout value provided to WaitOne (500ms in this case) is reached, it will return false. In this way, the test never waits longer than it needs to for its dependent multi-threaded code to execute. You can see several more examples like this one in the documentation of ASP.NET Core MemoryCache that I authored.

  • Daniel Hardt

    i always have build a TaskCompletionSource-Wrapper and use a async/await pattern for this kind of unit test.

    something like, only with a implementation of timeout with a cancellationToken:

    public Task FiresCallback()
    var myAsyncFunc = ()=>
    var tsc = new TaskCompletionSource();
    _memoryCache.Set(_cacheKey, _cacheItem,
    new MemoryCacheEntryOptions()
    (key, value, reason, substate) =>
    var result = $”‘{key}’:'{value}’ was evicted because: {reason}”;
    return tsc.Task;
    _result = await myAsyncFunc.Invoke();

    Assert.Equal(“‘key’:’value’ was evicted because: Removed”, _result);

    • ardalis

      That’s another good approach – thanks for sharing!