github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xsql/badconn/badconn_test.go (about) 1 package badconn 2 3 import ( 4 "context" 5 "database/sql/driver" 6 "errors" 7 "fmt" 8 "io" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" 13 "google.golang.org/grpc" 14 grpcCodes "google.golang.org/grpc/codes" 15 grpcStatus "google.golang.org/grpc/status" 16 17 "github.com/ydb-platform/ydb-go-sdk/v3/internal/backoff" 18 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 19 ) 20 21 var errsToCheck = []error{ 22 fmt.Errorf("unknown error"), 23 context.DeadlineExceeded, 24 context.Canceled, 25 xerrors.Transport( 26 //nolint:staticcheck 27 // ignore SA1019 28 //nolint:nolintlint 29 grpc.ErrClientConnClosing, 30 ), 31 xerrors.Transport(grpcStatus.Error(grpcCodes.Canceled, "")), 32 xerrors.Transport(grpcStatus.Error(grpcCodes.Unknown, "")), 33 xerrors.Transport(grpcStatus.Error(grpcCodes.InvalidArgument, "")), 34 xerrors.Transport(grpcStatus.Error(grpcCodes.DeadlineExceeded, "")), 35 xerrors.Transport(grpcStatus.Error(grpcCodes.NotFound, "")), 36 xerrors.Transport(grpcStatus.Error(grpcCodes.AlreadyExists, "")), 37 xerrors.Transport(grpcStatus.Error(grpcCodes.PermissionDenied, "")), 38 xerrors.Transport(grpcStatus.Error(grpcCodes.ResourceExhausted, "")), 39 xerrors.Transport(grpcStatus.Error(grpcCodes.FailedPrecondition, "")), 40 xerrors.Transport(grpcStatus.Error(grpcCodes.Aborted, "")), 41 xerrors.Transport(grpcStatus.Error(grpcCodes.OutOfRange, "")), 42 xerrors.Transport(grpcStatus.Error(grpcCodes.Unimplemented, "")), 43 xerrors.Transport(grpcStatus.Error(grpcCodes.Internal, "")), 44 xerrors.Transport(grpcStatus.Error(grpcCodes.Unavailable, "")), 45 xerrors.Retryable( 46 xerrors.Transport(grpcStatus.Error(grpcCodes.Unavailable, "")), 47 xerrors.WithBackoff(backoff.TypeFast), 48 xerrors.WithDeleteSession(), 49 ), 50 xerrors.Retryable( 51 grpcStatus.Error(grpcCodes.Unavailable, ""), 52 xerrors.WithBackoff(backoff.TypeFast), 53 xerrors.WithDeleteSession(), 54 ), 55 xerrors.Transport(grpcStatus.Error(grpcCodes.DataLoss, "")), 56 xerrors.Transport(grpcStatus.Error(grpcCodes.Unauthenticated, "")), 57 xerrors.Operation( 58 xerrors.WithStatusCode(Ydb.StatusIds_STATUS_CODE_UNSPECIFIED), 59 ), 60 xerrors.Operation( 61 xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST), 62 ), 63 xerrors.Operation( 64 xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED), 65 ), 66 xerrors.Operation( 67 xerrors.WithStatusCode(Ydb.StatusIds_INTERNAL_ERROR), 68 ), 69 xerrors.Operation( 70 xerrors.WithStatusCode(Ydb.StatusIds_ABORTED), 71 ), 72 xerrors.Operation( 73 xerrors.WithStatusCode(Ydb.StatusIds_UNAVAILABLE), 74 ), 75 xerrors.Operation( 76 xerrors.WithStatusCode(Ydb.StatusIds_OVERLOADED), 77 ), 78 xerrors.Operation( 79 xerrors.WithStatusCode(Ydb.StatusIds_SCHEME_ERROR), 80 ), 81 xerrors.Operation( 82 xerrors.WithStatusCode(Ydb.StatusIds_GENERIC_ERROR), 83 ), 84 xerrors.Operation( 85 xerrors.WithStatusCode(Ydb.StatusIds_TIMEOUT), 86 ), 87 xerrors.Operation( 88 xerrors.WithStatusCode(Ydb.StatusIds_BAD_SESSION), 89 ), 90 xerrors.Operation( 91 xerrors.WithStatusCode(Ydb.StatusIds_PRECONDITION_FAILED), 92 ), 93 xerrors.Operation( 94 xerrors.WithStatusCode(Ydb.StatusIds_ALREADY_EXISTS), 95 ), 96 xerrors.Operation( 97 xerrors.WithStatusCode(Ydb.StatusIds_NOT_FOUND), 98 ), 99 xerrors.Operation( 100 xerrors.WithStatusCode(Ydb.StatusIds_SESSION_EXPIRED), 101 ), 102 xerrors.Operation( 103 xerrors.WithStatusCode(Ydb.StatusIds_CANCELLED), 104 ), 105 xerrors.Operation( 106 xerrors.WithStatusCode(Ydb.StatusIds_UNDETERMINED), 107 ), 108 xerrors.Operation( 109 xerrors.WithStatusCode(Ydb.StatusIds_UNSUPPORTED), 110 ), 111 xerrors.Operation( 112 xerrors.WithStatusCode(Ydb.StatusIds_SESSION_BUSY), 113 ), 114 xerrors.Retryable(errors.New("retryable error")), 115 xerrors.Retryable(errors.New("retryable error"), xerrors.WithDeleteSession()), 116 io.EOF, 117 xerrors.WithStackTrace(io.EOF), 118 } 119 120 func Test_badConnError_Is(t *testing.T) { 121 for _, err := range errsToCheck { 122 t.Run(err.Error(), func(t *testing.T) { 123 err = Map(err) 124 require.Equal(t, 125 xerrors.MustDeleteSession(err), 126 xerrors.Is(err, driver.ErrBadConn), 127 ) 128 }) 129 } 130 } 131 132 func Test_badConnError_As_Error(t *testing.T) { 133 for _, err := range errsToCheck { 134 t.Run(err.Error(), func(t *testing.T) { 135 require.ErrorAs(t, Map(err), &err) //nolint:gosec 136 }) 137 } 138 }