forked from smallnest/weighted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmooth_weighted_test.go
49 lines (37 loc) · 943 Bytes
/
smooth_weighted_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package weighted
import "testing"
func TestSW_Next(t *testing.T) {
w := &SW{}
w.Add("server1", 5)
w.Add("server2", 2)
w.Add("server3", 3)
results := make(map[string]int)
for i := 0; i < 100; i++ {
s := w.Next().(string)
results[s]++
}
if results["server1"] != 50 || results["server2"] != 20 || results["server3"] != 30 {
t.Error("the algorithm is wrong")
}
w.Reset()
results = make(map[string]int)
for i := 0; i < 100; i++ {
s := w.Next().(string)
results[s]++
}
if results["server1"] != 50 || results["server2"] != 20 || results["server3"] != 30 {
t.Error("the algorithm is wrong")
}
w.RemoveAll()
w.Add("server1", 7)
w.Add("server2", 9)
w.Add("server3", 13)
results = make(map[string]int)
for i := 0; i < 29000; i++ {
s := w.Next().(string)
results[s]++
}
if results["server1"] != 7000 || results["server2"] != 9000 || results["server3"] != 13000 {
t.Error("the algorithm is wrong")
}
}