Adds a little wait after we get the event we were seeking.

This commit is contained in:
James Phillips 2017-01-26 19:05:56 -08:00
parent 3967cfe833
commit dcff5137f9
No known key found for this signature in database
GPG Key ID: 77183E682AC5FC11
2 changed files with 13 additions and 3 deletions

View File

@ -336,7 +336,6 @@ func (s *TestServer) waitForLeader() {
if _, ok := taggedAddresses["lan"]; !ok {
return false, fmt.Errorf("No lan tagged addresses")
}
return true, nil
}, func(err error) {
defer s.Stop()

View File

@ -1,6 +1,7 @@
package testutil
import (
"fmt"
"testing"
"time"
@ -22,6 +23,7 @@ func WaitForResult(try testFn, fail errorFn) {
var success bool
success, err = try()
if success {
time.Sleep(25 * time.Millisecond)
return
}
@ -39,11 +41,20 @@ 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,
}
err := rpc("Catalog.ListNodes", args, &out)
return out.QueryMeta.KnownLeader && out.Index > 0, err
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)
})