2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-07-06 10:40:54 +00:00
|
|
|
package router_test
|
2016-02-24 18:55:04 +00:00
|
|
|
|
|
|
|
import (
|
2016-02-25 06:23:50 +00:00
|
|
|
"fmt"
|
2016-03-28 21:53:29 +00:00
|
|
|
"math/rand"
|
2017-06-15 13:16:16 +00:00
|
|
|
"net"
|
2016-02-25 06:23:50 +00:00
|
|
|
"strings"
|
2016-02-24 18:55:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2017-07-06 10:40:54 +00:00
|
|
|
"github.com/hashicorp/consul/agent/router"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2016-02-24 18:55:04 +00:00
|
|
|
)
|
|
|
|
|
2017-11-08 02:13:23 +00:00
|
|
|
type fauxAddr struct {
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *fauxAddr) Network() string {
|
|
|
|
return "faux"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *fauxAddr) String() string {
|
|
|
|
return a.addr
|
|
|
|
}
|
|
|
|
|
2016-03-27 02:28:13 +00:00
|
|
|
type fauxConnPool struct {
|
2017-11-08 02:13:23 +00:00
|
|
|
// failPct between 0.0 and 1.0 == pct of time a Ping should fail.
|
2016-03-28 21:53:29 +00:00
|
|
|
failPct float64
|
2017-11-08 02:13:23 +00:00
|
|
|
|
|
|
|
// failAddr fail whenever we see this address.
|
|
|
|
failAddr net.Addr
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 07:48:34 +00:00
|
|
|
func (cp *fauxConnPool) Ping(dc string, nodeName string, addr net.Addr) (bool, error) {
|
2016-03-28 21:53:29 +00:00
|
|
|
var success bool
|
2017-11-08 02:13:23 +00:00
|
|
|
|
2016-03-28 21:53:29 +00:00
|
|
|
successProb := rand.Float64()
|
|
|
|
if successProb > cp.failPct {
|
|
|
|
success = true
|
|
|
|
}
|
2017-11-08 02:13:23 +00:00
|
|
|
|
|
|
|
if cp.failAddr != nil && addr.String() == cp.failAddr.String() {
|
|
|
|
success = false
|
|
|
|
}
|
|
|
|
|
2016-03-28 21:53:29 +00:00
|
|
|
return success, nil
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 06:08:13 +00:00
|
|
|
type fauxSerf struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *fauxSerf) NumNodes() int {
|
|
|
|
return 16384
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func testManager(t testing.TB) (m *router.Manager) {
|
|
|
|
logger := testutil.Logger(t)
|
2016-02-25 06:08:13 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2020-09-14 20:16:44 +00:00
|
|
|
m = router.New(logger, shutdownCh, &fauxSerf{}, &fauxConnPool{}, "", noopRebalancer)
|
2016-03-29 22:58:15 +00:00
|
|
|
return m
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:16:44 +00:00
|
|
|
func noopRebalancer() {}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func testManagerFailProb(t testing.TB, failPct float64) (m *router.Manager) {
|
|
|
|
logger := testutil.Logger(t)
|
2016-03-28 21:53:29 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2020-09-14 20:16:44 +00:00
|
|
|
m = router.New(logger, shutdownCh, &fauxSerf{}, &fauxConnPool{failPct: failPct}, "", noopRebalancer)
|
2016-03-29 22:58:15 +00:00
|
|
|
return m
|
2016-03-28 21:53:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func testManagerFailAddr(t testing.TB, failAddr net.Addr) (m *router.Manager) {
|
|
|
|
logger := testutil.Logger(t)
|
2017-11-08 02:13:23 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2020-09-14 20:16:44 +00:00
|
|
|
m = router.New(logger, shutdownCh, &fauxSerf{}, &fauxConnPool{failAddr: failAddr}, "", noopRebalancer)
|
2017-11-08 02:13:23 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
// func (m *Manager) AddServer(server *metadata.Server) {
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestServers_AddServer(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManager(t)
|
2016-02-25 06:23:50 +00:00
|
|
|
var num int
|
2016-03-29 22:58:15 +00:00
|
|
|
num = m.NumServers()
|
2016-02-25 06:23:50 +00:00
|
|
|
if num != 0 {
|
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
s1 := &metadata.Server{Name: "s1"}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(s1)
|
|
|
|
num = m.NumServers()
|
2016-02-25 06:23:50 +00:00
|
|
|
if num != 1 {
|
|
|
|
t.Fatalf("Expected one server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(s1)
|
|
|
|
num = m.NumServers()
|
2016-02-25 06:23:50 +00:00
|
|
|
if num != 1 {
|
|
|
|
t.Fatalf("Expected one server (still)")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
s2 := &metadata.Server{Name: "s2"}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(s2)
|
|
|
|
num = m.NumServers()
|
2016-02-25 06:23:50 +00:00
|
|
|
if num != 2 {
|
|
|
|
t.Fatalf("Expected two servers")
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2017-03-15 00:47:37 +00:00
|
|
|
// func (m *Manager) IsOffline() bool {
|
|
|
|
func TestServers_IsOffline(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManager(t)
|
2017-03-15 00:47:37 +00:00
|
|
|
if !m.IsOffline() {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
s1 := &metadata.Server{Name: "s1"}
|
2017-03-15 00:47:37 +00:00
|
|
|
m.AddServer(s1)
|
|
|
|
if m.IsOffline() {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
m.RebalanceServers()
|
|
|
|
if m.IsOffline() {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
m.RemoveServer(s1)
|
|
|
|
m.RebalanceServers()
|
|
|
|
if !m.IsOffline() {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
const failPct = 0.5
|
2020-01-28 23:50:41 +00:00
|
|
|
m = testManagerFailProb(t, failPct)
|
2017-03-15 00:47:37 +00:00
|
|
|
m.AddServer(s1)
|
|
|
|
var on, off int
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
m.RebalanceServers()
|
|
|
|
if m.IsOffline() {
|
|
|
|
off++
|
|
|
|
} else {
|
|
|
|
on++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if on == 0 || off == 0 {
|
|
|
|
t.Fatalf("bad: %d %d", on, off)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
// func (m *Manager) FindServer() (server *metadata.Server) {
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestServers_FindServer(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManager(t)
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.FindServer() != nil {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Expected nil return")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
m.AddServer(&metadata.Server{Name: "s1"})
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != 1 {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Expected one server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
s1 := m.FindServer()
|
2016-02-25 06:05:05 +00:00
|
|
|
if s1 == nil {
|
|
|
|
t.Fatalf("Expected non-nil server")
|
|
|
|
}
|
|
|
|
if s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
s1 = m.FindServer()
|
2016-02-25 06:05:05 +00:00
|
|
|
if s1 == nil || s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server (still)")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
m.AddServer(&metadata.Server{Name: "s2"})
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != 2 {
|
2016-02-25 06:05:05 +00:00
|
|
|
t.Fatalf("Expected two servers")
|
|
|
|
}
|
2016-03-29 22:58:15 +00:00
|
|
|
s1 = m.FindServer()
|
2016-02-25 06:05:05 +00:00
|
|
|
if s1 == nil || s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server (still)")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.NotifyFailedServer(s1)
|
|
|
|
s2 := m.FindServer()
|
2016-02-25 06:05:05 +00:00
|
|
|
if s2 == nil || s2.Name != "s2" {
|
|
|
|
t.Fatalf("Expected s2 server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.NotifyFailedServer(s2)
|
|
|
|
s1 = m.FindServer()
|
2016-02-25 06:05:05 +00:00
|
|
|
if s1 == nil || s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server")
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestServers_New(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := testutil.Logger(t)
|
2016-02-25 06:23:50 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2020-09-14 20:16:44 +00:00
|
|
|
m := router.New(logger, shutdownCh, &fauxSerf{}, &fauxConnPool{}, "", noopRebalancer)
|
2016-03-29 22:58:15 +00:00
|
|
|
if m == nil {
|
|
|
|
t.Fatalf("Manager nil")
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
// func (m *Manager) NotifyFailedServer(server *metadata.Server) {
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestServers_NotifyFailedServer(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManager(t)
|
2016-02-25 06:23:50 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != 0 {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
s1 := &metadata.Server{Name: "s1"}
|
|
|
|
s2 := &metadata.Server{Name: "s2"}
|
2016-03-25 21:40:46 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// Try notifying for a server that is not managed by Manager
|
|
|
|
m.NotifyFailedServer(s1)
|
|
|
|
if m.NumServers() != 0 {
|
2016-03-25 21:40:46 +00:00
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(s1)
|
2016-03-25 21:40:46 +00:00
|
|
|
|
|
|
|
// Test again w/ a server not in the list
|
2016-03-29 22:58:15 +00:00
|
|
|
m.NotifyFailedServer(s2)
|
|
|
|
if m.NumServers() != 1 {
|
2016-03-25 21:40:46 +00:00
|
|
|
t.Fatalf("Expected one server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(s2)
|
|
|
|
if m.NumServers() != 2 {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Expected two servers")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
s1 = m.FindServer()
|
2016-02-25 06:23:50 +00:00
|
|
|
if s1 == nil || s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.NotifyFailedServer(s2)
|
|
|
|
s1 = m.FindServer()
|
2016-02-25 06:23:50 +00:00
|
|
|
if s1 == nil || s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server (still)")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.NotifyFailedServer(s1)
|
|
|
|
s2 = m.FindServer()
|
2016-02-25 06:23:50 +00:00
|
|
|
if s2 == nil || s2.Name != "s2" {
|
|
|
|
t.Fatalf("Expected s2 server")
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.NotifyFailedServer(s2)
|
|
|
|
s1 = m.FindServer()
|
2016-02-25 06:23:50 +00:00
|
|
|
if s1 == nil || s1.Name != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// func (m *Manager) NumServers() (numServers int) {
|
|
|
|
func TestServers_NumServers(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManager(t)
|
2016-02-24 18:55:04 +00:00
|
|
|
var num int
|
2016-03-29 22:58:15 +00:00
|
|
|
num = m.NumServers()
|
2016-02-24 18:55:04 +00:00
|
|
|
if num != 0 {
|
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
s := &metadata.Server{}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(s)
|
|
|
|
num = m.NumServers()
|
2016-02-24 18:55:04 +00:00
|
|
|
if num != 1 {
|
|
|
|
t.Fatalf("Expected one server after AddServer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// func (m *Manager) RebalanceServers() {
|
|
|
|
func TestServers_RebalanceServers(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2016-03-28 21:53:29 +00:00
|
|
|
const failPct = 0.5
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManagerFailProb(t, failPct)
|
2016-02-25 06:23:50 +00:00
|
|
|
const maxServers = 100
|
|
|
|
const numShuffleTests = 100
|
|
|
|
const uniquePassRate = 0.5
|
|
|
|
|
|
|
|
// Make a huge list of nodes.
|
|
|
|
for i := 0; i < maxServers; i++ {
|
|
|
|
nodeName := fmt.Sprintf("s%02d", i)
|
2017-07-06 10:48:37 +00:00
|
|
|
m.AddServer(&metadata.Server{Name: nodeName})
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 06:23:50 +00:00
|
|
|
// Keep track of how many unique shuffles we get.
|
|
|
|
uniques := make(map[string]struct{}, maxServers)
|
|
|
|
for i := 0; i < numShuffleTests; i++ {
|
2016-03-29 22:58:15 +00:00
|
|
|
m.RebalanceServers()
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-02-25 06:23:50 +00:00
|
|
|
var names []string
|
|
|
|
for j := 0; j < maxServers; j++ {
|
2016-03-29 22:58:15 +00:00
|
|
|
server := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server)
|
2016-02-25 06:23:50 +00:00
|
|
|
names = append(names, server.Name)
|
|
|
|
}
|
|
|
|
key := strings.Join(names, "|")
|
|
|
|
uniques[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to allow for the fact that there won't always be a unique
|
|
|
|
// shuffle each pass, so we just look for smell here without the test
|
|
|
|
// being flaky.
|
|
|
|
if len(uniques) < int(maxServers*uniquePassRate) {
|
|
|
|
t.Fatalf("unique shuffle ratio too low: %d/%d", len(uniques), maxServers)
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2017-11-08 02:13:23 +00:00
|
|
|
func TestServers_RebalanceServers_AvoidFailed(t *testing.T) {
|
|
|
|
// Do a large number of rebalances with one failed server in the
|
|
|
|
// list and make sure we never have that one selected afterwards.
|
|
|
|
// This was added when fixing #3463, when we were just doing the
|
|
|
|
// shuffle and not actually cycling servers. We do a large number
|
|
|
|
// of trials with a small number of servers to try to make sure
|
|
|
|
// the shuffle alone won't give the right answer.
|
|
|
|
servers := []*metadata.Server{
|
2020-06-16 17:19:31 +00:00
|
|
|
{Name: "s1", Addr: &fauxAddr{"s1"}},
|
|
|
|
{Name: "s2", Addr: &fauxAddr{"s2"}},
|
|
|
|
{Name: "s3", Addr: &fauxAddr{"s3"}},
|
2017-11-08 02:13:23 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManagerFailAddr(t, &fauxAddr{"s2"})
|
2017-11-08 02:13:23 +00:00
|
|
|
for _, s := range servers {
|
|
|
|
m.AddServer(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.RebalanceServers()
|
|
|
|
if front := m.FindServer().Name; front == "s2" {
|
|
|
|
t.Fatalf("should have avoided the failed server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
// func (m *Manager) RemoveServer(server *metadata.Server) {
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestManager_RemoveServer(t *testing.T) {
|
2016-03-25 21:40:46 +00:00
|
|
|
const nodeNameFmt = "s%02d"
|
2020-01-28 23:50:41 +00:00
|
|
|
m := testManager(t)
|
2016-02-25 06:23:50 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != 0 {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:40:46 +00:00
|
|
|
// Test removing server before its added
|
|
|
|
nodeName := fmt.Sprintf(nodeNameFmt, 1)
|
2017-07-06 10:48:37 +00:00
|
|
|
s1 := &metadata.Server{Name: nodeName}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.RemoveServer(s1)
|
|
|
|
m.AddServer(s1)
|
2016-03-25 21:40:46 +00:00
|
|
|
|
|
|
|
nodeName = fmt.Sprintf(nodeNameFmt, 2)
|
2017-07-06 10:48:37 +00:00
|
|
|
s2 := &metadata.Server{Name: nodeName}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.RemoveServer(s2)
|
|
|
|
m.AddServer(s2)
|
2016-03-25 21:40:46 +00:00
|
|
|
|
2016-02-25 06:23:50 +00:00
|
|
|
const maxServers = 19
|
2016-03-25 21:40:46 +00:00
|
|
|
// Already added two servers above
|
|
|
|
for i := maxServers; i > 2; i-- {
|
|
|
|
nodeName := fmt.Sprintf(nodeNameFmt, i)
|
2017-07-06 10:48:37 +00:00
|
|
|
server := &metadata.Server{Name: nodeName}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(server)
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != maxServers {
|
|
|
|
t.Fatalf("Expected %d servers, received %d", maxServers, m.NumServers())
|
2016-03-28 18:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.RebalanceServers()
|
2016-02-25 06:23:50 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != maxServers {
|
|
|
|
t.Fatalf("Expected %d servers, received %d", maxServers, m.NumServers())
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
findServer := func(server *metadata.Server) bool {
|
2016-03-29 22:58:15 +00:00
|
|
|
for i := m.NumServers(); i > 0; i-- {
|
|
|
|
s := m.FindServer()
|
2016-02-25 06:23:50 +00:00
|
|
|
if s == server {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedNumServers := maxServers
|
2017-07-06 10:48:37 +00:00
|
|
|
removedServers := make([]*metadata.Server, 0, maxServers)
|
2016-02-25 06:23:50 +00:00
|
|
|
|
|
|
|
// Remove servers from the front of the list
|
|
|
|
for i := 3; i > 0; i-- {
|
2016-03-29 22:58:15 +00:00
|
|
|
server := m.FindServer()
|
2016-02-25 06:23:50 +00:00
|
|
|
if server == nil {
|
|
|
|
t.Fatalf("FindServer returned nil")
|
|
|
|
}
|
2016-03-29 22:58:15 +00:00
|
|
|
m.RemoveServer(server)
|
2016-02-25 06:23:50 +00:00
|
|
|
expectedNumServers--
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != expectedNumServers {
|
|
|
|
t.Fatalf("Expected %d servers (got %d)", expectedNumServers, m.NumServers())
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
2016-03-25 21:10:09 +00:00
|
|
|
if findServer(server) == true {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Did not expect to find server %s after removal from the front", server.Name)
|
|
|
|
}
|
|
|
|
removedServers = append(removedServers, server)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove server from the end of the list
|
|
|
|
for i := 3; i > 0; i-- {
|
2016-03-29 22:58:15 +00:00
|
|
|
server := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server)
|
|
|
|
m.RemoveServer(server)
|
2016-02-25 06:23:50 +00:00
|
|
|
expectedNumServers--
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != expectedNumServers {
|
|
|
|
t.Fatalf("Expected %d servers (got %d)", expectedNumServers, m.NumServers())
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
2016-03-25 21:10:09 +00:00
|
|
|
if findServer(server) == true {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Did not expect to find server %s", server.Name)
|
|
|
|
}
|
|
|
|
removedServers = append(removedServers, server)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove server from the middle of the list
|
|
|
|
for i := 3; i > 0; i-- {
|
2016-03-29 22:58:15 +00:00
|
|
|
server := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server)
|
|
|
|
server2 := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server2) // server2 now at end of the list
|
2016-02-25 06:23:50 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
m.RemoveServer(server)
|
2016-02-25 06:23:50 +00:00
|
|
|
expectedNumServers--
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != expectedNumServers {
|
|
|
|
t.Fatalf("Expected %d servers (got %d)", expectedNumServers, m.NumServers())
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
2016-03-25 21:10:09 +00:00
|
|
|
if findServer(server) == true {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Did not expect to find server %s", server.Name)
|
|
|
|
}
|
|
|
|
removedServers = append(removedServers, server)
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers()+len(removedServers) != maxServers {
|
|
|
|
t.Fatalf("Expected %d+%d=%d servers", m.NumServers(), len(removedServers), maxServers)
|
2016-02-25 06:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drain the remaining servers from the middle
|
2016-03-29 22:58:15 +00:00
|
|
|
for i := m.NumServers(); i > 0; i-- {
|
|
|
|
server := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server)
|
|
|
|
server2 := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server2) // server2 now at end of the list
|
|
|
|
m.RemoveServer(server)
|
2016-02-25 06:23:50 +00:00
|
|
|
removedServers = append(removedServers, server)
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.NumServers() != 0 {
|
2016-02-25 06:23:50 +00:00
|
|
|
t.Fatalf("Expected an empty server list")
|
|
|
|
}
|
|
|
|
if len(removedServers) != maxServers {
|
|
|
|
t.Fatalf("Expected all servers to be in removed server list")
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// func (m *Manager) Start() {
|