Skip to content

Commit

Permalink
chore(pubsub): switch to using errors.Is (#11114)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongalex authored Nov 12, 2024
1 parent 1ce4b6d commit 08cf305
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 18 deletions.
3 changes: 2 additions & 1 deletion pubsub/example_subscription_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pubsub_test

import (
"context"
"errors"
"fmt"

"cloud.google.com/go/pubsub"
Expand Down Expand Up @@ -43,7 +44,7 @@ func ExampleSubscriptionIterator_Next() {
it := client.Subscriptions(ctx)
for {
sub, err := it.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pubsub/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func ExampleTopic_Subscriptions() {
// List all subscriptions of the topic (maybe of multiple projects).
for subs := topic.Subscriptions(ctx); ; {
sub, err := subs.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down Expand Up @@ -464,7 +464,7 @@ func ExampleSnapshotConfigIterator_Next() {
iter := client.Snapshots(ctx)
for {
snapConfig, err := iter.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pubsub/example_topic_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pubsub_test

import (
"context"
"errors"
"fmt"

"cloud.google.com/go/pubsub"
Expand All @@ -42,7 +43,7 @@ func ExampleTopicIterator_Next() {
it := client.Topics(ctx)
for {
t, err := it.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pubsub/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestIntegration_Admin(t *testing.T) {
if err == nil && s.name == snap.name {
return true, nil
}
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
return false, fmt.Errorf("cannot find snapshot: %q", snap.name)
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pubsub/internal/longtest/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func cleanupTopic(ctx context.Context, client *pubsub.Client) error {
it := client.Topics(ctx)
for {
t, err := it.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down Expand Up @@ -424,7 +424,7 @@ func cleanupSubscription(ctx context.Context, client *pubsub.Client) error {
it := client.Subscriptions(ctx)
for {
s, err := it.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pubsub/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (it *messageIterator) receive(maxToPull int32) ([]*Message, error) {
// If the cancellation comes from the underlying grpc client getting closed,
// do propagate the cancellation error.
// See https://github.com/googleapis/google-cloud-go/pull/10153#discussion_r1600814775
if err != nil && it.ps.ctx.Err() == context.Canceled {
if err != nil && errors.Is(it.ps.ctx.Err(), context.Canceled) {
err = io.EOF
}
}
Expand Down Expand Up @@ -406,7 +406,7 @@ func (it *messageIterator) pullMessages(maxToPull int32) ([]*pb.ReceivedMessage,
MaxMessages: maxToPull,
}, gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(maxSendRecvBytes)))
switch {
case err == context.Canceled:
case errors.Is(err, context.Canceled):
return nil, nil
case status.Code(err) == codes.Canceled:
return nil, nil
Expand Down
5 changes: 3 additions & 2 deletions pubsub/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package pubsub

import (
"context"
"errors"
"io"
"sync"
"time"
Expand Down Expand Up @@ -128,7 +129,7 @@ func (s *mockServer) StreamingPull(stream pb.Subscriber_StreamingPullServer) err
s.mu.Unlock()
// Nothing to send, so wait for the client to shut down the stream.
err := <-errc // a real error, or at least EOF
if err == io.EOF {
if errors.Is(err, io.EOF) {
return nil
}
return err
Expand All @@ -142,7 +143,7 @@ func (s *mockServer) StreamingPull(stream pb.Subscriber_StreamingPullServer) err
// This reduces flakiness of tests involving retry.
time.Sleep(200 * time.Millisecond)
}
if pr.err == io.EOF {
if errors.Is(pr.err, io.EOF) {
return nil
}
if pr.err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pubsub/pstest/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package pstest

import (
"context"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -1381,7 +1382,7 @@ func (st *stream) pull(wg *sync.WaitGroup) error {
var err error
select {
case err = <-errc:
if err == io.EOF {
if errors.Is(err, io.EOF) {
err = nil
}
case <-tchan:
Expand Down
11 changes: 6 additions & 5 deletions pubsub/pstest/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pstest
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -175,7 +176,7 @@ func TestTopics(t *testing.T) {
Topic: topics[1].Name,
})
expectedErr := status.Errorf(codes.FailedPrecondition, "topic %q used as deadLetter for %s", topics[1].Name, s.Name)
if err == nil || err.Error() != expectedErr.Error() {
if err == nil || !errors.Is(err, expectedErr) {
t.Fatalf("returned a different error than the expected one. \nReceived '%s'; \nExpected: '%s'", err, expectedErr)
}
})
Expand Down Expand Up @@ -261,8 +262,8 @@ func TestSubscriptions(t *testing.T) {
},
})
expectedErr := status.Errorf(codes.NotFound, "deadLetter topic \"projects/P/topics/nonexisting\"")
if err == nil || err.Error() != expectedErr.Error() {
t.Fatalf("expected subscription creation to fail with a specific err but it didn't. \nError: %s \nExepcted err: %s", err, expectedErr)
if err == nil || !errors.Is(err, expectedErr) {
t.Fatalf("expected subscription creation to fail with a specific err but it didn't. \nError: %s \nExpected err: %s", err, expectedErr)
}
_, err = server.GServer.DeleteTopic(ctx, &pb.DeleteTopicRequest{
Topic: topic.Name,
Expand Down Expand Up @@ -666,7 +667,7 @@ func TestStreamingPullAck(t *testing.T) {

for i := 0; i < 4; i++ {
res, err := spc.Recv()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down Expand Up @@ -797,7 +798,7 @@ func TestAckDeadline(t *testing.T) {
})
for {
res, err := spc.Recv()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pubsub/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ func (s *Subscription) Receive(ctx context.Context, f func(context.Context, *Mes
}

msgs, err := iter.receive(maxToPull)
if err == io.EOF {
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
Expand Down

0 comments on commit 08cf305

Please sign in to comment.