github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/errors/helper_test.go (about) 1 // Copyright 2020 PingCAP, 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package errors 15 16 import ( 17 "context" 18 "testing" 19 20 "github.com/pingcap/check" 21 "github.com/pingcap/errors" 22 "github.com/pingcap/ticdc/pkg/util/testleak" 23 ) 24 25 func TestSuite(t *testing.T) { 26 check.TestingT(t) 27 } 28 29 type helperSuite struct{} 30 31 var _ = check.Suite(&helperSuite{}) 32 33 func (s *helperSuite) TestWrapError(c *check.C) { 34 defer testleak.AfterTest(c)() 35 var ( 36 rfcError = ErrDecodeFailed 37 err = errors.New("test") 38 testCases = []struct { 39 err error 40 isNil bool 41 expected string 42 }{ 43 {nil, true, ""}, 44 {err, false, "[CDC:ErrDecodeFailed]test"}, 45 } 46 ) 47 for _, tc := range testCases { 48 we := WrapError(rfcError, tc.err) 49 if tc.isNil { 50 c.Assert(we, check.IsNil) 51 } else { 52 c.Assert(we, check.NotNil) 53 c.Assert(we.Error(), check.Equals, tc.expected) 54 } 55 } 56 } 57 58 func (s *helperSuite) TestIsRetryableError(c *check.C) { 59 defer testleak.AfterTest(c)() 60 61 tests := []struct { 62 name string 63 err error 64 want bool 65 }{ 66 {"nil error", nil, false}, 67 {"context Canceled err", context.Canceled, false}, 68 {"context DeadlineExceeded err", context.DeadlineExceeded, false}, 69 {"normal err", errors.New("test"), true}, 70 {"cdc reachMaxTry err", ErrReachMaxTry, true}, 71 } 72 for _, tt := range tests { 73 ret := IsRetryableError(tt.err) 74 c.Assert(ret, check.Equals, tt.want, check.Commentf("case:%s", tt.name)) 75 } 76 }