|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestServerList(t *testing.T) { |
| 11 | + s := newServerList() |
| 12 | + |
| 13 | + // New lists should be empty |
| 14 | + if e := s.all(); len(e) != 0 { |
| 15 | + t.Fatalf("expected empty list to return an empty list, but received: %+q", e) |
| 16 | + } |
| 17 | + |
| 18 | + mklist := func() endpoints { |
| 19 | + return endpoints{ |
| 20 | + &endpoint{"b", nil, 1}, |
| 21 | + &endpoint{"c", nil, 1}, |
| 22 | + &endpoint{"g", nil, 2}, |
| 23 | + &endpoint{"d", nil, 1}, |
| 24 | + &endpoint{"e", nil, 1}, |
| 25 | + &endpoint{"f", nil, 1}, |
| 26 | + &endpoint{"h", nil, 2}, |
| 27 | + &endpoint{"a", nil, 0}, |
| 28 | + } |
| 29 | + } |
| 30 | + s.set(mklist()) |
| 31 | + |
| 32 | + orig := mklist() |
| 33 | + all := s.all() |
| 34 | + if len(all) != len(orig) { |
| 35 | + t.Fatalf("expected %d endpoints but only have %d", len(orig), len(all)) |
| 36 | + } |
| 37 | + |
| 38 | + // Assert list is properly randomized+sorted |
| 39 | + for i, pri := range []int{0, 1, 1, 1, 1, 1, 2, 2} { |
| 40 | + if all[i].priority != pri { |
| 41 | + t.Errorf("expected endpoint %d (%+q) to be priority %d", i, all[i], pri) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Subsequent sets should reshuffle (try multiple times as they may |
| 46 | + // shuffle in the same order) |
| 47 | + tries := 0 |
| 48 | + max := 3 |
| 49 | + for ; tries < max; tries++ { |
| 50 | + if s.all().String() == s.all().String() { |
| 51 | + // eek, matched; try again in case we just got unlucky |
| 52 | + continue |
| 53 | + } |
| 54 | + break |
| 55 | + } |
| 56 | + if tries == max { |
| 57 | + t.Fatalf("after %d attempts servers were still not random reshuffled", tries) |
| 58 | + } |
| 59 | + |
| 60 | + // Mark an endpoint as failed enough that it should be at the end of the list |
| 61 | + sa := &endpoint{"a", nil, 0} |
| 62 | + s.failed(sa) |
| 63 | + s.failed(sa) |
| 64 | + s.failed(sa) |
| 65 | + all2 := s.all() |
| 66 | + if len(all2) != len(orig) { |
| 67 | + t.Fatalf("marking should not have changed list length") |
| 68 | + } |
| 69 | + if all2[len(all)-1].name != sa.name { |
| 70 | + t.Fatalf("failed endpoint should be at end of list: %+q", all2) |
| 71 | + } |
| 72 | + |
| 73 | + // But if the bad endpoint succeeds even once it should be bumped to the top group |
| 74 | + s.good(sa) |
| 75 | + found := false |
| 76 | + for _, e := range s.all() { |
| 77 | + if e.name == sa.name { |
| 78 | + if e.priority != 0 { |
| 79 | + t.Fatalf("server newly marked good should have highest priority") |
| 80 | + } |
| 81 | + found = true |
| 82 | + } |
| 83 | + } |
| 84 | + if !found { |
| 85 | + t.Fatalf("what happened to endpoint A?!") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestClient_ServerList tests client methods that interact with the internal |
| 90 | +// nomad server list. |
| 91 | +func TestClient_ServerList(t *testing.T) { |
| 92 | + // manually create a mostly empty client to avoid spinning up a ton of |
| 93 | + // goroutines that complicate testing |
| 94 | + client := Client{servers: newServerList(), logger: log.New(os.Stderr, "", log.Ltime|log.Lshortfile)} |
| 95 | + |
| 96 | + if s := client.GetServers(); len(s) != 0 { |
| 97 | + t.Fatalf("expected server lit to be empty but found: %+q", s) |
| 98 | + } |
| 99 | + if err := client.SetServers(nil); err != noServers { |
| 100 | + t.Fatalf("expected setting an empty list to return a 'no servers' error but received %v", err) |
| 101 | + } |
| 102 | + if err := client.SetServers([]string{"not-a-real-domain.fake"}); err == nil { |
| 103 | + t.Fatalf("expected setting a bad server to return an error") |
| 104 | + } |
| 105 | + if err := client.SetServers([]string{"bad.fake", "127.0.0.1:1234", "127.0.0.1"}); err != nil { |
| 106 | + t.Fatalf("expected setting at least one good server to succeed but received: %v", err) |
| 107 | + } |
| 108 | + s := client.GetServers() |
| 109 | + if len(s) != 2 { |
| 110 | + t.Fatalf("expected 2 servers but received: %+q", s) |
| 111 | + } |
| 112 | + for _, host := range s { |
| 113 | + if !strings.HasPrefix(host, "127.0.0.1:") { |
| 114 | + t.Errorf("expected both servers to be localhost and include port but found: %s", host) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments