Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 43 additions & 40 deletions core/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"testing"
"testing/synctest"
"time"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -331,54 +332,56 @@ func TestWaitWithContext(t *testing.T) {
},
} {
t.Run(tt.desc, func(t *testing.T) {
type respType struct{}
synctest.Test(t, func(t *testing.T) {
type respType struct{}

numberCheckFnCalls := 0
checkFn := func() (waitFinished bool, response *respType, err error) {
numberCheckFnCalls++
if numberCheckFnCalls == tt.checkFnNumberCallsToFinishWait {
if tt.checkFnWaitSucceeds {
return true, &respType{}, nil
numberCheckFnCalls := 0
checkFn := func() (waitFinished bool, response *respType, err error) {
numberCheckFnCalls++
if numberCheckFnCalls == tt.checkFnNumberCallsToFinishWait {
if tt.checkFnWaitSucceeds {
return true, &respType{}, nil
}
return true, &respType{}, fmt.Errorf("the async action couldn't be done")
}
return true, &respType{}, fmt.Errorf("the async action couldn't be done")
}

if numberCheckFnCalls < tt.checkFnNumberCallsUntilErr {
return false, nil, nil
}
if numberCheckFnCalls < tt.checkFnNumberCallsUntilErr {
return false, nil, nil
}

if tt.checkFnReturnsTempErr {
return false, nil, &oapierror.GenericOpenAPIError{
StatusCode: RetryHttpErrorStatusCodes[0],
ErrorMessage: "something bad happened when checking if the async action was finished",
if tt.checkFnReturnsTempErr {
return false, nil, &oapierror.GenericOpenAPIError{
StatusCode: RetryHttpErrorStatusCodes[0],
ErrorMessage: "something bad happened when checking if the async action was finished",
}
}
return false, nil, fmt.Errorf("something bad happened when checking if the async action was finished")
}
return false, nil, fmt.Errorf("something bad happened when checking if the async action was finished")
}
handler := AsyncActionHandler[respType]{
checkFn: checkFn,
sleepBeforeWait: tt.handlerSleepBeforeWait,
throttle: tt.handlerThrottle,
timeout: tt.handlerTimeout,
tempErrRetryLimit: tt.handlerTempErrRetryLimit,
}
ctx, cancel := context.WithTimeout(context.Background(), tt.contextTimeout)
defer cancel()
handler := AsyncActionHandler[respType]{
checkFn: checkFn,
sleepBeforeWait: tt.handlerSleepBeforeWait,
throttle: tt.handlerThrottle,
timeout: tt.handlerTimeout,
tempErrRetryLimit: tt.handlerTempErrRetryLimit,
}
ctx, cancel := context.WithTimeout(context.Background(), tt.contextTimeout)
defer cancel()

resp, err := handler.WaitWithContext(ctx)
resp, err := handler.WaitWithContext(ctx)

if tt.wantErr && (err == nil) {
t.Errorf("expected error but got none")
}
if !tt.wantErr && (err != nil) {
t.Errorf("expected no error but got \"%v\"", err)
}
if (err == nil) && (resp == nil) {
t.Errorf("got nil err but nil resp")
}
if numberCheckFnCalls != tt.wantCheckFnNumberCalls {
t.Errorf("expected %d calls to checkFn but got %d instead", tt.wantCheckFnNumberCalls, numberCheckFnCalls)
}
if tt.wantErr && (err == nil) {
t.Errorf("expected error but got none")
}
if !tt.wantErr && (err != nil) {
t.Errorf("expected no error but got \"%v\"", err)
}
if (err == nil) && (resp == nil) {
t.Errorf("got nil err but nil resp")
}
if numberCheckFnCalls != tt.wantCheckFnNumberCalls {
t.Errorf("expected %d calls to checkFn but got %d instead", tt.wantCheckFnNumberCalls, numberCheckFnCalls)
}
})
})
}
}
Expand Down
62 changes: 31 additions & 31 deletions services/alb/v2api/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"net/http"
"testing"
"time"
"testing/synctest"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
Expand Down Expand Up @@ -109,23 +109,23 @@ func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
client := newAPIMock(&mockSettings{
responses: tt.responses,
})
synctest.Test(t, func(t *testing.T) {
ctx := context.Background()
client := newAPIMock(&mockSettings{
responses: tt.responses,
})

handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
got, err := handler.SetTimeout(1 * time.Second).
SetThrottle(250 * time.Millisecond).
WaitWithContext(ctx)
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
got, err := handler.WaitWithContext(ctx)

if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
}
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
}

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("differing loadbalancer %s", diff)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("differing loadbalancer %s", diff)
}
})
})
}
}
Expand Down Expand Up @@ -193,25 +193,25 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
client := newAPIMock(&mockSettings{
responses: tt.responses,
})
synctest.Test(t, func(t *testing.T) {
ctx := context.Background()
client := newAPIMock(&mockSettings{
responses: tt.responses,
})

handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
_, err := handler.SetTimeout(1 * time.Second).
SetThrottle(250 * time.Millisecond).
WaitWithContext(ctx)
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
_, err := handler.WaitWithContext(ctx)

if tt.wantErr != (err != nil) {
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
}
if tt.wantErr {
var apiErr *oapierror.GenericOpenAPIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected openapi error, got %v", err)
if tt.wantErr != (err != nil) {
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
}
if tt.wantErr {
var apiErr *oapierror.GenericOpenAPIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected openapi error, got %v", err)
}
}
}
})
})
}
}
62 changes: 31 additions & 31 deletions services/alb/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"net/http"
"testing"
"time"
"testing/synctest"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
Expand Down Expand Up @@ -98,22 +98,22 @@ func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
client := &apiClientLoadbalancerMocked{
responses: tt.responses,
}
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
got, err := handler.SetTimeout(1 * time.Second).
SetThrottle(250 * time.Millisecond).
WaitWithContext(ctx)
synctest.Test(t, func(t *testing.T) {
ctx := context.Background()
client := &apiClientLoadbalancerMocked{
responses: tt.responses,
}
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
got, err := handler.WaitWithContext(ctx)

if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
}
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
}

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("differing loadbalancer %s", diff)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("differing loadbalancer %s", diff)
}
})
})
}
}
Expand Down Expand Up @@ -181,24 +181,24 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
client := &apiClientLoadbalancerMocked{
responses: tt.responses,
}
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
_, err := handler.SetTimeout(1 * time.Second).
SetThrottle(250 * time.Millisecond).
WaitWithContext(ctx)
synctest.Test(t, func(t *testing.T) {
ctx := context.Background()
client := &apiClientLoadbalancerMocked{
responses: tt.responses,
}
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
_, err := handler.WaitWithContext(ctx)

if tt.wantErr != (err != nil) {
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
}
if tt.wantErr {
var apiErr *oapierror.GenericOpenAPIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected openapi error, got %v", err)
if tt.wantErr != (err != nil) {
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
}
if tt.wantErr {
var apiErr *oapierror.GenericOpenAPIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected openapi error, got %v", err)
}
}
}
})
})
}
}
71 changes: 41 additions & 30 deletions services/cdn/v1api/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"testing"
"testing/synctest"
"time"

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

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
handler := CreateDistributionPoolWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
}
synctest.Test(t, func(t *testing.T) {
handler := CreateDistributionPoolWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
}
})
})
}
}
Expand Down Expand Up @@ -287,12 +290,14 @@ func TestDeleteDistributionWaitHandler(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
handler := DeleteDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if dist != nil {
t.Fatalf("Distribution not deleted")
}
synctest.Test(t, func(t *testing.T) {
handler := DeleteDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if dist != nil {
t.Fatalf("Distribution not deleted")
}
})
})
}
}
Expand Down Expand Up @@ -402,12 +407,14 @@ func TestUpdateDistributionWaitHandler(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
handler := UpdateDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
}
synctest.Test(t, func(t *testing.T) {
handler := UpdateDistributionWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId)
dist, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if diff := cmp.Diff(tc.expectedDistribution, dist, cmpopts.IgnoreUnexported(cdn.NullableString{}, cdn.NullableInt64{})); diff != "" {
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
}
})
})
}
}
Expand Down Expand Up @@ -519,12 +526,14 @@ func TestCreateCustomDomainWaitHandler(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
handler := CreateCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if diff := cmp.Diff(tc.expectedCustomDomain, customDomain); diff != "" {
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
}
synctest.Test(t, func(t *testing.T) {
handler := CreateCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if diff := cmp.Diff(tc.expectedCustomDomain, customDomain); diff != "" {
t.Fatalf("Unexpected response (-want, +got):\n%s", diff)
}
})
})
}
}
Expand Down Expand Up @@ -640,12 +649,14 @@ func TestDeleteCustomDomainHandler(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
handler := DeleteCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if customDomain != nil {
t.Fatalf("Custom domain not deleted")
}
synctest.Test(t, func(t *testing.T) {
handler := DeleteCDNCustomDomainWaitHandler(context.Background(), tc.apiClient, tc.projectId, tc.distributionId, tc.customDomain)
customDomain, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
tc.errCheck(t, err)
if customDomain != nil {
t.Fatalf("Custom domain not deleted")
}
})
})
}
}
Loading
Loading