MCPcopy
hub / github.com/grpc/grpc-go / TestRetryUnary

Method TestRetryUnary

test/retry_test.go:47–109  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

45)
46
47func (s) TestRetryUnary(t *testing.T) {
48 serverMu := sync.Mutex{}
49 i := -1
50 ss := &stubserver.StubServer{
51 EmptyCallF: func(context.Context, *testpb.Empty) (r *testpb.Empty, err error) {
52 serverMu.Lock()
53 defer serverMu.Unlock()
54 defer func() { t.Logf("server call %v returning err %v", i, err) }()
55 i++
56 switch i {
57 case 0, 2, 5:
58 return &testpb.Empty{}, nil
59 case 6, 8, 11:
60 return nil, status.New(codes.Internal, "non-retryable error").Err()
61 }
62 return nil, status.New(codes.AlreadyExists, "retryable error").Err()
63 },
64 }
65 if err := ss.Start([]grpc.ServerOption{},
66 grpc.WithDefaultServiceConfig(`{
67 "methodConfig": [{
68 "name": [{"service": "grpc.testing.TestService"}],
69 "waitForReady": true,
70 "retryPolicy": {
71 "MaxAttempts": 4,
72 "InitialBackoff": ".01s",
73 "MaxBackoff": ".01s",
74 "BackoffMultiplier": 1.0,
75 "RetryableStatusCodes": [ "ALREADY_EXISTS" ]
76 }
77 }]}`)); err != nil {
78 t.Fatalf("Error starting endpoint server: %v", err)
79 }
80 defer ss.Stop()
81
82 testCases := []struct {
83 wantCode codes.Code
84 wantCount int
85 }{
86 {codes.OK, 0},
87 {codes.OK, 2},
88 {codes.OK, 5},
89 {codes.Internal, 6},
90 {codes.Internal, 8},
91 {codes.Internal, 11},
92 {codes.AlreadyExists, 15},
93 }
94 for num, tc := range testCases {
95 t.Log("Case", num)
96 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
97 _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{})
98 cancel()
99 if status.Code(err) != tc.wantCode {
100 t.Fatalf("EmptyCall(_, _) = _, %v; want _, <Code() = %v>", err, tc.wantCode)
101 }
102 serverMu.Lock()
103 if i != tc.wantCount {
104 serverMu.Unlock()

Callers

nothing calls this directly

Calls 12

StartMethod · 0.95
StopMethod · 0.95
NewFunction · 0.92
WithDefaultServiceConfigFunction · 0.92
CodeFunction · 0.92
ErrMethod · 0.80
LogfMethod · 0.65
FatalfMethod · 0.65
LogMethod · 0.65
EmptyCallMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected