|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package yarpc |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/uber/submitqueue/platform/errs" |
| 24 | + "go.uber.org/yarpc/yarpcerrors" |
| 25 | +) |
| 26 | + |
| 27 | +type customYARPCError struct { |
| 28 | + status *yarpcerrors.Status |
| 29 | +} |
| 30 | + |
| 31 | +func (e customYARPCError) Error() string { |
| 32 | + return e.status.Error() |
| 33 | +} |
| 34 | + |
| 35 | +func (e customYARPCError) YARPCError() *yarpcerrors.Status { |
| 36 | + return e.status |
| 37 | +} |
| 38 | + |
| 39 | +func TestClassifier_StatusCodes(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + code yarpcerrors.Code |
| 43 | + want errs.Verdict |
| 44 | + }{ |
| 45 | + {name: "cancelled", code: yarpcerrors.CodeCancelled, want: errs.InfraRetryable}, |
| 46 | + {name: "unknown", code: yarpcerrors.CodeUnknown, want: errs.InfraDependencyRetryable}, |
| 47 | + {name: "deadline exceeded", code: yarpcerrors.CodeDeadlineExceeded, want: errs.InfraDependencyRetryable}, |
| 48 | + {name: "resource exhausted", code: yarpcerrors.CodeResourceExhausted, want: errs.InfraDependencyRetryable}, |
| 49 | + {name: "aborted", code: yarpcerrors.CodeAborted, want: errs.InfraDependencyRetryable}, |
| 50 | + {name: "internal", code: yarpcerrors.CodeInternal, want: errs.InfraDependencyRetryable}, |
| 51 | + {name: "unavailable", code: yarpcerrors.CodeUnavailable, want: errs.InfraDependencyRetryable}, |
| 52 | + {name: "invalid argument", code: yarpcerrors.CodeInvalidArgument, want: errs.InfraDependency}, |
| 53 | + {name: "not found", code: yarpcerrors.CodeNotFound, want: errs.InfraDependency}, |
| 54 | + {name: "already exists", code: yarpcerrors.CodeAlreadyExists, want: errs.InfraDependency}, |
| 55 | + {name: "permission denied", code: yarpcerrors.CodePermissionDenied, want: errs.InfraDependency}, |
| 56 | + {name: "failed precondition", code: yarpcerrors.CodeFailedPrecondition, want: errs.InfraDependency}, |
| 57 | + {name: "out of range", code: yarpcerrors.CodeOutOfRange, want: errs.InfraDependency}, |
| 58 | + {name: "unimplemented", code: yarpcerrors.CodeUnimplemented, want: errs.InfraDependency}, |
| 59 | + {name: "data loss", code: yarpcerrors.CodeDataLoss, want: errs.InfraDependency}, |
| 60 | + {name: "unauthenticated", code: yarpcerrors.CodeUnauthenticated, want: errs.InfraDependency}, |
| 61 | + {name: "ok", code: yarpcerrors.CodeOK, want: errs.Unknown}, |
| 62 | + {name: "unrecognized code", code: yarpcerrors.Code(99), want: errs.Unknown}, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + assert.Equal(t, tt.want, Classifier.Classify(yarpcerrors.Newf(tt.code, "rpc failed"))) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestClassifier_Unknown(t *testing.T) { |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + err error |
| 76 | + }{ |
| 77 | + {name: "wrapped status", err: fmt.Errorf("call failed: %w", yarpcerrors.DeadlineExceededErrorf("late"))}, |
| 78 | + {name: "plain error", err: errors.New("anything")}, |
| 79 | + {name: "nil", err: nil}, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + assert.Equal(t, errs.Unknown, Classifier.Classify(tt.err)) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestClassifier_TransportSpecificYARPCError(t *testing.T) { |
| 90 | + err := customYARPCError{status: yarpcerrors.Newf(yarpcerrors.CodeUnavailable, "down")} |
| 91 | + assert.Equal(t, errs.InfraDependencyRetryable, Classifier.Classify(err)) |
| 92 | +} |
| 93 | + |
| 94 | +func TestClassifier_AppliedViaProcessor(t *testing.T) { |
| 95 | + processor := errs.NewClassifierProcessor(Classifier) |
| 96 | + |
| 97 | + t.Run("wrapped deadline is a retryable dependency error", func(t *testing.T) { |
| 98 | + err := fmt.Errorf("set ref: %w", yarpcerrors.DeadlineExceededErrorf("context deadline exceeded")) |
| 99 | + out := processor.Process(err) |
| 100 | + assert.True(t, errs.IsRetryable(out)) |
| 101 | + assert.True(t, errs.IsDependencyError(out)) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("wrapped invalid argument is a non-retryable dependency error", func(t *testing.T) { |
| 105 | + err := fmt.Errorf("set ref: %w", yarpcerrors.InvalidArgumentErrorf("bad ref")) |
| 106 | + out := processor.Process(err) |
| 107 | + assert.False(t, errs.IsRetryable(out)) |
| 108 | + assert.True(t, errs.IsDependencyError(out)) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("cancelled is retryable without dependency attribution", func(t *testing.T) { |
| 112 | + out := processor.Process(yarpcerrors.CancelledErrorf("caller cancelled")) |
| 113 | + assert.True(t, errs.IsRetryable(out)) |
| 114 | + assert.False(t, errs.IsDependencyError(out)) |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("a controller verdict wins over the classifier", func(t *testing.T) { |
| 118 | + err := errs.NewDependencyError(yarpcerrors.UnavailableErrorf("down")) |
| 119 | + out := processor.Process(err) |
| 120 | + assert.Same(t, err, out) |
| 121 | + assert.False(t, errs.IsRetryable(out)) |
| 122 | + }) |
| 123 | +} |
0 commit comments