Skip to content

Commit

Permalink
chore: resolve race condition in test assertion (#585)
Browse files Browse the repository at this point in the history
In some cases, the refresh ahead cache may have already begun a refresh
cycle before the test calls Close on the instance. When this happens,
the returned error is not a context.Canceled error, but instead a
DialError.

This commit updates the TestClose method to check that either error is
returned. The point of the test is to ensure close results in an error
when the next call to ConnectionInfo is made. Which error is less
important.

Fixes #444
  • Loading branch information
enocom authored Jun 4, 2024
1 parent ed2a8ed commit 28839d9
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions internal/alloydb/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ func testInstanceURI() InstanceURI {

func TestConnectInfoErrors(t *testing.T) {
ctx := context.Background()
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(ctx, option.WithTokenSource(stubTokenSource{}))
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(
ctx, option.WithTokenSource(stubTokenSource{}),
)
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
Expand All @@ -222,7 +224,16 @@ func TestConnectInfoErrors(t *testing.T) {

func TestClose(t *testing.T) {
ctx := context.Background()
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(ctx, option.WithTokenSource(stubTokenSource{}))
inst := mock.NewFakeInstance(
"my-project", "my-region", "my-cluster", "my-instance",
)
mc, url, _ := mock.HTTPClient(
mock.InstanceGetSuccess(inst, 1),
mock.CreateEphemeralSuccess(inst, 1),
)
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(
ctx, option.WithHTTPClient(mc), option.WithEndpoint(url),
)
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
Expand All @@ -239,7 +250,9 @@ func TestClose(t *testing.T) {
i.Close()

_, err = i.ConnectionInfo(ctx)
if !errors.Is(err, context.Canceled) {

dErr := &errtype.DialError{}
if !errors.Is(err, context.Canceled) && !errors.As(err, &dErr) {
t.Fatalf("failed to retrieve connect info: %v", err)
}
}
Expand Down

0 comments on commit 28839d9

Please sign in to comment.