generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
robin_test.go
80 lines (61 loc) · 1.31 KB
/
robin_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package robin
import (
"sync"
"testing"
)
func TestLoadbalancer_Next(t *testing.T) {
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
for i := 0; i < 10; i++ {
if lb.Next() != set[i%len(set)] {
t.Errorf("expected %d, got %d", set[i%len(set)], lb.Next())
}
}
}
func TestLoadbalancer_Next_ThreadSafe(t *testing.T) {
var set []int
for i := 0; i < 2000; i++ {
set = append(set, i)
}
lb := NewLoadbalancer(set)
var wg sync.WaitGroup
for i := 0; i < 1337; i++ {
wg.Add(1)
go func() {
lb.Next()
wg.Done()
}()
}
wg.Wait()
if lb.Next() != 1337 {
t.Errorf("expected %d, got %d", 1337, lb.Next())
}
}
func TestLoadbalancer_AddItems(t *testing.T) {
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
lb.AddItems(4, 5, 6)
if lb.Items[5] != 6 {
t.Errorf("expected %d, got %d", 6, lb.Items[5])
}
}
func TestLoadbalancer_Reset(t *testing.T) {
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
for i := 0; i < 10; i++ {
if lb.Next() != set[i%len(set)] {
t.Errorf("expected %d, got %d", set[i%len(set)], lb.Next())
}
}
lb.Reset()
if lb.idx != 0 {
t.Errorf("expected %d, got %d", 0, lb.idx)
}
}
func TestLoadbalancer_Current(t *testing.T) {
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
if lb.Current() != set[0] {
t.Errorf("expected %d, got %d", set[0], lb.Current())
}
}