DEV Community

Benjamin DeCoste
Benjamin DeCoste

Posted on

3

Testing time with Golang

We can all agree that TDD is the bees knees, right? Something that can trip newbies up with golang is writing tests against a function that has a timeout.

Often when waiting for a result over a channel, it can make sense to add a timeout so we don't wait forever. Consider the following code

func doSomething(done chan struct{}) {
  // simulate an operation that takes too long
  time.Sleep(time.Second * 5)
  done <- struct{}
}

// We need to test this
func MyCode() {
  c := make(chan struct{})

  select {
  case <- done:
    // hurray!
  case <- time.After(time.Second * 5)
    // oh no!
}
Enter fullscreen mode Exit fullscreen mode

Writing a unit test that exercises our timeout logic and doesn't take 5 seconds to run can seem difficult at first, but it is actually quite simple. We can use something called remote function overriding to declare a variable that we can override in our test.

var after = time.After
Enter fullscreen mode Exit fullscreen mode

And our switch statement changes like so

select {
  case <- done:
    // hurray!
  case <- after(time.Second * 5)
    // oh no!
}
Enter fullscreen mode Exit fullscreen mode

Now, in our tests, we can easily change the definition of after, to make it happen instantly

func TestSomething(t *testing.T) {
  after = func(d time.Duration) <-chan time.Time {
    return time.After(0)
  }

  // your test code here
}
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️