1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package internal
16
17 import (
18 "context"
19 "errors"
20 "testing"
21 "time"
22
23 gax "github.com/googleapis/gax-go/v2"
24 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
26 )
27
28 func TestRetry(t *testing.T) {
29 ctx := context.Background()
30
31
32 n := 0
33 endRetry := errors.New("end retry")
34 err := retry(ctx, gax.Backoff{},
35 func() (bool, error) {
36 n++
37 if n < 10 {
38 return false, nil
39 }
40 return true, endRetry
41 },
42 func(context.Context, time.Duration) error { return nil })
43 if got, want := err, endRetry; got != want {
44 t.Errorf("got %v, want %v", err, endRetry)
45 }
46 if n != 10 {
47 t.Errorf("n: got %d, want %d", n, 10)
48 }
49
50
51
52 n = 0
53 err = retry(ctx, gax.Backoff{},
54 func() (bool, error) { return false, nil },
55 func(context.Context, time.Duration) error {
56 n++
57 if n < 10 {
58 return nil
59 }
60 return context.DeadlineExceeded
61 })
62 if err == nil {
63 t.Error("got nil, want error")
64 }
65 }
66
67 func TestRetryPreserveError(t *testing.T) {
68
69
70 err := retry(context.Background(), gax.Backoff{},
71 func() (bool, error) {
72 return false, status.Error(codes.NotFound, "not found")
73 },
74 func(context.Context, time.Duration) error {
75 return context.DeadlineExceeded
76 })
77 if err == nil {
78 t.Fatalf("unexpectedly got nil error")
79 }
80 wantError := "retry failed with context deadline exceeded; last error: rpc error: code = NotFound desc = not found"
81 if g, w := err.Error(), wantError; g != w {
82 t.Errorf("got error %q, want %q", g, w)
83 }
84 got, ok := status.FromError(err)
85 if !ok {
86 t.Fatalf("got %T, wanted a status", got)
87 }
88 if g, w := got.Code(), codes.NotFound; g != w {
89 t.Errorf("got code %v, want %v", g, w)
90 }
91 wantMessage := "retry failed with context deadline exceeded; last error: rpc error: code = NotFound desc = not found"
92 if g, w := got.Message(), wantMessage; g != w {
93 t.Errorf("got message %q, want %q", g, w)
94 }
95 }
96
97 func TestRetryWrapsErrorWithStatusUnknown(t *testing.T) {
98
99
100 err := retry(context.Background(), gax.Backoff{},
101 func() (bool, error) {
102 return false, errors.New("test error")
103 },
104 func(context.Context, time.Duration) error {
105 return context.DeadlineExceeded
106 })
107 if err == nil {
108 t.Fatalf("unexpectedly got nil error")
109 }
110 wantError := "retry failed with context deadline exceeded; last error: test error"
111 if g, w := err.Error(), wantError; g != w {
112 t.Errorf("got error %q, want %q", g, w)
113 }
114 got, _ := status.FromError(err)
115 if g, w := got.Code(), codes.Unknown; g != w {
116 t.Errorf("got code %v, want %v", g, w)
117 }
118 }
119
View as plain text