Merge pull request #6145 from shykes/pr_out_pkg_testutils_utility_functions_to_facilitate_writing_go_tests
pkg/testutils: utility functions to facilitate writing Go tests
This commit is contained in:
commit
2f72fdf6ea
5 changed files with 30 additions and 16 deletions
|
@ -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:
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
|
||||
Cristian Staretu <cristian.staretu@gmail.com> (@unclejack)
|
||||
|
|
2
pkg/testutils/MAINTAINERS
Normal file
2
pkg/testutils/MAINTAINERS
Normal file
|
@ -0,0 +1,2 @@
|
|||
Solomon Hykes <s@docker.com> (@shykes)
|
||||
Cristian Staretu <cristian.staretu@gmail.com> (@unclejack)
|
2
pkg/testutils/README.md
Normal file
2
pkg/testutils/README.md
Normal file
|
@ -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
pkg/testutils/testutils.go
Normal file
23
pkg/testutils/testutils.go
Normal file
|
@ -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:
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue