github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/client/serverlist_test.go (about)

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