-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient_test.go
More file actions
687 lines (585 loc) · 17.8 KB
/
client_test.go
File metadata and controls
687 lines (585 loc) · 17.8 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Package aiven provides a client for interacting with the Aiven API.
package aiven
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"testing/synctest"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aiven/go-client-codegen/handler/clickhouse"
"github.com/aiven/go-client-codegen/handler/service"
)
func TestNewClient(t *testing.T) {
token := os.Getenv("AIVEN_TOKEN")
if token == "" {
t.Skip("token is required for the test")
}
c, err := NewClient(DebugOpt(true))
require.NoError(t, err)
ctx := context.Background()
tokens, err := c.AccessTokenList(ctx)
require.NoError(t, err)
found := 0
for _, to := range tokens {
if strings.HasPrefix(token, to.TokenPrefix) {
found++
}
}
assert.Equal(t, 1, found)
}
func TestServiceCreate(t *testing.T) {
var callCount int64
// Creates a test server
mux := http.NewServeMux()
mux.HandleFunc(
"/v1/project/aiven-project/service",
func(w http.ResponseWriter, r *http.Request) {
// Validates request
expectIn := new(service.ServiceCreateIn)
err := json.NewDecoder(r.Body).Decode(expectIn)
assert.NoError(t, err)
assert.Equal(t, "my-clickhouse", expectIn.ServiceName)
assert.Equal(t, "clickhouse", expectIn.ServiceType)
assert.Regexp(t, `go-client-codegen/[0-9\.]+ unit-test`, r.Header["User-Agent"])
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`{"service": {"plan": "wow", "state": "RUNNING"}}`))
assert.NoError(t, err)
atomic.AddInt64(&callCount, 1)
},
)
mux.HandleFunc(
"/v1/project/aiven-project/service/my-clickhouse/clickhouse/query/stats",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "limit=1&order_by=max_time%3Aasc", r.URL.RawQuery)
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"queries": [{"calls": 1}]}`))
assert.NoError(t, err)
atomic.AddInt64(&callCount, 1)
},
)
server := httptest.NewServer(mux)
defer server.Close()
// Points a new client to the server url
c, err := NewClient(TokenOpt("token"), HostOpt(server.URL), UserAgentOpt("unit-test"))
require.NotNil(t, c)
require.NoError(t, err)
// Makes create request
in := &service.ServiceCreateIn{
ServiceName: "my-clickhouse",
ServiceType: "clickhouse",
}
ctx := context.Background()
project := "aiven-project"
out, err := c.ServiceCreate(ctx, project, in)
require.NoError(t, err)
require.NotNil(t, out)
assert.Equal(t, "wow", out.Plan)
assert.Equal(t, service.ServiceStateTypeRunning, out.State)
// Validates query params
stats, err := c.ServiceClickHouseQueryStats(
ctx, project, in.ServiceName,
clickhouse.ServiceClickHouseQueryStatsLimit(1),
clickhouse.ServiceClickHouseQueryStatsOrderByType(clickhouse.OrderByTypeMaxTimeAsc),
)
require.NoError(t, err)
assert.Len(t, stats, 1)
// All calls are received
assert.EqualValues(t, 2, callCount)
}
func TestServiceCreateErrorsRetries(t *testing.T) {
cases := []struct {
Name string
ResponseBody string
ErrorExpect string
RetryMax int
CallsExpect int
}{
{
Name: "message field only",
ResponseBody: `{"message": "Internal Server Error"}`,
ErrorExpect: "[500 ServiceCreate]: Internal Server Error",
RetryMax: 6,
CallsExpect: 7,
},
{
Name: "with errors field, list",
ResponseBody: `{"message": "Something went wrong", "errors": ["oh!", "no!"]}`,
ErrorExpect: `[500 ServiceCreate]: Something went wrong (["oh!","no!"])`,
RetryMax: 1,
CallsExpect: 2,
},
{
Name: "with errors field, string",
ResponseBody: `{"message": "Something went wrong", "errors": "wow!"}`,
ErrorExpect: `[500 ServiceCreate]: Something went wrong ("wow!")`,
RetryMax: 1,
CallsExpect: 2,
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
var callsActual int64
// Creates a test server
mux := http.NewServeMux()
mux.HandleFunc(
"/v1/project/aiven-project/service",
func(w http.ResponseWriter, _ *http.Request) {
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte(tt.ResponseBody))
assert.NoError(t, err)
atomic.AddInt64(&callsActual, 1)
},
)
server := httptest.NewServer(mux)
// Points a new client to the server url
c, err := NewClient(
TokenOpt("token"),
UserAgentOpt("unit-test"),
HostOpt(server.URL),
RetryMaxOpt(tt.RetryMax),
RetryWaitMinOpt(1*time.Millisecond),
RetryWaitMaxOpt(3*time.Millisecond),
DebugOpt(false),
)
require.NotNil(t, c)
require.NoError(t, err)
// Makes create request
in := &service.ServiceCreateIn{
ServiceName: "my-clickhouse",
ServiceType: "clickhouse",
}
ctx := context.Background()
out, err := c.ServiceCreate(ctx, "aiven-project", in)
assert.Nil(t, out)
assert.Equal(t, err.Error(), tt.ErrorExpect)
// All calls are received
assert.EqualValues(t, tt.CallsExpect, callsActual)
server.Close()
})
}
}
func TestFmtQuery(t *testing.T) {
t.Parallel()
tests := []struct {
name string
operationID string
query [][2]string
want string
}{
{
name: "With no params",
operationID: "TestOperation",
query: nil,
want: "limit=999",
},
{
name: "With existing params",
operationID: "TestOperation",
query: [][2]string{
{"foo", "bar"},
{"baz", "qux"},
},
want: "baz=qux&foo=bar&limit=999",
},
{
name: "With custom limit",
operationID: "TestOperation",
query: [][2]string{
{"limit", "50"},
},
want: "limit=50",
},
{
name: "Ignored operation",
operationID: "ServiceKafkaQuotaDescribe",
query: [][2]string{
{"foo", "bar"},
},
want: "foo=bar",
},
{
name: "Multiple parameters with same key",
operationID: "TestOperation",
query: [][2]string{
{"tag", "v1"},
{"tag", "v2"},
},
want: "limit=999&tag=v1&tag=v2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := fmtQuery(tt.operationID, tt.query...)
if got != tt.want {
t.Errorf("fmtQuery() = %v, want %v", got, tt.want)
}
})
}
}
func TestServiceIntegrationEndpointGet(t *testing.T) {
var callCount int64
// Creates a test server
mux := http.NewServeMux()
mux.HandleFunc(
"/v1/project/{project}/integration_endpoint/{integration_endpoint_id}",
func(w http.ResponseWriter, r *http.Request) {
assert.Regexp(t, `go-client-codegen/[0-9\.]+ unit-test`, r.Header["User-Agent"])
assert.Equal(t, "/v1/project/aiven-endpoint-project/integration_endpoint/foo?include_secrets=true&limit=999", r.RequestURI)
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"service_integration_endpoint": {"endpoint_name": "wow"}}`))
assert.NoError(t, err)
atomic.AddInt64(&callCount, 1)
},
)
server := httptest.NewServer(mux)
defer server.Close()
// Points a new client to the server url
c, err := NewClient(TokenOpt("token"), HostOpt(server.URL), UserAgentOpt("unit-test"))
require.NotNil(t, c)
require.NoError(t, err)
ctx := context.Background()
project := "aiven-endpoint-project"
out, err := c.ServiceIntegrationEndpointGet(ctx, project, "foo", service.ServiceIntegrationEndpointGetIncludeSecrets(true))
require.NoError(t, err)
require.NotNil(t, out)
assert.Equal(t, "wow", out.EndpointName)
// All calls are received
assert.EqualValues(t, 1, callCount)
}
type doerFunc func(*http.Request) (*http.Response, error)
func (f doerFunc) Do(req *http.Request) (*http.Response, error) {
return f(req)
}
func TestSingleFlightDeduplication(t *testing.T) {
// This test checks that EnableSingleFlight deduplicates concurrent identical requests:
// the first call reaches the HTTP Doer and blocks, and all other concurrent calls reuse
// the same in-flight result without reaching the Doer themselves.
//
// We use a blocking Doer and testing/synctest to make the execution deterministic:
// synctest.Wait() pauses the test until all goroutines in the bubble are durably blocked.
synctest.Test(t, func(t *testing.T) {
const concurrency = 20
// 1) Create a client with a Doer that blocks until we release it.
// releaseNow is called explicitly and also deferred to avoid leaving goroutines stuck if the test fails early.
var releaseOnce sync.Once
release := make(chan struct{})
releaseNow := func() { releaseOnce.Do(func() { close(release) }) }
defer releaseNow()
const body = `{"services":[{"service_name":"svc"}]}`
entered := make(chan struct{})
c, err := NewClient(
TokenOpt("token"),
HostOpt("http://example.com"),
UserAgentOpt("unit-test"),
DoerOpt(doerFunc(func(req *http.Request) (*http.Response, error) {
// 2) Signal that the first request reached the Doer.
// If a second request reaches the Doer, this will panic (close of closed channel).
close(entered)
// 3) Block the first request so the rest of the goroutines can race into singleflight.
<-release
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader(body)),
ContentLength: int64(len(body)),
Request: req,
}, nil
})),
EnableSingleFlightOpt(true),
)
require.NoError(t, err)
ctx := t.Context()
// 4) Start the first request and wait until it reaches the Doer (and blocks).
var firstErr error
var firstRes []service.ServiceOut
firstDone := make(chan struct{})
go func() {
defer close(firstDone)
firstRes, firstErr = c.ServiceList(ctx, "test-project")
}()
<-entered
synctest.Wait()
// 5) Start concurrent requests. They mustn't reach the Doer while the first is in-flight.
res := make([][]service.ServiceOut, concurrency)
errs := make([]error, concurrency)
var wg sync.WaitGroup
for i := range concurrency {
wg.Go(func() {
res[i], errs[i] = c.ServiceList(ctx, "test-project")
})
}
synctest.Wait()
// 6) Release the first request and wait for every goroutine to finish.
releaseNow()
<-firstDone
require.NoError(t, firstErr)
wg.Wait()
// 7) All callers should see the same result.
for _, err := range errs {
require.NoError(t, err)
}
for _, r := range res {
require.Equal(t, firstRes, r)
}
})
}
func TestSingleFlightDeduplicationHTTPServer(t *testing.T) {
const concurrency = 20
var callCount int64
var releaseOnce sync.Once
release := make(chan struct{})
releaseNow := func() { releaseOnce.Do(func() { close(release) }) }
defer releaseNow()
entered := make(chan struct{})
mux := http.NewServeMux()
mux.HandleFunc(
"/v1/project/test-project/service",
func(w http.ResponseWriter, _ *http.Request) {
if atomic.AddInt64(&callCount, 1) == 1 {
close(entered)
}
<-release
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"services":[{"service_name":"svc"}]}`))
},
)
server := httptest.NewServer(mux)
defer server.Close()
c, err := NewClient(
TokenOpt("token"),
HostOpt(server.URL),
UserAgentOpt("unit-test"),
DoerOpt(server.Client()),
EnableSingleFlightOpt(true),
)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
start := make(chan struct{})
var ready sync.WaitGroup
ready.Add(concurrency)
type callResult struct {
res []service.ServiceOut
err error
}
results := make(chan callResult, concurrency)
var wg sync.WaitGroup
for range concurrency {
wg.Go(func() {
ready.Done()
<-start
res, err := c.ServiceList(ctx, "test-project")
results <- callResult{res: res, err: err}
})
}
ready.Wait()
close(start)
select {
case <-entered:
case <-time.After(5 * time.Second):
t.Fatal("server wasn't called")
}
// Give other goroutines a chance to enter the client call while the server is still blocked.
time.Sleep(50 * time.Millisecond)
releaseNow()
wg.Wait()
close(results)
var first []service.ServiceOut
for r := range results {
require.NoError(t, r.err)
if first == nil {
first = r.res
continue
}
require.Equal(t, first, r.res)
}
require.Less(t, atomic.LoadInt64(&callCount), int64(concurrency))
}
// TestSingleFlightDifferentProjects tests that singleflight doesn't merge different paths.
func TestSingleFlightDifferentProjects(t *testing.T) {
// This test checks that EnableSingleFlight does not merge requests with different paths.
// Here we use two different project names, so the generated URL path differs.
synctest.Test(t, func(t *testing.T) {
// 1) Arrange: a Doer that blocks so requests overlap in time.
var releaseOnce sync.Once
release := make(chan struct{})
releaseNow := func() { releaseOnce.Do(func() { close(release) }) }
defer releaseNow()
const (
bodyA = `{"services":[{"service_name":"svc-a"}]}`
bodyB = `{"services":[{"service_name":"svc-b"}]}`
)
enteredA := make(chan struct{})
enteredB := make(chan struct{})
c, err := NewClient(
TokenOpt("token"),
HostOpt("http://example.com"),
UserAgentOpt("unit-test"),
DoerOpt(doerFunc(func(req *http.Request) (*http.Response, error) {
var body string
switch req.URL.Path {
case "/v1/project/project-a/service":
// If the same project request reaches the Doer twice, panic (close of closed channel).
close(enteredA)
body = bodyA
case "/v1/project/project-b/service":
close(enteredB)
body = bodyB
default:
return nil, errors.New("unexpected request path: " + req.URL.Path)
}
<-release
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader(body)),
ContentLength: int64(len(body)),
Request: req,
}, nil
})),
EnableSingleFlightOpt(true),
)
require.NoError(t, err)
ctx := t.Context()
type listResult struct {
res []service.ServiceOut
err error
}
// 2) Start the first request and wait until it reaches the Doer (and blocks).
projectACh := make(chan listResult, 1)
go func() {
res, err := c.ServiceList(ctx, "project-a")
projectACh <- listResult{res: res, err: err}
}()
<-enteredA
synctest.Wait()
// 3) Start the second request while the first is still in-flight. It must reach the Doer too.
projectBCh := make(chan listResult, 1)
go func() {
res, err := c.ServiceList(ctx, "project-b")
projectBCh <- listResult{res: res, err: err}
}()
synctest.Wait()
select {
case <-enteredB:
default:
t.Fatal("project-b request did not reach the Doer; likely deduplicated with project-a")
}
// 4) Release both requests and verify results.
releaseNow()
projectARes := <-projectACh
projectBRes := <-projectBCh
require.NoError(t, projectARes.err)
require.NoError(t, projectBRes.err)
require.Len(t, projectARes.res, 1)
require.Len(t, projectBRes.res, 1)
require.NotEqual(t, projectARes.res, projectBRes.res)
require.Equal(t, "svc-a", projectARes.res[0].ServiceName)
require.Equal(t, "svc-b", projectBRes.res[0].ServiceName)
})
}
func TestSingleFlightMethodIsolation(t *testing.T) {
// This test checks that singleflight never merges requests with different HTTP methods.
// We run several concurrent requests for the same path+query, but with different methods.
// Each method should start its own in-flight request (and deduplicate only within itself).
synctest.Test(t, func(t *testing.T) {
const copies = 5
// A blocking Doer makes all requests overlap in time.
var releaseOnce sync.Once
release := make(chan struct{})
releaseNow := func() { releaseOnce.Do(func() { close(release) }) }
defer releaseNow()
methods := []string{
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodTrace,
}
entered := make(map[string]chan struct{}, len(methods))
for _, method := range methods {
entered[method] = make(chan struct{})
}
queryLimit := [2]string{"limit", "1"}
d := &aivenClient{
Host: "http://example.com",
UserAgent: "unit-test",
Token: "token",
EnableSingleFlight: true,
doer: doerFunc(func(req *http.Request) (*http.Response, error) {
if req.URL.Path != "/v1/test" {
return nil, errors.New("unexpected request path: " + req.URL.Path)
}
if req.URL.RawQuery != "limit=1" {
return nil, errors.New("unexpected request query: " + req.URL.RawQuery)
}
ch, ok := entered[req.Method]
if !ok {
return nil, errors.New("unexpected request method: " + req.Method)
}
// If the same method reaches the Doer twice, panic (close of closed channel).
close(ch)
<-release
body := req.Method
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader(body)),
ContentLength: int64(len(body)),
Request: req,
}, nil
}),
}
ctx := t.Context()
type callResult struct {
method string
body []byte
err error
}
results := make(chan callResult, copies*len(methods))
var wg sync.WaitGroup
for _, method := range methods {
for range copies {
wg.Go(func() {
b, err := d.Do(ctx, "op", method, "/v1/test", nil, queryLimit)
results <- callResult{method: method, body: b, err: err}
})
}
}
synctest.Wait()
// Ensure every method reached the Doer (i.e., didn't merge into another one).
for _, method := range methods {
select {
case <-entered[method]:
default:
t.Errorf("%s request did not reach the Doer; likely merged into another method", method)
}
}
releaseNow()
wg.Wait()
close(results)
for r := range results {
require.NoError(t, r.err)
require.Equal(t, r.method, string(r.body))
}
})
}