forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
62 lines (54 loc) · 1.25 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package testutil
import (
"fmt"
"testing"
"time"
"github.com/hashicorp/consul/consul/structs"
)
type testFn func() (bool, error)
type errorFn func(error)
const (
baseWait = 1 * time.Millisecond
maxWait = 100 * time.Millisecond
)
func WaitForResult(try testFn, fail errorFn) {
var err error
wait := baseWait
for retries := 100; retries > 0; retries-- {
var success bool
success, err = try()
if success {
time.Sleep(25 * time.Millisecond)
return
}
time.Sleep(wait)
wait *= 2
if wait > maxWait {
wait = maxWait
}
}
fail(err)
}
type rpcFn func(string, interface{}, interface{}) error
func WaitForLeader(t *testing.T, rpc rpcFn, dc string) structs.IndexedNodes {
var out structs.IndexedNodes
WaitForResult(func() (bool, error) {
// Ensure we have a leader and a node registration.
args := &structs.DCSpecificRequest{
Datacenter: dc,
}
if err := rpc("Catalog.ListNodes", args, &out); err != nil {
return false, fmt.Errorf("Catalog.ListNodes failed: %v", err)
}
if !out.QueryMeta.KnownLeader {
return false, fmt.Errorf("No leader")
}
if out.Index == 0 {
return false, fmt.Errorf("Consul index is 0")
}
return true, nil
}, func(err error) {
t.Fatalf("failed to find leader: %v", err)
})
return out
}