Merge pull request #307 from weaveworks/303-test-cpu

Add -cpu 4 to tests.
This commit is contained in:
Tom Wilkie
2015-07-08 11:23:49 +01:00
5 changed files with 34 additions and 27 deletions

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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)

20
test/poll.go Normal file
View File

@@ -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)
}
}

View File

@@ -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)
}
}