diff --git a/testutils/MAINTAINERS b/testutils/MAINTAINERS new file mode 100644 index 0000000..012d27a --- /dev/null +++ b/testutils/MAINTAINERS @@ -0,0 +1 @@ +Solomon Hykes (@shykes) diff --git a/testutils/README.md b/testutils/README.md new file mode 100644 index 0000000..a208a90 --- /dev/null +++ b/testutils/README.md @@ -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. diff --git a/testutils/testutils.go b/testutils/testutils.go new file mode 100644 index 0000000..4655e58 --- /dev/null +++ b/testutils/testutils.go @@ -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: + } +}