Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
codyoss committed Dec 20, 2024
2 parents 7f61b96 + 946c3e8 commit ffeefea
Show file tree
Hide file tree
Showing 16 changed files with 713 additions and 351 deletions.
2 changes: 1 addition & 1 deletion firestore/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module cloud.google.com/go/firestore
go 1.21

require (
cloud.google.com/go v0.116.0
cloud.google.com/go v0.117.0
cloud.google.com/go/longrunning v0.6.2
github.com/google/go-cmp v0.6.0
github.com/googleapis/gax-go/v2 v2.14.0
Expand Down
4 changes: 2 additions & 2 deletions firestore/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.116.0 h1:B3fRrSDkLRt5qSHWe40ERJvhvnQwdZiHu0bJOpldweE=
cloud.google.com/go v0.116.0/go.mod h1:cEPSRWPzZEswwdr9BxE6ChEn01dWlTaF05LiC2Xs70U=
cloud.google.com/go v0.117.0 h1:Z5TNFfQxj7WG2FgOGX1ekC5RiXrYgms6QscOm32M/4s=
cloud.google.com/go v0.117.0/go.mod h1:ZbwhVTb1DBGt2Iwb3tNO6SEK4q+cplHZmLWH+DelYYc=
cloud.google.com/go/auth v0.13.0 h1:8Fu8TZy167JkW8Tj3q7dIkr2v4cndv41ouecJx0PAHs=
cloud.google.com/go/auth v0.13.0/go.mod h1:COOjD9gwfKNKz+IIduatIhYJQIc0mG3H102r/EMxX6Q=
cloud.google.com/go/auth/oauth2adapt v0.2.6 h1:V6a6XDu2lTwPZWOawrAa9HUK+DB2zfJyTuciBG5hFkU=
Expand Down
132 changes: 82 additions & 50 deletions firestore/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
envPrivateKey = "GCLOUD_TESTS_GOLANG_FIRESTORE_KEY"
envDatabases = "GCLOUD_TESTS_GOLANG_FIRESTORE_DATABASES"
envEmulator = "FIRESTORE_EMULATOR_HOST"
indexBuilding = "index is currently building"
)

var (
Expand Down Expand Up @@ -260,12 +261,14 @@ func handleCreateIndexResp(ctx context.Context, indexNames []string, wg *sync.Wa
// deleteIndexes deletes composite indexes created in createIndexes function
func deleteIndexes(ctx context.Context, indexNames []string) {
for _, indexName := range indexNames {
err := iAdminClient.DeleteIndex(ctx, &adminpb.DeleteIndexRequest{
Name: indexName,
testutil.RetryWithoutTest(5, 5*time.Second, func(r *testutil.R) {
err := iAdminClient.DeleteIndex(ctx, &adminpb.DeleteIndexRequest{
Name: indexName,
})
if err != nil {
r.Errorf("Failed to delete index \"%s\": %+v\n", indexName, err)
}
})
if err != nil {
log.Printf("Failed to delete index \"%s\": %+v\n", indexName, err)
}
}
}

Expand Down Expand Up @@ -2437,7 +2440,7 @@ func TestDetectProjectID(t *testing.T) {
ts := testutil.ErroringTokenSource{}
// Try to use creds without project ID.
_, err := NewClient(ctx, DetectProjectID, option.WithTokenSource(ts))
if err == nil || err.Error() != "firestore: see the docs on DetectProjectID" {
if err == nil || err.Error() != "unable to detect projectID, please refer to docs for DetectProjectID" {
t.Errorf("expected an error while using TokenSource that does not have a project ID")
}
}
Expand Down Expand Up @@ -2931,28 +2934,39 @@ func TestIntegration_AggregationQueries(t *testing.T) {
}

for _, tc := range testcases {
var aggResult AggregationResult
var err error
if tc.runInTransaction {
client.RunTransaction(ctx, func(ctx context.Context, tx *Transaction) error {
aggResult, err = tc.aggregationQuery.Transaction(tx).Get(ctx)
return err
t.Run(tc.desc, func(t *testing.T) {
testutil.Retry(t, 5, 5*time.Second, func(r *testutil.R) {
var aggResult AggregationResult
var err error
if tc.runInTransaction {
client.RunTransaction(ctx, func(ctx context.Context, tx *Transaction) error {
aggResult, err = tc.aggregationQuery.Transaction(tx).Get(ctx)
return err
})
} else {
aggResult, err = tc.aggregationQuery.Get(ctx)
}

// Retry only if index building is in progress
s, ok := status.FromError(err)
if err != nil && ok && s != nil && s.Code() != codes.FailedPrecondition &&
strings.Contains(s.Message(), indexBuilding) {
r.Errorf("Get: %v", err)
return
}

// Compare expected and actual results
if err != nil && !tc.wantErr {
r.Fatalf("got: %v, want: nil", err)
}
if err == nil && tc.wantErr {
r.Fatalf("got: %v, wanted error", err)
}
if !reflect.DeepEqual(aggResult, tc.result) {
r.Fatalf("got: %v, want: %v", aggResult, tc.result)
}
})
} else {
aggResult, err = tc.aggregationQuery.Get(ctx)
}
if err != nil && !tc.wantErr {
t.Errorf("%s: got: %v, want: nil", tc.desc, err)
continue
}
if err == nil && tc.wantErr {
t.Errorf("%s: got: %v, wanted error", tc.desc, err)
continue
}
if !reflect.DeepEqual(aggResult, tc.result) {
t.Errorf("%s: got: %v, want: %v", tc.desc, aggResult, tc.result)
continue
}
})
}
}

Expand Down Expand Up @@ -3313,33 +3327,51 @@ func TestIntegration_FindNearest(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
iter := tc.vq.Documents(ctx)
gotDocs, err := iter.GetAll()
if err != nil {
t.Fatalf("GetAll: %+v", err)
}

if len(gotDocs) != len(tc.wantBeans) {
t.Fatalf("Expected %v results, got %d", len(tc.wantBeans), len(gotDocs))
}

for i, doc := range gotDocs {
var gotBean coffeeBean
if len(tc.wantResField) != 0 {
_, ok := doc.Data()[tc.wantResField]
if !ok {
t.Errorf("Expected %v field to exist in %v", tc.wantResField, doc.Data())
}
testutil.Retry(t, 5, 5*time.Second, func(r *testutil.R) {
// Get all documents
iter := tc.vq.Documents(ctx)
gotDocs, err := iter.GetAll()

// Retry only if index building is in progress
s, ok := status.FromError(err)
if err != nil && ok && s != nil && s.Code() != codes.FailedPrecondition &&
strings.Contains(s.Message(), indexBuilding) {
r.Errorf("GetAll: %v", err)
return
}
err := doc.DataTo(&gotBean)

if err != nil {
t.Errorf("#%v: DataTo: %+v", doc.Ref.ID, err)
continue
t.Fatalf("GetAll: %+v", err)
}

// Compare expected and actual results length
if len(gotDocs) != len(tc.wantBeans) {
t.Fatalf("Expected %v results, got %d", len(tc.wantBeans), len(gotDocs))
}
if tc.wantBeans[i].ID != gotBean.ID {
t.Errorf("#%v: want: %v, got: %v", i, beans[i].ID, gotBean.ID)

// Compare results
for i, doc := range gotDocs {
var gotBean coffeeBean

// Compare expected and actual result field
if len(tc.wantResField) != 0 {
_, ok := doc.Data()[tc.wantResField]
if !ok {
t.Errorf("Expected %v field to exist in %v", tc.wantResField, doc.Data())
}
}

// Compare expected and actual document ID
err := doc.DataTo(&gotBean)
if err != nil {
t.Errorf("#%v: DataTo: %+v", doc.Ref.ID, err)
continue
}
if tc.wantBeans[i].ID != gotBean.ID {
t.Errorf("#%v: want: %v, got: %v", i, beans[i].ID, gotBean.ID)
}
}
}
})
})
}
}
2 changes: 1 addition & 1 deletion internal/generated/snippets/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.13
toolchain go1.23.0

require (
cloud.google.com/go v0.116.0
cloud.google.com/go v0.117.0
cloud.google.com/go/accessapproval v1.8.2
cloud.google.com/go/accesscontextmanager v1.9.2
cloud.google.com/go/advisorynotifications v0.0.0-00010101000000-000000000000
Expand Down
2 changes: 1 addition & 1 deletion logging/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module cloud.google.com/go/logging
go 1.21

require (
cloud.google.com/go v0.116.0
cloud.google.com/go v0.117.0
cloud.google.com/go/compute/metadata v0.6.0
cloud.google.com/go/iam v1.2.2
cloud.google.com/go/longrunning v0.6.2
Expand Down
4 changes: 2 additions & 2 deletions logging/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.116.0 h1:B3fRrSDkLRt5qSHWe40ERJvhvnQwdZiHu0bJOpldweE=
cloud.google.com/go v0.116.0/go.mod h1:cEPSRWPzZEswwdr9BxE6ChEn01dWlTaF05LiC2Xs70U=
cloud.google.com/go v0.117.0 h1:Z5TNFfQxj7WG2FgOGX1ekC5RiXrYgms6QscOm32M/4s=
cloud.google.com/go v0.117.0/go.mod h1:ZbwhVTb1DBGt2Iwb3tNO6SEK4q+cplHZmLWH+DelYYc=
cloud.google.com/go/auth v0.13.0 h1:8Fu8TZy167JkW8Tj3q7dIkr2v4cndv41ouecJx0PAHs=
cloud.google.com/go/auth v0.13.0/go.mod h1:COOjD9gwfKNKz+IIduatIhYJQIc0mG3H102r/EMxX6Q=
cloud.google.com/go/auth/oauth2adapt v0.2.6 h1:V6a6XDu2lTwPZWOawrAa9HUK+DB2zfJyTuciBG5hFkU=
Expand Down
110 changes: 110 additions & 0 deletions logging/internal/testing/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testing

import (
"testing"
"time"

"cloud.google.com/go/internal/testutil"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var defaultMaxAttempts = 10
var defaultSleep = 10 * time.Second
var defaultRetryableCodes = map[codes.Code]bool{
codes.Unavailable: true,
}

// Iterator is a wrapper interface type for iterators in the logadmin
// library that have a Next function that gets the next item/error, or returns
// nil/iterator.Done if the object has no next item.
type Iterator[T any] interface {
Next() (*T, error)
}

// handleError handles the given error for the retry attempt.
func handleError(r *testutil.R, err error) {
if err != nil {
s, ok := status.FromError(err)

// Throw a fatal error if the error is not retryable or if it cannot be converted into
// a status object.
if ok && !defaultRetryableCodes[s.Code()] {
r.Fatalf("%+v\n", err)
} else if ok {
r.Errorf("%+v\n", err)
} else {
r.Fatalf("%+v\n", err)
}
}
}

// Retry is a wrapper around testutil.Retry that retries the test function on Unavailable errors, otherwise, Fatalfs.
func Retry(t *testing.T, f func(r *testutil.R) error) bool {
retryFunc := func(r *testutil.R) {
err := f(r)
handleError(r, err)
}
return testutil.Retry(t, defaultMaxAttempts, defaultSleep, retryFunc)
}

// RetryAndExpectError retries the test function on Unavailable errors, otherwise passes
// if a different error was thrown. If no non-retryable error is returned, fails.
func RetryAndExpectError(t *testing.T, f func(r *testutil.R) error) bool {
retryFunc := func(r *testutil.R) {
err := f(r)

if err != nil {
s, ok := status.FromError(err)

// Only retry on retryable errors, otherwise pass.
if ok && defaultRetryableCodes[s.Code()] {
r.Errorf("%+v\n", err)
}
} else {
r.Fatalf("got no error, expected one")
}
}

return testutil.Retry(t, defaultMaxAttempts, defaultSleep, retryFunc)
}

// RetryIteratorNext is a wrapper around testutil.Retry that retries the given iterator's Next function
// and returns the next object, retrying if a retryable error is found. If a non-retryable error is found, fail
// the test.
func RetryIteratorNext[T any](t *testing.T, it Iterator[T]) (*T, bool) {
var next *T
var err error
retryFunc := func(r *testutil.R) {
next, err = it.Next()
if err != nil {
if err == iterator.Done {
return
}

handleError(r, err)
}
}
testutil.Retry(t, defaultMaxAttempts, defaultSleep, retryFunc)
if err == iterator.Done {
return nil, true
}
return next, false
}
Loading

0 comments on commit ffeefea

Please sign in to comment.