generated from kedacore/github-template
-
Notifications
You must be signed in to change notification settings - Fork 98
/
queue_pinger_test.go
311 lines (288 loc) · 6.87 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"github.com/kedacore/http-add-on/pkg/k8s"
kedanet "github.com/kedacore/http-add-on/pkg/net"
"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]queue.Count{
"host1": {
Concurrency: 123,
RPS: 123,
},
"host2": {
Concurrency: 234,
RPS: 234,
},
"host3": {
Concurrency: 345,
RPS: 345,
},
"host4": {
Concurrency: 456,
RPS: 456,
},
}
q := queue.NewMemory()
for host, count := range counts {
q.EnsureKey(host, time.Minute, time.Second)
r.NoError(q.Increase(host, count.Concurrency))
}
srv, srvURL, endpoints, err := startFakeQueueEndpointServer(
svcName,
q,
3,
)
r.NoError(err)
defer srv.Close()
pinger := newQueuePinger(
logr.Discard(),
func(context.Context, string, string) (*v1.Endpoints, error) {
return endpoints, nil
},
ns,
svcName,
deplName,
srvURL.Port(),
)
ticker := time.NewTicker(tickDur)
fakeCache := k8s.NewFakeEndpointsCache()
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.Concurrency*3, retCount.Concurrency)
r.Equal(count.RPS*3, retCount.RPS)
}
}
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]queue.Count{
"host1": {
Concurrency: 123,
RPS: 123,
},
"host2": {
Concurrency: 234,
RPS: 234,
},
"host3": {
Concurrency: 345,
RPS: 345,
},
"host4": {
Concurrency: 456,
RPS: 456,
},
}
q := queue.NewMemory()
for host, count := range counts.Counts {
q.EnsureKey(host, time.Minute, time.Second)
r.NoError(q.Increase(host, count.Concurrency))
}
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 := newQueuePinger(
logr.Discard(),
endpointsFn,
ns,
svcName,
deplName,
srvURL.Port(),
)
r.NoError(pinger.fetchAndSaveCounts(ctx))
// 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 {
val.Concurrency *= 3
val.RPS *= 3
expectedCounts[host] = val
}
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]queue.Count{
"host1": {
Concurrency: 123,
RPS: 123,
},
"host2": {
Concurrency: 234,
RPS: 234,
},
"host3": {
Concurrency: 345,
RPS: 345,
},
"host4": {
Concurrency: 456,
RPS: 456,
},
}
q := queue.NewMemory()
for host, count := range counts.Counts {
q.EnsureKey(host, time.Minute, time.Second)
r.NoError(q.Increase(host, count.Concurrency))
}
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, err := fetchCounts(
ctx,
logr.Discard(),
endpointsFn,
ns,
svcName,
srvURL.Port(),
)
r.NoError(err)
// 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 {
val.Concurrency *= 3
val.RPS *= 3
expectedCounts[host] = val
}
r.Equal(expectedCounts, cts)
}
// startFakeQueuePinger starts a fake server that simulates
// an interceptor with its /queue endpoint, then returns a
// *v1.Endpoints object that contains the URL of the new fake
// server. also returns the *httptest.Server that runs the
// endpoint along with its URL. the caller is responsible for
// calling testServer.Close() when done.
//
// returns nil for the first 3 return value and a non-nil error in
// case of a failure.
func startFakeQueueEndpointServer(
svcName string,
q queue.CountReader,
numEndpoints int,
) (*httptest.Server, *url.URL, *v1.Endpoints, error) {
hdl := http.NewServeMux()
queue.AddCountsRoute(logr.Discard(), hdl, q)
srv, srvURL, err := kedanet.StartTestServer(hdl)
if err != nil {
return nil, nil, nil, err
}
endpoints, err := k8s.FakeEndpointsForURL(srvURL, "testns", svcName, numEndpoints)
if err != nil {
return nil, nil, nil, err
}
return srv, srvURL, endpoints, nil
}
type fakeQueuePingerOpts struct {
endpoints *v1.Endpoints
tickDur time.Duration
port string
}
type optsFunc func(*fakeQueuePingerOpts)
// newFakeQueuePinger creates the machinery required for a fake
// queuePinger implementation, including a time.Ticker, then returns
// the ticker and the pinger. it is the caller's responsibility to
// call ticker.Stop() on the returned ticker.
func newFakeQueuePinger(
lggr logr.Logger,
optsFuncs ...optsFunc,
) (*time.Ticker, *queuePinger, error) {
opts := &fakeQueuePingerOpts{
endpoints: &v1.Endpoints{},
tickDur: time.Second,
port: "8080",
}
for _, optsFunc := range optsFuncs {
optsFunc(opts)
}
ticker := time.NewTicker(opts.tickDur)
pinger := newQueuePinger(
lggr,
func(context.Context, string, string) (*v1.Endpoints, error) {
return opts.endpoints, nil
},
"testns",
"testsvc",
"testdepl",
opts.port,
)
return ticker, pinger, nil
}