|
| 1 | +package topic |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" |
| 9 | + grpcCodes "google.golang.org/grpc/codes" |
| 10 | + |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/backoff" |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCheckRetryMode(t *testing.T) { |
| 16 | + fastError := xerrors.Transport(xerrors.WithCode(grpcCodes.Unavailable)) |
| 17 | + slowError := xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_OVERLOADED)) |
| 18 | + unretriable := xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED)) |
| 19 | + |
| 20 | + table := []struct { |
| 21 | + name string |
| 22 | + err error |
| 23 | + settings RetrySettings |
| 24 | + duration time.Duration |
| 25 | + resBackoff backoff.Backoff |
| 26 | + resRetriable bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "OK", |
| 30 | + err: nil, |
| 31 | + settings: RetrySettings{}, |
| 32 | + duration: 0, |
| 33 | + resBackoff: nil, |
| 34 | + resRetriable: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "RetryRetriableErrorFast", |
| 38 | + err: fastError, |
| 39 | + settings: RetrySettings{}, |
| 40 | + duration: 0, |
| 41 | + resBackoff: backoff.Fast, |
| 42 | + resRetriable: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "RetryRetriableErrorFastWithTimeout", |
| 46 | + err: fastError, |
| 47 | + settings: RetrySettings{ |
| 48 | + StartTimeout: time.Second, |
| 49 | + }, |
| 50 | + duration: time.Second * 2, |
| 51 | + resBackoff: nil, |
| 52 | + resRetriable: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "RetryRetriableErrorSlow", |
| 56 | + err: slowError, |
| 57 | + settings: RetrySettings{}, |
| 58 | + duration: 0, |
| 59 | + resBackoff: backoff.Slow, |
| 60 | + resRetriable: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "RetryRetriableErrorSlowWithTimeout", |
| 64 | + err: slowError, |
| 65 | + settings: RetrySettings{ |
| 66 | + StartTimeout: time.Second, |
| 67 | + }, |
| 68 | + duration: time.Second * 2, |
| 69 | + resBackoff: nil, |
| 70 | + resRetriable: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "UnretriableError", |
| 74 | + err: unretriable, |
| 75 | + settings: RetrySettings{}, |
| 76 | + duration: 0, |
| 77 | + resBackoff: nil, |
| 78 | + resRetriable: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "UserOverrideFastErrorDefault", |
| 82 | + err: fastError, |
| 83 | + settings: RetrySettings{ |
| 84 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 85 | + return PublicRetryDecisionDefault |
| 86 | + }, |
| 87 | + }, |
| 88 | + duration: 0, |
| 89 | + resBackoff: backoff.Fast, |
| 90 | + resRetriable: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "UserOverrideFastErrorRetry", |
| 94 | + err: fastError, |
| 95 | + settings: RetrySettings{ |
| 96 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 97 | + return PublicRetryDecisionRetry |
| 98 | + }, |
| 99 | + }, |
| 100 | + duration: 0, |
| 101 | + resBackoff: backoff.Fast, |
| 102 | + resRetriable: true, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "UserOverrideFastErrorStop", |
| 106 | + err: fastError, |
| 107 | + settings: RetrySettings{ |
| 108 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 109 | + return PublicRetryDecisionStop |
| 110 | + }, |
| 111 | + }, |
| 112 | + duration: 0, |
| 113 | + resBackoff: nil, |
| 114 | + resRetriable: false, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "UserOverrideSlowErrorDefault", |
| 118 | + err: slowError, |
| 119 | + settings: RetrySettings{ |
| 120 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 121 | + return PublicRetryDecisionDefault |
| 122 | + }, |
| 123 | + }, |
| 124 | + duration: 0, |
| 125 | + resBackoff: backoff.Slow, |
| 126 | + resRetriable: true, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "UserOverrideSlowErrorRetry", |
| 130 | + err: slowError, |
| 131 | + settings: RetrySettings{ |
| 132 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 133 | + return PublicRetryDecisionRetry |
| 134 | + }, |
| 135 | + }, |
| 136 | + duration: 0, |
| 137 | + resBackoff: backoff.Slow, |
| 138 | + resRetriable: true, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "UserOverrideSlowErrorStop", |
| 142 | + err: slowError, |
| 143 | + settings: RetrySettings{ |
| 144 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 145 | + return PublicRetryDecisionStop |
| 146 | + }, |
| 147 | + }, |
| 148 | + duration: 0, |
| 149 | + resBackoff: nil, |
| 150 | + resRetriable: false, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "UserOverrideUnretriableErrorDefault", |
| 154 | + err: xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED)), |
| 155 | + settings: RetrySettings{ |
| 156 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 157 | + return PublicRetryDecisionDefault |
| 158 | + }, |
| 159 | + }, |
| 160 | + duration: 0, |
| 161 | + resBackoff: nil, |
| 162 | + resRetriable: false, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "UserOverrideUnretriableErrorRetry", |
| 166 | + err: xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED)), |
| 167 | + settings: RetrySettings{ |
| 168 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 169 | + return PublicRetryDecisionRetry |
| 170 | + }, |
| 171 | + }, |
| 172 | + duration: 0, |
| 173 | + resBackoff: backoff.Slow, |
| 174 | + resRetriable: true, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "UserOverrideUnretriableErrorStop", |
| 178 | + err: xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED)), |
| 179 | + settings: RetrySettings{ |
| 180 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 181 | + return PublicRetryDecisionStop |
| 182 | + }, |
| 183 | + }, |
| 184 | + duration: 0, |
| 185 | + resBackoff: nil, |
| 186 | + resRetriable: false, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "UserOverrideFastErrorRetryWithTimeout", |
| 190 | + err: xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_UNAUTHORIZED)), |
| 191 | + settings: RetrySettings{ |
| 192 | + StartTimeout: time.Second, |
| 193 | + CheckError: func(errInfo PublicCheckErrorRetryArgs) PublicCheckRetryResult { |
| 194 | + return PublicRetryDecisionRetry |
| 195 | + }, |
| 196 | + }, |
| 197 | + duration: time.Second * 2, |
| 198 | + resBackoff: nil, |
| 199 | + resRetriable: false, |
| 200 | + }, |
| 201 | + } |
| 202 | + |
| 203 | + for _, test := range table { |
| 204 | + t.Run(test.name, func(t *testing.T) { |
| 205 | + resBackoff, retriable := CheckRetryMode(test.err, test.settings, test.duration) |
| 206 | + require.Equal(t, test.resBackoff, resBackoff) |
| 207 | + require.Equal(t, test.resRetriable, retriable) |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
0 commit comments