-
Notifications
You must be signed in to change notification settings - Fork 1
/
round_robin_test.go
137 lines (128 loc) · 2.58 KB
/
round_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Package roundrobin
package roundrobin
import (
"fmt"
"reflect"
"testing"
)
type resource struct {
id int
name string
}
func TestRoundRobin(t *testing.T) {
tests := []struct {
resources []*resource
iserr bool
expected []string
want []*resource
}{
{
resources: []*resource{
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
{4, "resource-4"},
{5, "resource-5"},
{6, "resource-6"},
{7, "resource-7"},
},
iserr: false,
want: []*resource{
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
{4, "resource-4"},
{5, "resource-5"},
{6, "resource-6"},
{7, "resource-7"},
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
{4, "resource-4"},
{5, "resource-5"},
{6, "resource-6"},
{7, "resource-7"},
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
{4, "resource-4"},
{5, "resource-5"},
{6, "resource-6"},
{7, "resource-7"},
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
},
},
{
resources: []*resource{},
iserr: true,
want: []*resource{},
},
}
for i, test := range tests {
rr, err := New(test.resources...)
if err != nil && !test.iserr {
t.Errorf("tests[%d] - RoundRobin iserr is wrong. want: %v, but got: %v", i, test.want, true)
}
for j := 0; j < len(test.want); j++ {
got := rr.Next()
if !reflect.DeepEqual(got, test.want[j]) {
t.Errorf("tests[%d] index[%d] - RoundRobin is wrong. want: %v, got: %v", i, j, *test.want[j], *got)
}
}
}
}
func BenchmarkRoundRobinSync(b *testing.B) {
resources := []*struct {
id int
name string
}{
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
{4, "resource-4"},
{5, "resource-5"},
{6, "resource-6"},
{7, "resource-7"},
}
for i := 1; i < len(resources)+1; i++ {
b.Run(fmt.Sprintf("RoundRobinSize(%d)", i), func(b *testing.B) {
rr, err := New(resources[:i]...)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rr.Next()
}
})
}
}
func BenchmarkRoundRobinASync(b *testing.B) {
resources := []*struct {
id int
name string
}{
{1, "resource-1"},
{2, "resource-2"},
{3, "resource-3"},
{4, "resource-4"},
{5, "resource-5"},
{6, "resource-6"},
{7, "resource-7"},
}
for i := 1; i < len(resources)+1; i++ {
b.Run(fmt.Sprintf("RoundRobinSize(%d)", i), func(b *testing.B) {
rr, err := New(resources[:i]...)
if err != nil {
b.Fatal(err)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rr.Next()
}
})
})
}
}