Skip to content

Commit

Permalink
pkg/testutils: utility functions to facilitate writing Go tests
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Solomon Hykes <[email protected]> (github: shykes)
  • Loading branch information
Solomon Hykes committed Jun 1, 2014
1 parent 4edcbfd commit ca231b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
17 changes: 2 additions & 15 deletions engine/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"github.com/dotcloud/docker/pkg/beam"
"github.com/dotcloud/docker/pkg/testutils"
"io"
"strings"
"testing"
Expand Down Expand Up @@ -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:
}
}
1 change: 1 addition & 0 deletions pkg/testutils/MAINTAINERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Solomon Hykes <[email protected]> (@shykes)
2 changes: 2 additions & 0 deletions pkg/testutils/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -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:
}
}

0 comments on commit ca231b3

Please sign in to comment.