Add retries to StatsFetcherTest (#5892)

This commit is contained in:
Freddy 2019-05-23 19:51:31 -04:00 committed by GitHub
parent bb28ebda7b
commit c9e6640337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 34 deletions

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/hashicorp/consul/agent/metadata" "github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/consul/types" "github.com/hashicorp/consul/types"
) )
@ -47,51 +48,55 @@ func TestStatsFetcher(t *testing.T) {
// Do a normal fetch and make sure we get three responses. // Do a normal fetch and make sure we get three responses.
func() { func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) retry.Run(t, func(r *retry.R) {
defer cancel() ctx, cancel := context.WithTimeout(context.Background(), time.Second)
stats := s1.statsFetcher.Fetch(ctx, s1.LANMembers()) defer cancel()
if len(stats) != 3 { stats := s1.statsFetcher.Fetch(ctx, s1.LANMembers())
t.Fatalf("bad: %#v", stats) if len(stats) != 3 {
} t.Fatalf("bad: %#v", stats)
for id, stat := range stats {
switch types.NodeID(id) {
case s1.config.NodeID, s2.config.NodeID, s3.config.NodeID:
// OK
default:
t.Fatalf("bad: %s", id)
} }
for id, stat := range stats {
switch types.NodeID(id) {
case s1.config.NodeID, s2.config.NodeID, s3.config.NodeID:
// OK
default:
t.Fatalf("bad: %s", id)
}
if stat == nil || stat.LastTerm == 0 { if stat == nil || stat.LastTerm == 0 {
t.Fatalf("bad: %#v", stat) t.Fatalf("bad: %#v", stat)
}
} }
} })
}() }()
// Fake an in-flight request to server 3 and make sure we don't fetch // Fake an in-flight request to server 3 and make sure we don't fetch
// from it. // from it.
func() { func() {
s1.statsFetcher.inflight[string(s3.config.NodeID)] = struct{}{} retry.Run(t, func(r *retry.R) {
defer delete(s1.statsFetcher.inflight, string(s3.config.NodeID)) s1.statsFetcher.inflight[string(s3.config.NodeID)] = struct{}{}
defer delete(s1.statsFetcher.inflight, string(s3.config.NodeID))
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
stats := s1.statsFetcher.Fetch(ctx, s1.LANMembers()) stats := s1.statsFetcher.Fetch(ctx, s1.LANMembers())
if len(stats) != 2 { if len(stats) != 2 {
t.Fatalf("bad: %#v", stats) t.Fatalf("bad: %#v", stats)
}
for id, stat := range stats {
switch types.NodeID(id) {
case s1.config.NodeID, s2.config.NodeID:
// OK
case s3.config.NodeID:
t.Fatalf("bad")
default:
t.Fatalf("bad: %s", id)
} }
for id, stat := range stats {
switch types.NodeID(id) {
case s1.config.NodeID, s2.config.NodeID:
// OK
case s3.config.NodeID:
t.Fatalf("bad")
default:
t.Fatalf("bad: %s", id)
}
if stat == nil || stat.LastTerm == 0 { if stat == nil || stat.LastTerm == 0 {
t.Fatalf("bad: %#v", stat) t.Fatalf("bad: %#v", stat)
}
} }
} })
}() }()
} }