pkg/testutils: utility functions to facilitate writing Go tests

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
Upstream-commit: ca231b3de52f718d96c0ff6e7af40c7c0fade623
Component: engine
This commit is contained in:
Solomon Hykes
2014-05-09 21:11:24 -07:00
parent e0357c5a9f
commit 0311fdd2c0
4 changed files with 28 additions and 15 deletions

View File

@ -0,0 +1 @@
Solomon Hykes <s@docker.com> (@shykes)

View File

@ -0,0 +1,2 @@
`testutils` is a collection of utility functions to facilitate the writing
of tests. It is used in various places by the Docker test suite.

View File

@ -0,0 +1,23 @@
package testutils
import (
"testing"
"time"
)
// Timeout calls f and waits for 100ms for it to complete.
// If it doesn't, it causes the tests to fail.
// t must be a valid testing context.
func Timeout(t *testing.T, f func()) {
onTimeout := time.After(100 * time.Millisecond)
onDone := make(chan bool)
go func() {
f()
close(onDone)
}()
select {
case <-onTimeout:
t.Fatalf("timeout")
case <-onDone:
}
}