title: C#: How to test a basic threaded worker class
body: <p>I'm dipping my toes in how to test multi-threaded stuff, but not quite sure how to get started. I'm sure I will figure more stuff out easier if I could just get stuff going, so I was wondering if someone could help me write an NUnit test case for this simple class:</p>
  
  <pre><code>class Worker
  {
      public event EventHandler&lt;EventArgs&gt; Done = (s, e) =&gt; { };
  
      public void StartWork()
      {
          var thread = new Thread(Work) { Name = "Worker Thread" };
          thread.Start();
      }
  
      private void Work()
      {
          // Do some heavy lifting
          Thread.Sleep(500);
          Done(this, EventArgs.Empty);
      }
  }
  </code></pre>
  
  <p>What I would like to test is simply: Is the <code>Done</code> event raised when it finishes. I would have no problems if it was synchronous, but not sure where to even begin when it is not. A simple test if it wasn't multi-threaded (and the <code>Work</code> method wasn't private) could be:</p>
  
  <pre><code>[TestFixture]
  class WorkerTests
  {
      [Test]
      public void DoWork_WhenDone_EventIsRaised()
      {
          var worker = new Worker();
  
          var eventWasRaised = false;
          worker.Done += (s, e) =&gt; eventWasRaised = true;
  
          worker.Work();
          Assert.That(eventWasRaised);
      }
  }
  </code></pre>
  
  <p>Any pointers?</p>