Skip to content

Commit

Permalink
More stable Unit Test / fixes according to @freddygv suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
pierresouchay committed Aug 23, 2018
1 parent baf8ac4 commit e069418
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 5 additions & 8 deletions agent/consul/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1514,20 +1514,17 @@ func TestCatalog_ListServices_Stale(t *testing.T) {
t.Fatalf("bad: %#v", out.Services)
}

if out.Services["consul"] == nil {
t.Fatalf("expected consul service.ID, had: %#v", out)
}

if !out.KnownLeader {
t.Fatalf("should have a leader: %v", out)
}

s1.Leave()
s1.Shutdown()
os.RemoveAll(dir1)

testrpc.WaitUntilNoLeader(t, s2.RPC, "dc1")

args.AllowStale = false
// Run the query, do not wait for leader, never any contact with leader, should fail
// Since the leader is now down, non-stale query should fail now
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err == nil || err.Error() != structs.ErrNoLeader.Error() {
t.Fatalf("expected %v but got err: %v and %v", structs.ErrNoLeader, err, out)
}
Expand All @@ -1543,8 +1540,8 @@ func TestCatalog_ListServices_Stale(t *testing.T) {
t.Fatalf("bad: %#v", out)
}

if out.Services["consul"] == nil {
t.Fatalf("expected consul service.ID, had: %#v", out)
if out.KnownLeader {
t.Fatalf("should not have a leader anymore: %#v", out)
}
}

Expand Down
14 changes: 14 additions & 0 deletions testrpc/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func WaitForLeader(t *testing.T, rpc rpcFn, dc string) {
})
}

// WaitUntilNoLeader ensures no leader is present, useful for testing lost leadership.
func WaitUntilNoLeader(t *testing.T, rpc rpcFn, dc string) {
var out structs.IndexedNodes
retry.Run(t, func(r *retry.R) {
args := &structs.DCSpecificRequest{Datacenter: dc}
if err := rpc("Catalog.ListNodes", args, &out); err == nil {
r.Fatalf("It still has a leader: %#q", out)
}
if out.QueryMeta.KnownLeader {
r.Fatalf("Has still a leader")
}
})
}

// WaitForTestAgent ensures we have a node with serfHealth check registered
func WaitForTestAgent(t *testing.T, rpc rpcFn, dc string) {
var nodes structs.IndexedNodes
Expand Down

0 comments on commit e069418

Please sign in to comment.