|
| 1 | +package conn |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + grpcCodes "google.golang.org/grpc/codes" |
| 9 | + grpcStatus "google.golang.org/grpc/status" |
| 10 | + |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" |
| 12 | +) |
| 13 | + |
| 14 | +func TestIsBadConn(t *testing.T) { |
| 15 | + for i, tt := range []struct { |
| 16 | + err error |
| 17 | + goodConnCodes []grpcCodes.Code |
| 18 | + badConn bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + err: fmt.Errorf("test"), |
| 22 | + badConn: false, |
| 23 | + }, |
| 24 | + { |
| 25 | + err: xerrors.Operation(), |
| 26 | + badConn: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.ResourceExhausted, "")), |
| 30 | + badConn: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Unavailable, "")), |
| 34 | + badConn: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.OK, "")), |
| 38 | + badConn: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Canceled, "")), |
| 42 | + badConn: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Unknown, "")), |
| 46 | + badConn: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.InvalidArgument, "")), |
| 50 | + badConn: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.DeadlineExceeded, "")), |
| 54 | + badConn: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.NotFound, "")), |
| 58 | + badConn: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.AlreadyExists, "")), |
| 62 | + badConn: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.PermissionDenied, "")), |
| 66 | + badConn: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.FailedPrecondition, "")), |
| 70 | + badConn: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Aborted, "")), |
| 74 | + badConn: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.OutOfRange, "")), |
| 78 | + badConn: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Unimplemented, "")), |
| 82 | + badConn: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Internal, "")), |
| 86 | + badConn: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.DataLoss, "")), |
| 90 | + badConn: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Unauthenticated, "")), |
| 94 | + badConn: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + err: xerrors.Transport(grpcStatus.Error(grpcCodes.Unauthenticated, "")), |
| 98 | + goodConnCodes: []grpcCodes.Code{ |
| 99 | + grpcCodes.Unauthenticated, |
| 100 | + }, |
| 101 | + badConn: false, |
| 102 | + }, |
| 103 | + } { |
| 104 | + t.Run(fmt.Sprintf("%d. %v", i, tt.err), func(t *testing.T) { |
| 105 | + require.Equal(t, tt.badConn, IsBadConn(tt.err, tt.goodConnCodes...)) |
| 106 | + require.Equal(t, tt.badConn, IsBadConn(xerrors.WithStackTrace(tt.err), tt.goodConnCodes...)) |
| 107 | + require.Equal(t, tt.badConn, IsBadConn(xerrors.Retryable(tt.err), tt.goodConnCodes...)) |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments