mirror of
https://github.com/status-im/consul.git
synced 2025-02-27 21:00:44 +00:00
Proof of concept using WaitForResult
in tests
This commit is contained in:
parent
8f37f967e0
commit
028bc7e1b4
@ -2,6 +2,7 @@ package consul
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/hashicorp/consul/testutil"
|
||||||
"github.com/hashicorp/consul/consul/structs"
|
"github.com/hashicorp/consul/consul/structs"
|
||||||
"net/rpc"
|
"net/rpc"
|
||||||
"os"
|
"os"
|
||||||
@ -35,12 +36,12 @@ func TestCatalogRegister(t *testing.T) {
|
|||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for leader
|
testutil.WaitForResult(func() (bool, error) {
|
||||||
time.Sleep(100 * time.Millisecond)
|
err := client.Call("Catalog.Register", &arg, &out)
|
||||||
|
return err == nil, err
|
||||||
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
|
}, func(err error) {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCatalogRegister_ForwardLeader(t *testing.T) {
|
func TestCatalogRegister_ForwardLeader(t *testing.T) {
|
||||||
|
26
testutil/wait.go
Normal file
26
testutil/wait.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user