Share via


2009 Advent Calendar December 16th

So far I'm pretty pleased with how the ImportantObject is protected by the ImportantProvider but we're dealing with thread safety and how can we be sure the MutexLock class really does what it is supposed to do? We should put that class under its own set of tests. So let's start with a simple one:

    1:      public class Given_an_unlocked_MutexLock
   2:      {
   3:          private MutexLock _lock = new MutexLock();
   4:   
   5:          [Fact(Timeout=1000)]
   6:          void It_should_be_possible_to_lock()
   7:          {
   8:              _lock.Lock();
   9:          }
  10:      }

With implementation:

    1:      public class MutexLock : Lock
   2:      {
   3:          private readonly Mutex _lock = new Mutex();
   4:   
   5:          public void Lock()
   6:          {
   7:              _lock.WaitOne();
   8:          }
   9:   
  10:          public void Unlock()
  11:          {
  12:              _lock.ReleaseMutex();
  13:          }
  14:      }