Skip to content

Commit

Permalink
Merge pull request #838 from blight19/master
Browse files Browse the repository at this point in the history
update test
  • Loading branch information
smallnest authored Jan 11, 2024
2 parents 438414a + 1b001d3 commit 7ef7294
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions client/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,58 @@ func Test_consistentHashSelector_UpdateServer(t *testing.T) {
}

func TestWeightedRoundRobinSelector_Select(t *testing.T) {
// a b a c a b a a b a c a b a
sers := []string{"ServerA", "ServerB", "ServerA", "ServerC", "ServerA", "ServerB", "ServerA",
"ServerA", "ServerB", "ServerA", "ServerC", "ServerA", "ServerB", "ServerA"}
calc := make(map[string]int)
servers := make(map[string]string)
servers["ServerA"] = "weight=4"
servers["ServerB"] = "weight=2"
servers["ServerC"] = "weight=1"
weightSelector := newWeightedRoundRobinSelector(servers).(*weightedRoundRobinSelector)
ctx := context.Background()
for i := 0; i < 7; i++ {
s := weightSelector.Select(ctx, "", "", nil)
if _, ok := calc[s]; ok {
calc[s]++
} else {
calc[s] = 1
}
}
if calc["ServerA"] != 4 {
t.Errorf("expected %d but got %d", 4, calc["ServerA"])
}
if calc["ServerB"] != 2 {
t.Errorf("expected %d but got %d", 2, calc["ServerB"])
}
if calc["ServerC"] != 1 {
t.Errorf("expected %d but got %d", 1, calc["ServerC"])
}
}
func TestWeightedRoundRobinSelector_UpdateServer(t *testing.T) {
calc := make(map[string]int)
servers := make(map[string]string)
servers["ServerA"] = "weight=4"
servers["ServerB"] = "weight=2"
servers["ServerC"] = "weight=1"
weightSelector := newWeightedRoundRobinSelector(servers).(*weightedRoundRobinSelector)

for i := 0; i < 14; i++ {
ctx := context.Background()
servers["ServerA"] = "weight=5"
weightSelector.UpdateServer(servers)
for i := 0; i < 8; i++ {
s := weightSelector.Select(ctx, "", "", nil)
if s != sers[i] {
t.Errorf("expected %s but got %s", sers[i], s)
if _, ok := calc[s]; ok {
calc[s]++
} else {
calc[s] = 1
}
}
if calc["ServerA"] != 5 {
t.Errorf("expected %d but got %d", 4, calc["ServerA"])
}
if calc["ServerB"] != 2 {
t.Errorf("expected %d but got %d", 2, calc["ServerB"])
}
if calc["ServerC"] != 1 {
t.Errorf("expected %d but got %d", 1, calc["ServerC"])
}
}

func BenchmarkWeightedRoundRobinSelector_Select(b *testing.B) {
Expand Down

0 comments on commit 7ef7294

Please sign in to comment.