diff --git a/bin/test b/bin/test index 8ed6bfe95..b3b41153f 100755 --- a/bin/test +++ b/bin/test @@ -2,10 +2,10 @@ set -eu -GO_TEST_ARGS="" +GO_TEST_ARGS="-cpu 4 -timeout 10s -tags netgo" SLOW="" if [ $# -eq 1 ] && [ "$1" = "-slow" ]; then - GO_TEST_ARGS="-race -covermode=atomic" + GO_TEST_ARGS="$GO_TEST_ARGS -race -covermode=atomic" SLOW="yes" fi @@ -26,7 +26,7 @@ for dir in $(find . -type f -name '*_test.go' | grep -v '^./.git/' | grep -v '^ GO_TEST_ARGS_RUN="$GO_TEST_ARGS" fi - if ! go test $GO_TEST_ARGS_RUN -timeout 10s -tags netgo $dir ; then + if ! go test $GO_TEST_ARGS_RUN $dir ; then fail=1 fi diff --git a/probe/docker/container_test.go b/probe/docker/container_test.go index d2eb116e4..f940f0c44 100644 --- a/probe/docker/container_test.go +++ b/probe/docker/container_test.go @@ -8,9 +8,12 @@ import ( "net/http" "runtime" "testing" + "time" client "github.com/fsouza/go-dockerclient" + "github.com/weaveworks/scope/probe/docker" + "github.com/weaveworks/scope/test" ) type mockConnection struct { @@ -56,11 +59,10 @@ func TestContainer(t *testing.T) { if err = json.NewEncoder(writer).Encode(&stats); err != nil { t.Error(err) } - runtime.Gosched() // wait for StartGatheringStats goroutine to receive the stats // Now see if we go them - nmd := c.GetNodeMetadata() - if nmd[docker.MemoryUsage] != "12345" { - t.Errorf("want 12345, got %s", nmd[docker.MemoryUsage]) - } + test.Poll(t, 10*time.Millisecond, func() bool { + nmd := c.GetNodeMetadata() + return nmd[docker.MemoryUsage] == "12345" + }, "Failed to get stats") } diff --git a/probe/docker/registry_test.go b/probe/docker/registry_test.go index bd90b3e3b..ea8bc8350 100644 --- a/probe/docker/registry_test.go +++ b/probe/docker/registry_test.go @@ -160,13 +160,11 @@ func TestRegistry(t *testing.T) { defer registry.Stop() runtime.Gosched() - { + test.Poll(t, 10*time.Millisecond, func() bool { have := allContainers(registry) want := []docker.Container{&mockContainer{container1}} - if !reflect.DeepEqual(want, have) { - t.Errorf("%s", test.Diff(want, have)) - } - } + return reflect.DeepEqual(want, have) + }, "Didn't get containers!") { have := allImages(registry) diff --git a/test/poll.go b/test/poll.go new file mode 100644 index 000000000..0b0133c54 --- /dev/null +++ b/test/poll.go @@ -0,0 +1,20 @@ +package test + +import ( + "testing" + "time" +) + +// Poll repeatedly evaluates condition until we either timeout, or it suceeds. +func Poll(t *testing.T, d time.Duration, condition func() bool, msg string) { + deadline := time.Now().Add(d) + for { + if time.Now().After(deadline) { + t.Fatal(msg) + } + if condition() { + return + } + time.Sleep(d / 10) + } +} diff --git a/xfer/collector_internal_test.go b/xfer/collector_internal_test.go index 015dfa2f8..9e406a191 100644 --- a/xfer/collector_internal_test.go +++ b/xfer/collector_internal_test.go @@ -53,7 +53,7 @@ func TestCollector(t *testing.T) { } reports <- r - poll(t, 100*time.Millisecond, func() bool { + test.Poll(t, 100*time.Millisecond, func() bool { return len(concreteCollector.peek().Address.NodeMetadatas) == 1 }, "missed the report") @@ -101,16 +101,3 @@ func testPublisher(t *testing.T, input <-chan interface{}) net.Listener { }() return ln } - -func poll(t *testing.T, d time.Duration, condition func() bool, msg string) { - deadline := time.Now().Add(d) - for { - if time.Now().After(deadline) { - t.Fatal(msg) - } - if condition() { - return - } - time.Sleep(d / 10) - } -}