Skip to content

Commit 08cf305

Browse files
authored
chore(pubsub): switch to using errors.Is (#11114)
1 parent 1ce4b6d commit 08cf305

File tree

10 files changed

+23
-18
lines changed

10 files changed

+23
-18
lines changed

pubsub/example_subscription_iterator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package pubsub_test
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021

2122
"cloud.google.com/go/pubsub"
@@ -43,7 +44,7 @@ func ExampleSubscriptionIterator_Next() {
4344
it := client.Subscriptions(ctx)
4445
for {
4546
sub, err := it.Next()
46-
if err == iterator.Done {
47+
if errors.Is(err, iterator.Done) {
4748
break
4849
}
4950
if err != nil {

pubsub/example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func ExampleTopic_Subscriptions() {
200200
// List all subscriptions of the topic (maybe of multiple projects).
201201
for subs := topic.Subscriptions(ctx); ; {
202202
sub, err := subs.Next()
203-
if err == iterator.Done {
203+
if errors.Is(err, iterator.Done) {
204204
break
205205
}
206206
if err != nil {
@@ -464,7 +464,7 @@ func ExampleSnapshotConfigIterator_Next() {
464464
iter := client.Snapshots(ctx)
465465
for {
466466
snapConfig, err := iter.Next()
467-
if err == iterator.Done {
467+
if errors.Is(err, iterator.Done) {
468468
break
469469
}
470470
if err != nil {

pubsub/example_topic_iterator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package pubsub_test
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021

2122
"cloud.google.com/go/pubsub"
@@ -42,7 +43,7 @@ func ExampleTopicIterator_Next() {
4243
it := client.Topics(ctx)
4344
for {
4445
t, err := it.Next()
45-
if err == iterator.Done {
46+
if errors.Is(err, iterator.Done) {
4647
break
4748
}
4849
if err != nil {

pubsub/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestIntegration_Admin(t *testing.T) {
184184
if err == nil && s.name == snap.name {
185185
return true, nil
186186
}
187-
if err == iterator.Done {
187+
if errors.Is(err, iterator.Done) {
188188
return false, fmt.Errorf("cannot find snapshot: %q", snap.name)
189189
}
190190
if err != nil {

pubsub/internal/longtest/endtoend_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func cleanupTopic(ctx context.Context, client *pubsub.Client) error {
383383
it := client.Topics(ctx)
384384
for {
385385
t, err := it.Next()
386-
if err == iterator.Done {
386+
if errors.Is(err, iterator.Done) {
387387
break
388388
}
389389
if err != nil {
@@ -424,7 +424,7 @@ func cleanupSubscription(ctx context.Context, client *pubsub.Client) error {
424424
it := client.Subscriptions(ctx)
425425
for {
426426
s, err := it.Next()
427-
if err == iterator.Done {
427+
if errors.Is(err, iterator.Done) {
428428
break
429429
}
430430
if err != nil {

pubsub/iterator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (it *messageIterator) receive(maxToPull int32) ([]*Message, error) {
279279
// If the cancellation comes from the underlying grpc client getting closed,
280280
// do propagate the cancellation error.
281281
// See https://github.com/googleapis/google-cloud-go/pull/10153#discussion_r1600814775
282-
if err != nil && it.ps.ctx.Err() == context.Canceled {
282+
if err != nil && errors.Is(it.ps.ctx.Err(), context.Canceled) {
283283
err = io.EOF
284284
}
285285
}
@@ -406,7 +406,7 @@ func (it *messageIterator) pullMessages(maxToPull int32) ([]*pb.ReceivedMessage,
406406
MaxMessages: maxToPull,
407407
}, gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(maxSendRecvBytes)))
408408
switch {
409-
case err == context.Canceled:
409+
case errors.Is(err, context.Canceled):
410410
return nil, nil
411411
case status.Code(err) == codes.Canceled:
412412
return nil, nil

pubsub/mock_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package pubsub
1818

1919
import (
2020
"context"
21+
"errors"
2122
"io"
2223
"sync"
2324
"time"
@@ -128,7 +129,7 @@ func (s *mockServer) StreamingPull(stream pb.Subscriber_StreamingPullServer) err
128129
s.mu.Unlock()
129130
// Nothing to send, so wait for the client to shut down the stream.
130131
err := <-errc // a real error, or at least EOF
131-
if err == io.EOF {
132+
if errors.Is(err, io.EOF) {
132133
return nil
133134
}
134135
return err
@@ -142,7 +143,7 @@ func (s *mockServer) StreamingPull(stream pb.Subscriber_StreamingPullServer) err
142143
// This reduces flakiness of tests involving retry.
143144
time.Sleep(200 * time.Millisecond)
144145
}
145-
if pr.err == io.EOF {
146+
if errors.Is(pr.err, io.EOF) {
146147
return nil
147148
}
148149
if pr.err != nil {

pubsub/pstest/fake.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package pstest
2424

2525
import (
2626
"context"
27+
"errors"
2728
"fmt"
2829
"io"
2930
"math/rand"
@@ -1381,7 +1382,7 @@ func (st *stream) pull(wg *sync.WaitGroup) error {
13811382
var err error
13821383
select {
13831384
case err = <-errc:
1384-
if err == io.EOF {
1385+
if errors.Is(err, io.EOF) {
13851386
err = nil
13861387
}
13871388
case <-tchan:

pubsub/pstest/fake_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package pstest
1717
import (
1818
"bytes"
1919
"context"
20+
"errors"
2021
"fmt"
2122
"io"
2223
"math/rand"
@@ -175,7 +176,7 @@ func TestTopics(t *testing.T) {
175176
Topic: topics[1].Name,
176177
})
177178
expectedErr := status.Errorf(codes.FailedPrecondition, "topic %q used as deadLetter for %s", topics[1].Name, s.Name)
178-
if err == nil || err.Error() != expectedErr.Error() {
179+
if err == nil || !errors.Is(err, expectedErr) {
179180
t.Fatalf("returned a different error than the expected one. \nReceived '%s'; \nExpected: '%s'", err, expectedErr)
180181
}
181182
})
@@ -261,8 +262,8 @@ func TestSubscriptions(t *testing.T) {
261262
},
262263
})
263264
expectedErr := status.Errorf(codes.NotFound, "deadLetter topic \"projects/P/topics/nonexisting\"")
264-
if err == nil || err.Error() != expectedErr.Error() {
265-
t.Fatalf("expected subscription creation to fail with a specific err but it didn't. \nError: %s \nExepcted err: %s", err, expectedErr)
265+
if err == nil || !errors.Is(err, expectedErr) {
266+
t.Fatalf("expected subscription creation to fail with a specific err but it didn't. \nError: %s \nExpected err: %s", err, expectedErr)
266267
}
267268
_, err = server.GServer.DeleteTopic(ctx, &pb.DeleteTopicRequest{
268269
Topic: topic.Name,
@@ -666,7 +667,7 @@ func TestStreamingPullAck(t *testing.T) {
666667

667668
for i := 0; i < 4; i++ {
668669
res, err := spc.Recv()
669-
if err == io.EOF {
670+
if errors.Is(err, io.EOF) {
670671
break
671672
}
672673
if err != nil {
@@ -797,7 +798,7 @@ func TestAckDeadline(t *testing.T) {
797798
})
798799
for {
799800
res, err := spc.Recv()
800-
if err == io.EOF {
801+
if errors.Is(err, io.EOF) {
801802
break
802803
}
803804
if err != nil {

pubsub/subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ func (s *Subscription) Receive(ctx context.Context, f func(context.Context, *Mes
13781378
}
13791379

13801380
msgs, err := iter.receive(maxToPull)
1381-
if err == io.EOF {
1381+
if errors.Is(err, io.EOF) {
13821382
return nil
13831383
}
13841384
if err != nil {

0 commit comments

Comments
 (0)