Handling background threads using NUnit and Rhino Mocks

I’ve just started using NUnit and Rhino Mocks, so this is not necessarily the best way to do things, but it works for me.

I have a class which has a method which kicks off a background thread.  This thread has an infinite loop — for each loop it accepts a request and services the request.

I needed to test an instance of the class, for one specific incoming request.  There are two issues — how to know when the object’s background thread has finished servicing the request, and how to stop the background thread from looping back and accepting another request.

This is what I ended up doing:

public class ServerTest {

  public void SomeTest() {

    bool done = false;

    MockRepository mocks = new MockRepository();

    using(mocks.Record()) {

      // ... lots of 'Expects'

      // This is the final call which gets made in the background thread
      // to complete the handling of the request
      Expect.Call(someObject.LastMethod).Do((Expect.Action)delegate() {
        done = true;
    using(mocks.Playback()) {
      Server server = new Server();
      int count = 0;
      while(!done && count < 10) {

The key point is that in the test’s action associated with the last method that gets invoked as part of your background thread’s processing loop, you need to set a flag so that the test knows that the last method has executed, and also abort the current thread so that the background thread doesn’t loop back, and consume another request.

2 thoughts on “Handling background threads using NUnit and Rhino Mocks

Leave a Reply

Your email address will not be published. Required fields are marked *