Proof of concept using `WaitForResult` in tests

This commit is contained in:
William Tisäter 2014-05-07 02:48:25 +02:00
parent a1c66d68c8
commit 81b572cb31
2 changed files with 32 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package consul
import (
"fmt"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/consul/consul/structs"
"net/rpc"
"os"
@ -35,12 +36,12 @@ func TestCatalogRegister(t *testing.T) {
t.Fatalf("err: %v", err)
}
// Wait for leader
time.Sleep(100 * time.Millisecond)
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
testutil.WaitForResult(func() (bool, error) {
err := client.Call("Catalog.Register", &arg, &out)
return err == nil, err
}, func(err error) {
t.Fatalf("err: %v", err)
}
})
}
func TestCatalogRegister_ForwardLeader(t *testing.T) {

26
testutil/wait.go Normal file
View File

@ -0,0 +1,26 @@
package testutil
import (
"time"
)
type testFn func() (bool, error)
type errorFn func(error)
func WaitForResult(test testFn, error errorFn) {
retries := 500 // 5 seconds timeout
for retries > 0 {
time.Sleep(10 * time.Millisecond)
retries--
success, err := test()
if success {
return
}
if retries == 0 {
error(err)
}
}
}