2009 Advent Calendar December 23rd

And the last test refactored to remove the slow tests:

    1:      public class Given_a_locked_MutexLock
   2:      {
   3:          private class FakeMutexWrapper : MutexWrapper
   4:          {
   5:              public bool Locked { get; private set; }
   6:   
   7:              public static int NumberOfLocks { get; private set; }
   8:              public static int NumberOfUnlocks { get; private set; }
   9:   
  10:              public static void Reset()
  11:              {
  12:                  NumberOfLocks = 0;
  13:                  NumberOfUnlocks = 0;
  14:              }
  15:   
  16:              public FakeMutexWrapper()
  17:              {
  18:                  Locked = false;
  19:              }
  20:   
  21:              public override void WaitOne()
  22:              {
  23:                  if (!Locked)
  24:                  {
  25:                      Locked = true;
  26:                      ++NumberOfLocks;
  27:                  }
  28:              }
  29:   
  30:              public override void ReleaseMutex()
  31:              {
  32:                  Locked = false;
  33:                  ++NumberOfUnlocks;
  34:              }
  35:          }
  36:   
  37:          private MutexLock<FakeMutexWrapper> _lock = new MutexLock<FakeMutexWrapper>();
  38:          
  39:          public Given_a_locked_MutexLock()
  40:          {
  41:              _lock.Lock();
  42:              FakeMutexWrapper.Reset();
  43:          }
  44:   
  45:          [Fact]
  46:          void It_should_not_take_the_lock()
  47:          {
  48:              _lock.Lock();
  49:              Assert.Equal(0, FakeMutexWrapper.NumberOfLocks);
  50:          }
  51:   
  52:          [Fact]
  53:          void It_should_take_lock_when_released()
  54:          {
  55:              _lock.Unlock();
  56:              _lock.Lock();
  57:              Assert.Equal(1, FakeMutexWrapper.NumberOfLocks);
  58:          }
  59:      }

So great! We now have fast test. But... Once again we're not testing the MutexWrapper. We'll take a look at that tomorrow.