Browse code

pkg/testutils: utility functions to facilitate writing Go tests

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)

Solomon Hykes authored on 2014/05/10 13:11:24
Showing 4 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"bytes"
6 6
 	"fmt"
7 7
 	"github.com/dotcloud/docker/pkg/beam"
8
+	"github.com/dotcloud/docker/pkg/testutils"
8 9
 	"io"
9 10
 	"strings"
10 11
 	"testing"
... ...
@@ -143,21 +144,7 @@ func testRemote(t *testing.T, senderSide, receiverSide func(*Engine)) {
143 143
 	receiverSide(receiver.Engine)
144 144
 	go receiver.Run()
145 145
 
146
-	timeout(t, func() {
146
+	testutils.Timeout(t, func() {
147 147
 		senderSide(eng)
148 148
 	})
149 149
 }
150
-
151
-func timeout(t *testing.T, f func()) {
152
-	onTimeout := time.After(100 * time.Millisecond)
153
-	onDone := make(chan bool)
154
-	go func() {
155
-		f()
156
-		close(onDone)
157
-	}()
158
-	select {
159
-	case <-onTimeout:
160
-		t.Fatalf("timeout")
161
-	case <-onDone:
162
-	}
163
-}
164 150
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+Solomon Hykes <s@docker.com> (@shykes)
0 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+`testutils` is a collection of utility functions to facilitate the writing
1
+of tests. It is used in various places by the Docker test suite.
0 2
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+package testutils
1
+
2
+import (
3
+	"testing"
4
+	"time"
5
+)
6
+
7
+// Timeout calls f and waits for 100ms for it to complete.
8
+// If it doesn't, it causes the tests to fail.
9
+// t must be a valid testing context.
10
+func Timeout(t *testing.T, f func()) {
11
+	onTimeout := time.After(100 * time.Millisecond)
12
+	onDone := make(chan bool)
13
+	go func() {
14
+		f()
15
+		close(onDone)
16
+	}()
17
+	select {
18
+	case <-onTimeout:
19
+		t.Fatalf("timeout")
20
+	case <-onDone:
21
+	}
22
+}