title: Correct way to test code that uses time.Ticker?
body: <p>I'd like your advice on the correct way to test code that uses time.Ticker</p>
  
  <p>For instance, let's say I have a countdown timer like below (just an example I thought up for the purposes of this question): </p>
  
  <pre><code>type TickFunc func(d time.Duration)
  
  func Countdown(duration time.Duration, interval time.Duration, tickCallback TickFunc) {
      ticker := time.NewTicker(interval)
      for remaining := duration; remaining &gt;= 0; remaining -= interval {
          tickCallback(remaining)
          &lt;-ticker.C
      }
  
      ticker.Stop()
   }
  </code></pre>
  
  <p><a href="http://play.golang.org/p/WJisY52a5L" rel="noreferrer">http://play.golang.org/p/WJisY52a5L</a></p>
  
  <p>If I wanted to test this, I'd want to provide a mock so that I can have tests that run quickly and predictably, so I'd need to find a way to get my mock into the Countdown function. </p>
  
  <p>I can think of a few ways to do this: </p>
  
  <p>Create a Ticker interface and a first class function internal to the package that I can patch for the purposes of testing: <a href="http://play.golang.org/p/oSGY75vl0U" rel="noreferrer">http://play.golang.org/p/oSGY75vl0U</a></p>
  
  <p>Create a Ticker interface and pass an implementation directly to the Countdown function: 
  <a href="http://play.golang.org/p/i67Ko5t4qk" rel="noreferrer">http://play.golang.org/p/i67Ko5t4qk</a></p>
  
  <p>If I do it the latter way, am I revealing too much information about how Countdown works and making it more difficult for potential clients to use this code? Instead of giving a duration and interval, they have to construct and pass in a Ticker. </p>
  
  <p>I'm very interested in hearing what's the best approach when testing code like this? Or how you would change the code to preserve the behaviour, but make it more testable?</p>
  
  <p>Thanks for your help!</p>