github.com/XiaoMi/Gaea@v1.2.5/parser/terror/terror_test.go (about)

     1  // Copyright 2015 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 terror
    15  
    16  import (
    17  	"encoding/json"
    18  	"testing"
    19  
    20  	. "github.com/pingcap/check"
    21  	"github.com/pingcap/errors"
    22  
    23  	"github.com/XiaoMi/Gaea/util/testleak"
    24  )
    25  
    26  func TestT(t *testing.T) {
    27  	CustomVerboseFlag = true
    28  	TestingT(t)
    29  }
    30  
    31  var _ = Suite(&testTErrorSuite{})
    32  
    33  type testTErrorSuite struct {
    34  }
    35  
    36  func (s *testTErrorSuite) TestErrCode(c *C) {
    37  	c.Assert(CodeMissConnectionID, Equals, ErrCode(1))
    38  	c.Assert(CodeResultUndetermined, Equals, ErrCode(2))
    39  }
    40  
    41  func (s *testTErrorSuite) TestTError(c *C) {
    42  	defer testleak.AfterTest(c)()
    43  	c.Assert(ClassParser.String(), Not(Equals), "")
    44  	c.Assert(ClassOptimizer.String(), Not(Equals), "")
    45  	c.Assert(ClassKV.String(), Not(Equals), "")
    46  	c.Assert(ClassServer.String(), Not(Equals), "")
    47  
    48  	parserErr := ClassParser.New(ErrCode(1), "error 1")
    49  	c.Assert(parserErr.Error(), Not(Equals), "")
    50  	c.Assert(ClassParser.EqualClass(parserErr), IsTrue)
    51  	c.Assert(ClassParser.NotEqualClass(parserErr), IsFalse)
    52  
    53  	c.Assert(ClassOptimizer.EqualClass(parserErr), IsFalse)
    54  	optimizerErr := ClassOptimizer.New(ErrCode(2), "abc")
    55  	c.Assert(ClassOptimizer.EqualClass(errors.New("abc")), IsFalse)
    56  	c.Assert(ClassOptimizer.EqualClass(nil), IsFalse)
    57  	c.Assert(optimizerErr.Equal(optimizerErr.GenWithStack("def")), IsTrue)
    58  	c.Assert(optimizerErr.Equal(nil), IsFalse)
    59  	c.Assert(optimizerErr.Equal(errors.New("abc")), IsFalse)
    60  
    61  	// Test case for FastGen.
    62  	c.Assert(optimizerErr.Equal(optimizerErr.FastGen("def")), IsTrue)
    63  	c.Assert(optimizerErr.Equal(optimizerErr.FastGen("def: %s", "def")), IsTrue)
    64  	kvErr := ClassKV.New(1062, "key already exist")
    65  	e := kvErr.FastGen("Duplicate entry '%d' for key 'PRIMARY'", 1)
    66  	c.Assert(e.Error(), Equals, "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
    67  	kvMySQLErrCodes := map[ErrCode]uint16{
    68  		1062: uint16(1062),
    69  	}
    70  	ErrClassToMySQLCodes[ClassKV] = kvMySQLErrCodes
    71  	sqlErr := errors.Cause(e).(*Error).ToSQLError()
    72  	c.Assert(sqlErr.Message, Equals, "Duplicate entry '1' for key 'PRIMARY'")
    73  	c.Assert(sqlErr.Code, Equals, uint16(1062))
    74  
    75  	err := errors.Trace(ErrCritical.GenWithStackByArgs("test"))
    76  	c.Assert(ErrCritical.Equal(err), IsTrue)
    77  
    78  	err = errors.Trace(ErrCritical)
    79  	c.Assert(ErrCritical.Equal(err), IsTrue)
    80  }
    81  
    82  func (s *testTErrorSuite) TestJson(c *C) {
    83  	prevTErr := &Error{
    84  		class:   ClassTable,
    85  		code:    CodeExecResultIsEmpty,
    86  		message: "json test",
    87  	}
    88  	buf, err := json.Marshal(prevTErr)
    89  	c.Assert(err, IsNil)
    90  	var curTErr Error
    91  	err = json.Unmarshal(buf, &curTErr)
    92  	c.Assert(err, IsNil)
    93  	isEqual := prevTErr.Equal(&curTErr)
    94  	c.Assert(isEqual, IsTrue)
    95  }
    96  
    97  var predefinedErr = ClassExecutor.New(ErrCode(123), "predefiend error")
    98  
    99  func example() error {
   100  	err := call()
   101  	return errors.Trace(err)
   102  }
   103  
   104  func call() error {
   105  	return predefinedErr.GenWithStack("error message:%s", "abc")
   106  }
   107  
   108  func (s *testTErrorSuite) TestErrorEqual(c *C) {
   109  	defer testleak.AfterTest(c)()
   110  	e1 := errors.New("test error")
   111  	c.Assert(e1, NotNil)
   112  
   113  	e2 := errors.Trace(e1)
   114  	c.Assert(e2, NotNil)
   115  
   116  	e3 := errors.Trace(e2)
   117  	c.Assert(e3, NotNil)
   118  
   119  	c.Assert(errors.Cause(e2), Equals, e1)
   120  	c.Assert(errors.Cause(e3), Equals, e1)
   121  	c.Assert(errors.Cause(e2), Equals, errors.Cause(e3))
   122  
   123  	e4 := errors.New("test error")
   124  	c.Assert(errors.Cause(e4), Not(Equals), e1)
   125  
   126  	e5 := errors.Errorf("test error")
   127  	c.Assert(errors.Cause(e5), Not(Equals), e1)
   128  
   129  	c.Assert(ErrorEqual(e1, e2), IsTrue)
   130  	c.Assert(ErrorEqual(e1, e3), IsTrue)
   131  	c.Assert(ErrorEqual(e1, e4), IsTrue)
   132  	c.Assert(ErrorEqual(e1, e5), IsTrue)
   133  
   134  	var e6 error
   135  
   136  	c.Assert(ErrorEqual(nil, nil), IsTrue)
   137  	c.Assert(ErrorNotEqual(e1, e6), IsTrue)
   138  	code1 := ErrCode(1)
   139  	code2 := ErrCode(2)
   140  	te1 := ClassParser.New(code1, "abc")
   141  	te2 := ClassParser.New(code1, "def")
   142  	te3 := ClassKV.New(code1, "abc")
   143  	te4 := ClassKV.New(code2, "abc")
   144  	c.Assert(ErrorEqual(te1, te2), IsTrue)
   145  	c.Assert(ErrorEqual(te1, te3), IsFalse)
   146  	c.Assert(ErrorEqual(te3, te4), IsFalse)
   147  }