forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqueue_pinger_test.go
264 lines (242 loc) · 6.1 KB
/
queue_pinger_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
context "context"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
"github.com/kedacore/http-add-on/pkg/k8s"
"github.com/kedacore/http-add-on/pkg/queue"
)
func TestCounts(t *testing.T) {
r := require.New(t)
ctx := context.Background()
const (
ns = "testns"
svcName = "testsvc"
deplName = "testdepl"
tickDur = 10 * time.Millisecond
numEndpoints = 3
)
// assemble an in-memory queue and start up a fake server that serves it.
// we'll configure the queue pinger to use that server below
counts := map[string]int{
"host1": 123,
"host2": 234,
"host3": 456,
"host4": 809,
}
q := queue.NewMemory(time.Second, false, logr.Logger{})
for host, count := range counts {
r.NoError(q.Resize(host, count))
}
srv, srvURL, endpoints, err := startFakeQueueEndpointServer(
svcName,
q,
3,
)
r.NoError(err)
defer srv.Close()
pinger, err := newQueuePinger(
ctx,
logr.Discard(),
func(context.Context, string, string) (*v1.Endpoints, error) {
return endpoints, nil
},
ns,
svcName,
deplName,
srvURL.Port(),
)
r.NoError(err)
// the pinger does an initial fetch, so ensure that
// the saved counts are correct
retCounts := pinger.counts()
r.Equal(len(counts), len(retCounts))
// now update the queue, start the ticker, and ensure
// that counts are updated after the first tick
r.NoError(q.Resize("host1", 1))
r.NoError(q.Resize("host2", 2))
r.NoError(q.Resize("host3", 3))
r.NoError(q.Resize("host4", 4))
ticker := time.NewTicker(tickDur)
fakeCache := k8s.NewFakeDeploymentCache()
go func() {
_ = pinger.start(ctx, ticker, fakeCache)
}()
// sleep to ensure we ticked and finished calling
// fetchAndSaveCounts
time.Sleep(tickDur * 2)
// now ensure that all the counts in the pinger
// are the same as in the queue, which has been updated
retCounts = pinger.counts()
expectedCounts, err := q.Current()
r.NoError(err)
r.Equal(len(expectedCounts.Counts), len(retCounts))
for host, count := range expectedCounts.Counts {
retCount, ok := retCounts[host]
r.True(
ok,
"returned count not found for host %s",
host,
)
// note that the returned value should be:
// (queue_count * num_endpoints)
r.Equal(count*3, retCount)
}
}
func TestFetchAndSaveCounts(t *testing.T) {
r := require.New(t)
ctx, done := context.WithCancel(context.Background())
defer done()
const (
ns = "testns"
svcName = "testsvc"
deplName = "testdepl"
adminPort = "8081"
numEndpoints = 3
)
counts := queue.NewCounts()
counts.Counts = map[string]int{
"host1": 123,
"host2": 234,
"host3": 345,
}
q := queue.NewMemory(time.Second, false, logr.Logger{})
for host, count := range counts.Counts {
r.NoError(q.Resize(host, count))
}
srv, srvURL, endpoints, err := startFakeQueueEndpointServer(
svcName, q, numEndpoints,
)
r.NoError(err)
defer srv.Close()
endpointsFn := func(
ctx context.Context,
ns,
svcName string,
) (*v1.Endpoints, error) {
return endpoints, nil
}
pinger, err := newQueuePinger(
ctx,
logr.Discard(),
endpointsFn,
ns,
svcName,
deplName,
srvURL.Port(),
// time.NewTicker(1*time.Millisecond),
)
r.NoError(err)
r.NoError(pinger.fetchAndSaveCounts(ctx))
// since all endpoints serve the same counts,
// expected aggregate is individual count * # endpoints
expectedAgg := counts.Aggregate() * numEndpoints
r.Equal(expectedAgg, pinger.aggregateCount)
// again, since all endpoints serve the same counts,
// the hosts will be the same as the original counts,
// but the value is (individual count * # endpoints)
expectedCounts := counts.Counts
for host, val := range expectedCounts {
expectedCounts[host] = val * numEndpoints
}
r.Equal(expectedCounts, pinger.allCounts)
}
func TestFetchCounts(t *testing.T) {
r := require.New(t)
ctx, done := context.WithCancel(context.Background())
defer done()
const (
ns = "testns"
svcName = "testsvc"
adminPort = "8081"
numEndpoints = 3
)
counts := queue.NewCounts()
counts.Counts = map[string]int{
"host1": 123,
"host2": 234,
"host3": 345,
}
q := queue.NewMemory(time.Second, false, logr.Logger{})
for host, count := range counts.Counts {
r.NoError(q.Resize(host, count))
}
srv, srvURL, endpoints, err := startFakeQueueEndpointServer(
svcName, q, numEndpoints,
)
r.NoError(err)
defer srv.Close()
endpointsFn := func(
context.Context,
string,
string,
) (*v1.Endpoints, error) {
return endpoints, nil
}
cts, agg, err := fetchCounts(
ctx,
logr.Discard(),
endpointsFn,
ns,
svcName,
srvURL.Port(),
)
r.NoError(err)
// since all endpoints serve the same counts,
// expected aggregate is individual count * # endpoints
expectedAgg := counts.Aggregate() * numEndpoints
r.Equal(expectedAgg, agg)
// again, since all endpoints serve the same counts,
// the hosts will be the same as the original counts,
// but the value is (individual count * # endpoints)
expectedCounts := counts.Counts
for host, val := range expectedCounts {
expectedCounts[host] = val * numEndpoints
}
r.Equal(expectedCounts, cts)
}
func TestMergeCountsWithRoutingTable(t *testing.T) {
r := require.New(t)
for _, tc := range cases(r) {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
grp, ctx := errgroup.WithContext(ctx)
r := require.New(t)
const C = 100
tickr, q, err := newFakeQueuePinger(
ctx,
logr.Discard(),
)
r.NoError(err)
defer tickr.Stop()
q.allCounts = tc.counts
retCh := make(chan map[string]int)
for i := 0; i < C; i++ {
grp.Go(func() error {
retCh <- q.mergeCountsWithRoutingTable(tc.table)
return nil
})
}
// ensure we receive from retCh C times
allRets := map[int]map[string]int{}
for i := 0; i < C; i++ {
allRets[i] = <-retCh
}
r.NoError(grp.Wait())
// ensure that all returned maps are the
// same
prev := allRets[0]
for i := 1; i < C; i++ {
r.Equal(prev, allRets[i])
prev = allRets[i]
}
// ensure that all the returned maps are
// equal to what we expected
r.Equal(tc.retCounts, prev)
})
}
}