From ca231b3de52f718d96c0ff6e7af40c7c0fade623 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 9 May 2014 21:11:24 -0700 Subject: [PATCH] pkg/testutils: utility functions to facilitate writing Go tests Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- engine/remote_test.go | 17 ++--------------- pkg/testutils/MAINTAINERS | 1 + pkg/testutils/README.md | 2 ++ pkg/testutils/testutils.go | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 pkg/testutils/MAINTAINERS create mode 100644 pkg/testutils/README.md create mode 100644 pkg/testutils/testutils.go diff --git a/engine/remote_test.go b/engine/remote_test.go index e59ac78cc0..1563660c97 100644 --- a/engine/remote_test.go +++ b/engine/remote_test.go @@ -5,6 +5,7 @@ import ( "bytes" "fmt" "github.com/dotcloud/docker/pkg/beam" + "github.com/dotcloud/docker/pkg/testutils" "io" "strings" "testing" @@ -143,21 +144,7 @@ func testRemote(t *testing.T, senderSide, receiverSide func(*Engine)) { receiverSide(receiver.Engine) go receiver.Run() - timeout(t, func() { + testutils.Timeout(t, func() { senderSide(eng) }) } - -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: - } -} diff --git a/pkg/testutils/MAINTAINERS b/pkg/testutils/MAINTAINERS new file mode 100644 index 0000000000..012d27a2e0 --- /dev/null +++ b/pkg/testutils/MAINTAINERS @@ -0,0 +1 @@ +Solomon Hykes (@shykes) diff --git a/pkg/testutils/README.md b/pkg/testutils/README.md new file mode 100644 index 0000000000..a208a90e68 --- /dev/null +++ b/pkg/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/pkg/testutils/testutils.go b/pkg/testutils/testutils.go new file mode 100644 index 0000000000..4655e5844d --- /dev/null +++ b/pkg/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: + } +}