github.com/bigcommerce/nomad@v0.9.3-bc/nomad/stats_fetcher_test.go (about) 1 package nomad 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/nomad/testutil" 9 ) 10 11 func TestStatsFetcher(t *testing.T) { 12 t.Parallel() 13 14 conf := func(c *Config) { 15 c.Region = "region-a" 16 c.DevDisableBootstrap = true 17 c.BootstrapExpect = 3 18 } 19 20 s1 := TestServer(t, conf) 21 defer s1.Shutdown() 22 23 s2 := TestServer(t, conf) 24 defer s2.Shutdown() 25 26 s3 := TestServer(t, conf) 27 defer s3.Shutdown() 28 29 TestJoin(t, s1, s2, s3) 30 testutil.WaitForLeader(t, s1.RPC) 31 32 members := s1.serf.Members() 33 if len(members) != 3 { 34 t.Fatalf("bad len: %d", len(members)) 35 } 36 37 var servers []*serverParts 38 for _, member := range members { 39 ok, server := isNomadServer(member) 40 if !ok { 41 t.Fatalf("bad: %#v", member) 42 } 43 servers = append(servers, server) 44 } 45 46 // Do a normal fetch and make sure we get three responses. 47 func() { 48 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 49 defer cancel() 50 stats := s1.statsFetcher.Fetch(ctx, s1.Members()) 51 if len(stats) != 3 { 52 t.Fatalf("bad: %#v", stats) 53 } 54 for id, stat := range stats { 55 switch id { 56 case s1.config.NodeID, s2.config.NodeID, s3.config.NodeID: 57 // OK 58 default: 59 t.Fatalf("bad: %s", id) 60 } 61 62 if stat == nil || stat.LastTerm == 0 { 63 t.Fatalf("bad: %#v", stat) 64 } 65 } 66 }() 67 68 // Fake an in-flight request to server 3 and make sure we don't fetch 69 // from it. 70 func() { 71 s1.statsFetcher.inflight[string(s3.config.NodeID)] = struct{}{} 72 defer delete(s1.statsFetcher.inflight, string(s3.config.NodeID)) 73 74 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 75 defer cancel() 76 stats := s1.statsFetcher.Fetch(ctx, s1.Members()) 77 if len(stats) != 2 { 78 t.Fatalf("bad: %#v", stats) 79 } 80 for id, stat := range stats { 81 switch id { 82 case s1.config.NodeID, s2.config.NodeID: 83 // OK 84 case s3.config.NodeID: 85 t.Fatalf("bad") 86 default: 87 t.Fatalf("bad: %s", id) 88 } 89 90 if stat == nil || stat.LastTerm == 0 { 91 t.Fatalf("bad: %#v", stat) 92 } 93 } 94 }() 95 }