瀏覽代碼

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 11 年之前
父節點
當前提交
ca231b3de5
共有 4 個文件被更改,包括 28 次插入15 次删除
  1. 2 15
      engine/remote_test.go
  2. 1 0
      pkg/testutils/MAINTAINERS
  3. 2 0
      pkg/testutils/README.md
  4. 23 0
      pkg/testutils/testutils.go

+ 2 - 15
engine/remote_test.go

@@ -5,6 +5,7 @@ import (
 	"bytes"
 	"bytes"
 	"fmt"
 	"fmt"
 	"github.com/dotcloud/docker/pkg/beam"
 	"github.com/dotcloud/docker/pkg/beam"
+	"github.com/dotcloud/docker/pkg/testutils"
 	"io"
 	"io"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
@@ -143,21 +144,7 @@ func testRemote(t *testing.T, senderSide, receiverSide func(*Engine)) {
 	receiverSide(receiver.Engine)
 	receiverSide(receiver.Engine)
 	go receiver.Run()
 	go receiver.Run()
 
 
-	timeout(t, func() {
+	testutils.Timeout(t, func() {
 		senderSide(eng)
 		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:
-	}
-}

+ 1 - 0
pkg/testutils/MAINTAINERS

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

+ 2 - 0
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.

+ 23 - 0
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:
+	}
+}