Skip to content

Commit a698c23

Browse files
feat(wait tests) wrap wait tests with synctest for determnistic runs (#6325)
* feat(wait tests) wrap wait tests with synctest for determnistic runs * Update services/resourcemanager/wait/wait_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * fix(lint) delete unused import * fix(synctest) migrate all logme v1api wait_tests --------- Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com>
1 parent 015330c commit a698c23

54 files changed

Lines changed: 5105 additions & 4617 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/wait/wait_test.go

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"testing"
8+
"testing/synctest"
89
"time"
910

1011
"github.com/google/go-cmp/cmp"
@@ -331,54 +332,56 @@ func TestWaitWithContext(t *testing.T) {
331332
},
332333
} {
333334
t.Run(tt.desc, func(t *testing.T) {
334-
type respType struct{}
335+
synctest.Test(t, func(t *testing.T) {
336+
type respType struct{}
335337

336-
numberCheckFnCalls := 0
337-
checkFn := func() (waitFinished bool, response *respType, err error) {
338-
numberCheckFnCalls++
339-
if numberCheckFnCalls == tt.checkFnNumberCallsToFinishWait {
340-
if tt.checkFnWaitSucceeds {
341-
return true, &respType{}, nil
338+
numberCheckFnCalls := 0
339+
checkFn := func() (waitFinished bool, response *respType, err error) {
340+
numberCheckFnCalls++
341+
if numberCheckFnCalls == tt.checkFnNumberCallsToFinishWait {
342+
if tt.checkFnWaitSucceeds {
343+
return true, &respType{}, nil
344+
}
345+
return true, &respType{}, fmt.Errorf("the async action couldn't be done")
342346
}
343-
return true, &respType{}, fmt.Errorf("the async action couldn't be done")
344-
}
345347

346-
if numberCheckFnCalls < tt.checkFnNumberCallsUntilErr {
347-
return false, nil, nil
348-
}
348+
if numberCheckFnCalls < tt.checkFnNumberCallsUntilErr {
349+
return false, nil, nil
350+
}
349351

350-
if tt.checkFnReturnsTempErr {
351-
return false, nil, &oapierror.GenericOpenAPIError{
352-
StatusCode: RetryHttpErrorStatusCodes[0],
353-
ErrorMessage: "something bad happened when checking if the async action was finished",
352+
if tt.checkFnReturnsTempErr {
353+
return false, nil, &oapierror.GenericOpenAPIError{
354+
StatusCode: RetryHttpErrorStatusCodes[0],
355+
ErrorMessage: "something bad happened when checking if the async action was finished",
356+
}
354357
}
358+
return false, nil, fmt.Errorf("something bad happened when checking if the async action was finished")
355359
}
356-
return false, nil, fmt.Errorf("something bad happened when checking if the async action was finished")
357-
}
358-
handler := AsyncActionHandler[respType]{
359-
checkFn: checkFn,
360-
sleepBeforeWait: tt.handlerSleepBeforeWait,
361-
throttle: tt.handlerThrottle,
362-
timeout: tt.handlerTimeout,
363-
tempErrRetryLimit: tt.handlerTempErrRetryLimit,
364-
}
365-
ctx, cancel := context.WithTimeout(context.Background(), tt.contextTimeout)
366-
defer cancel()
360+
handler := AsyncActionHandler[respType]{
361+
checkFn: checkFn,
362+
sleepBeforeWait: tt.handlerSleepBeforeWait,
363+
throttle: tt.handlerThrottle,
364+
timeout: tt.handlerTimeout,
365+
tempErrRetryLimit: tt.handlerTempErrRetryLimit,
366+
}
367+
ctx, cancel := context.WithTimeout(context.Background(), tt.contextTimeout)
368+
defer cancel()
367369

368-
resp, err := handler.WaitWithContext(ctx)
370+
resp, err := handler.WaitWithContext(ctx)
369371

370-
if tt.wantErr && (err == nil) {
371-
t.Errorf("expected error but got none")
372-
}
373-
if !tt.wantErr && (err != nil) {
374-
t.Errorf("expected no error but got \"%v\"", err)
375-
}
376-
if (err == nil) && (resp == nil) {
377-
t.Errorf("got nil err but nil resp")
378-
}
379-
if numberCheckFnCalls != tt.wantCheckFnNumberCalls {
380-
t.Errorf("expected %d calls to checkFn but got %d instead", tt.wantCheckFnNumberCalls, numberCheckFnCalls)
381-
}
372+
if tt.wantErr && (err == nil) {
373+
t.Errorf("expected error but got none")
374+
}
375+
if !tt.wantErr && (err != nil) {
376+
t.Errorf("expected no error but got \"%v\"", err)
377+
}
378+
if (err == nil) && (resp == nil) {
379+
t.Errorf("got nil err but nil resp")
380+
}
381+
if numberCheckFnCalls != tt.wantCheckFnNumberCalls {
382+
t.Errorf("expected %d calls to checkFn but got %d instead", tt.wantCheckFnNumberCalls, numberCheckFnCalls)
383+
}
384+
})
382385
})
383386
}
384387
}

services/alb/v2api/wait/wait_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"errors"
66
"net/http"
77
"testing"
8-
"time"
8+
"testing/synctest"
99

1010
"github.com/google/go-cmp/cmp"
1111
"github.com/google/uuid"
@@ -109,23 +109,23 @@ func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
109109
}
110110
for _, tt := range tests {
111111
t.Run(tt.name, func(t *testing.T) {
112-
ctx := context.Background()
113-
client := newAPIMock(&mockSettings{
114-
responses: tt.responses,
115-
})
112+
synctest.Test(t, func(t *testing.T) {
113+
ctx := context.Background()
114+
client := newAPIMock(&mockSettings{
115+
responses: tt.responses,
116+
})
116117

117-
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
118-
got, err := handler.SetTimeout(1 * time.Second).
119-
SetThrottle(250 * time.Millisecond).
120-
WaitWithContext(ctx)
118+
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
119+
got, err := handler.WaitWithContext(ctx)
121120

122-
if (err != nil) != tt.wantErr {
123-
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
124-
}
121+
if (err != nil) != tt.wantErr {
122+
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
123+
}
125124

126-
if diff := cmp.Diff(tt.want, got); diff != "" {
127-
t.Errorf("differing loadbalancer %s", diff)
128-
}
125+
if diff := cmp.Diff(tt.want, got); diff != "" {
126+
t.Errorf("differing loadbalancer %s", diff)
127+
}
128+
})
129129
})
130130
}
131131
}
@@ -193,25 +193,25 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
193193
}
194194
for _, tt := range tests {
195195
t.Run(tt.name, func(t *testing.T) {
196-
ctx := context.Background()
197-
client := newAPIMock(&mockSettings{
198-
responses: tt.responses,
199-
})
196+
synctest.Test(t, func(t *testing.T) {
197+
ctx := context.Background()
198+
client := newAPIMock(&mockSettings{
199+
responses: tt.responses,
200+
})
200201

201-
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
202-
_, err := handler.SetTimeout(1 * time.Second).
203-
SetThrottle(250 * time.Millisecond).
204-
WaitWithContext(ctx)
202+
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
203+
_, err := handler.WaitWithContext(ctx)
205204

206-
if tt.wantErr != (err != nil) {
207-
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
208-
}
209-
if tt.wantErr {
210-
var apiErr *oapierror.GenericOpenAPIError
211-
if !errors.As(err, &apiErr) {
212-
t.Fatalf("expected openapi error, got %v", err)
205+
if tt.wantErr != (err != nil) {
206+
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
207+
}
208+
if tt.wantErr {
209+
var apiErr *oapierror.GenericOpenAPIError
210+
if !errors.As(err, &apiErr) {
211+
t.Fatalf("expected openapi error, got %v", err)
212+
}
213213
}
214-
}
214+
})
215215
})
216216
}
217217
}

services/alb/wait/wait_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"errors"
66
"net/http"
77
"testing"
8-
"time"
8+
"testing/synctest"
99

1010
"github.com/google/go-cmp/cmp"
1111
"github.com/google/uuid"
@@ -98,22 +98,22 @@ func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
9898
}
9999
for _, tt := range tests {
100100
t.Run(tt.name, func(t *testing.T) {
101-
ctx := context.Background()
102-
client := &apiClientLoadbalancerMocked{
103-
responses: tt.responses,
104-
}
105-
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
106-
got, err := handler.SetTimeout(1 * time.Second).
107-
SetThrottle(250 * time.Millisecond).
108-
WaitWithContext(ctx)
101+
synctest.Test(t, func(t *testing.T) {
102+
ctx := context.Background()
103+
client := &apiClientLoadbalancerMocked{
104+
responses: tt.responses,
105+
}
106+
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
107+
got, err := handler.WaitWithContext(ctx)
109108

110-
if (err != nil) != tt.wantErr {
111-
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
112-
}
109+
if (err != nil) != tt.wantErr {
110+
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
111+
}
113112

114-
if diff := cmp.Diff(tt.want, got); diff != "" {
115-
t.Errorf("differing loadbalancer %s", diff)
116-
}
113+
if diff := cmp.Diff(tt.want, got); diff != "" {
114+
t.Errorf("differing loadbalancer %s", diff)
115+
}
116+
})
117117
})
118118
}
119119
}
@@ -181,24 +181,24 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
181181
}
182182
for _, tt := range tests {
183183
t.Run(tt.name, func(t *testing.T) {
184-
ctx := context.Background()
185-
client := &apiClientLoadbalancerMocked{
186-
responses: tt.responses,
187-
}
188-
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
189-
_, err := handler.SetTimeout(1 * time.Second).
190-
SetThrottle(250 * time.Millisecond).
191-
WaitWithContext(ctx)
184+
synctest.Test(t, func(t *testing.T) {
185+
ctx := context.Background()
186+
client := &apiClientLoadbalancerMocked{
187+
responses: tt.responses,
188+
}
189+
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
190+
_, err := handler.WaitWithContext(ctx)
192191

193-
if tt.wantErr != (err != nil) {
194-
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
195-
}
196-
if tt.wantErr {
197-
var apiErr *oapierror.GenericOpenAPIError
198-
if !errors.As(err, &apiErr) {
199-
t.Fatalf("expected openapi error, got %v", err)
192+
if tt.wantErr != (err != nil) {
193+
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
194+
}
195+
if tt.wantErr {
196+
var apiErr *oapierror.GenericOpenAPIError
197+
if !errors.As(err, &apiErr) {
198+
t.Fatalf("expected openapi error, got %v", err)
199+
}
200200
}
201-
}
201+
})
202202
})
203203
}
204204
}

services/cdn/v1api/wait/wait_test.go

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"strings"
88
"testing"
9+
"testing/synctest"
910
"time"
1011

1112
"github.com/google/go-cmp/cmp"
@@ -173,12 +174,14 @@ func TestCreateDistributionWaitHandler(t *testing.T) {
173174

174175
for name, tc := range tests {
175176
t.Run(name, func(t *testing.T) {
176-
handler := CreateDistributionPoolWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
177-
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
178-
tc.errCheck(t, err)
179-
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
180-
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
181-
}
177+
synctest.Test(t, func(t *testing.T) {
178+
handler := CreateDistributionPoolWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
179+
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
180+
tc.errCheck(t, err)
181+
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
182+
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
183+
}
184+
})
182185
})
183186
}
184187
}
@@ -287,12 +290,14 @@ func TestDeleteDistributionWaitHandler(t *testing.T) {
287290

288291
for name, tc := range tests {
289292
t.Run(name, func(t *testing.T) {
290-
handler := DeleteDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
291-
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
292-
tc.errCheck(t, err)
293-
if dist != nil {
294-
t.Fatalf("Distribution not deleted")
295-
}
293+
synctest.Test(t, func(t *testing.T) {
294+
handler := DeleteDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
295+
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
296+
tc.errCheck(t, err)
297+
if dist != nil {
298+
t.Fatalf("Distribution not deleted")
299+
}
300+
})
296301
})
297302
}
298303
}
@@ -402,12 +407,14 @@ func TestUpdateDistributionWaitHandler(t *testing.T) {
402407

403408
for name, tc := range tests {
404409
t.Run(name, func(t *testing.T) {
405-
handler := UpdateDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
406-
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
407-
tc.errCheck(t, err)
408-
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
409-
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
410-
}
410+
synctest.Test(t, func(t *testing.T) {
411+
handler := UpdateDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
412+
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
413+
tc.errCheck(t, err)
414+
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
415+
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
416+
}
417+
})
411418
})
412419
}
413420
}
@@ -519,12 +526,14 @@ func TestCreateCustomDomainWaitHandler(t *testing.T) {
519526

520527
for name, tc := range tests {
521528
t.Run(name, func(t *testing.T) {
522-
handler := CreateCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
523-
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
524-
tc.errCheck(t, err)
525-
if diff := cmp.Diff(tc.expectedCustomDomain, customDomain); diff != "" {
526-
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
527-
}
529+
synctest.Test(t, func(t *testing.T) {
530+
handler := CreateCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
531+
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
532+
tc.errCheck(t, err)
533+
if diff := cmp.Diff(tc.expectedCustomDomain, customDomain); diff != "" {
534+
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
535+
}
536+
})
528537
})
529538
}
530539
}
@@ -640,12 +649,14 @@ func TestDeleteCustomDomainHandler(t *testing.T) {
640649

641650
for name, tc := range tests {
642651
t.Run(name, func(t *testing.T) {
643-
handler := DeleteCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
644-
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
645-
tc.errCheck(t, err)
646-
if customDomain != nil {
647-
t.Fatalf("Custom domain not deleted")
648-
}
652+
synctest.Test(t, func(t *testing.T) {
653+
handler := DeleteCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
654+
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
655+
tc.errCheck(t, err)
656+
if customDomain != nil {
657+
t.Fatalf("Custom domain not deleted")
658+
}
659+
})
649660
})
650661
}
651662
}

0 commit comments

Comments
 (0)