title: How to unit test a timer class (adapter pattern)?
body: <p>I'm currently abstracting the concept of timers so that my classes that need one can use mock timers in tests or different implementations in operative mode (e.g. threadpool timers, thread-affine timers, etc.). Therefore, I created this interface:</p>
  
  <pre><code>public interface ITimer : IDisposable
  {
      bool IsEnabled { get; }
      bool IsAutoResetting { get; set; }
      TimeSpan Interval { get; set; }
  
      void Start();
      void Stop();
  
      event EventHandler IntervalElapsed;
  }
  </code></pre>
  
  <p>Now I want to create a wrapper that adapts the <code>System.Threading.Timer</code> class and implements that interface. I want to do it using test-driven development. My class currently looks somewhat like this:</p>
  
  <pre><code>public sealed class ThreadPoolTimer : ITimer
  {
      private readonly Timer _timer;
  
      public bool IsEnabled { get; private set; }
  
      public bool IsAutoResetting { get; set; }
  
      public TimeSpan Interval { get; set; }
  
      public ThreadPoolTimer()
      {
          Interval = this.GetDefaultInterval();
          _timer = new Timer(OnTimerCallback);
      }
  
      public void Dispose()
      {
          _timer.Dispose();
      }
  
      public void Start()
      {
  
      }
  
      public void Stop()
      {
  
      }
  
      private void OnTimerCallback(object state)
      {
          OnIntervalElapsed();
      }
  
      public event EventHandler IntervalElapsed;
  
      private void OnIntervalElapsed()
      {
          var handler = IntervalElapsed;
          if (handler != null)
              handler(this, EventArgs.Empty);
      }
  }
  </code></pre>
  
  <p><strong>My actual question is:</strong> how would you write unit tests that describe the (soft real-time) requirements for the behavior of <code>Start</code>, <code>Stop</code> and <code>IntervalElapsed</code>?</p>
  
  <p>In my opinion, i should use e.g. an <code>AutoResetEvent</code> and check if the event is raised within a certain timespan (maybe +/- 3ms). But writing that code somewhat violates the DAMP (descriptive and meaningful phrases) principle, I think. Is there an easier way to do this?</p>
  
  <p>Should I make the dependency to <code>System.Threading.Timer</code> external and then maybe use a shim for testing purposes? Unfortunately, the .NET timers do not have a common interface (which would make my work obsolete...)</p>
  
  <p>What are your thoughts on that topic? Is there any documentation that I have not found yet and that I should read?</p>
  
  <p>Sorry for having actually more than one question in this post, but this testing of soft real-time requirements is quite interesting, I think.</p>