Skip to content

Commit

Permalink
remove registry
Browse files Browse the repository at this point in the history
  • Loading branch information
imtbkcat committed Aug 10, 2020
1 parent a28915e commit 04320b7
Showing 1 changed file with 17 additions and 80 deletions.
97 changes: 17 additions & 80 deletions terror_test/terror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
package terror_test

import (
"bytes"
"encoding/json"
"fmt"
"os"
"runtime"
"sort"
"strings"
"testing"
"time"
Expand All @@ -32,14 +30,13 @@ import (
// Those fields below are copied from the original version of terror,
// so that we can reuse those test cases.
var (
reg = errors.NewRegistry("DB")
ClassExecutor = reg.RegisterErrorClass(5, "executor")
ClassKV = reg.RegisterErrorClass(8, "kv")
ClassOptimizer = reg.RegisterErrorClass(10, "planner")
ClassParser = reg.RegisterErrorClass(11, "parser")
ClassServer = reg.RegisterErrorClass(15, "server")
ClassTable = reg.RegisterErrorClass(19, "table")
ClassMember = reg.RegisterErrorClass(20, "member")
ClassExecutor = errors.RegisterErrorClass(5, "executor")
ClassKV = errors.RegisterErrorClass(8, "kv")
ClassOptimizer = errors.RegisterErrorClass(10, "planner")
ClassParser = errors.RegisterErrorClass(11, "parser")
ClassServer = errors.RegisterErrorClass(15, "server")
ClassTable = errors.RegisterErrorClass(19, "table")
ClassMember = errors.RegisterErrorClass(20, "member")
)

const (
Expand Down Expand Up @@ -89,7 +86,7 @@ func (s *testTErrorSuite) TestTError(c *C) {
kvErr := ClassKV.New(1062, "key already exist")
e := kvErr.FastGen("Duplicate entry '%d' for key 'PRIMARY'", 1)
c.Assert(e, NotNil)
c.Assert(e.Error(), Equals, "[DB:kv:1062] Duplicate entry '1' for key 'PRIMARY'")
c.Assert(e.Error(), Equals, "[kv:1062] Duplicate entry '1' for key 'PRIMARY'")
}

func (s *testTErrorSuite) TestJson(c *C) {
Expand Down Expand Up @@ -182,26 +179,7 @@ func (s *testTErrorSuite) TestNewError(c *C) {
today := time.Now().Weekday().String()
err := predefinedTextualErr.GenWithStackByArgs(today)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[DB:executor:ExecutorAbsent] executor is taking vacation at "+today)
}

func (s *testTErrorSuite) TestAllErrClasses(c *C) {
items := []errors.ErrClass{
ClassExecutor, ClassKV, ClassOptimizer, ClassParser, ClassServer, ClassTable,
}
registered := reg.AllErrorClasses()

// sort it to align them.
sort.Slice(items, func(i, j int) bool {
return items[i].ID < items[j].ID
})
sort.Slice(registered, func(i, j int) bool {
return registered[i].ID < registered[j].ID
})

for i := range items {
c.Assert(items[i].ID, Equals, registered[i].ID)
}
c.Assert(err.Error(), Equals, "[executor:ExecutorAbsent] executor is taking vacation at "+today)
}

func (s *testTErrorSuite) TestErrorExists(c *C) {
Expand Down Expand Up @@ -240,9 +218,8 @@ func (s *testTErrorSuite) TestErrorExists(c *C) {
}

func (s *testTErrorSuite) TestRFCCode(c *C) {
reg := errors.NewRegistry("TEST")
errc1 := reg.RegisterErrorClass(1, "TestErr1")
errc2 := reg.RegisterErrorClass(2, "TestErr2")
errc1 := errors.RegisterErrorClass(1, "TestErr1")
errc2 := errors.RegisterErrorClass(2, "TestErr2")
c1err1 := errc1.DefineError().
TextualCode("Err1").
MessageTemplate("nothing").
Expand All @@ -251,10 +228,9 @@ func (s *testTErrorSuite) TestRFCCode(c *C) {
TextualCode("Err2").
MessageTemplate("nothing").
Build()
c.Assert(c1err1.RFCCode(), Equals, errors.RFCErrorCode("TEST:TestErr1:Err1"))
c.Assert(c2err2.RFCCode(), Equals, errors.RFCErrorCode("TEST:TestErr2:Err2"))
blankReg := errors.NewRegistry("")
errb := blankReg.RegisterErrorClass(1, "Blank")
c.Assert(c1err1.RFCCode(), Equals, errors.RFCErrorCode("TestErr1:Err1"))
c.Assert(c2err2.RFCCode(), Equals, errors.RFCErrorCode("TestErr2:Err2"))
errb := errors.RegisterErrorClass(3, "Blank")
berr := errb.DefineError().
TextualCode("B1").
MessageTemplate("nothing").
Expand Down Expand Up @@ -284,45 +260,6 @@ workaround = '''Check the status, monitoring data and log of the TiKV server.'''
`
)

func (*testTErrorSuite) TestExport(c *C) {
RegKV := errors.NewRegistry("KV")
Class2PC := RegKV.RegisterErrorClass(1, "2PC")
_ = Class2PC.DefineError().
NumericCode(8005).
Description("A certain Raft Group is not available, such as the number of replicas is not enough.\n" +
"This error usually occurs when the TiKV server is busy or the TiKV node is down.").
Workaround("Check whether `tidb_disable_txn_auto_retry` is set to `on`. If so, set it to `off`; " +
"if it is already `off`, increase the value of `tidb_retry_limit` until the error no longer occurs.").
MessageTemplate("Write Conflict, txnStartTS is stale").
Build()

ClassRegion := RegKV.RegisterErrorClass(2, "Region")
_ = ClassRegion.DefineError().
TextualCode("Unavailable").
Description("A certain Raft Group is not available, such as the number of replicas is not enough.\n" +
"This error usually occurs when the TiKV server is busy or the TiKV node is down.").
Workaround("Check the status, monitoring data and log of the TiKV server.").
MessageTemplate("Region is unavailable").
Build()

ClassSomewhat := RegKV.RegisterErrorClass(3, "Somewhat")
_ = ClassSomewhat.DefineError().
TextualCode("Foo").
MessageTemplate("some %.6s thing happened, and some %#v goes verbose. I'm %6.3f percent confusing...\n" +
"Maybe only %[3]*.[2]*[1]f peaces of placeholders can save me... Oh my %s.%d!").
Build()

result := bytes.NewBuffer([]byte{})
err := RegKV.ExportTo(result)
c.Assert(err, IsNil)
resultStr := result.String()
fmt.Println("Result: ")
fmt.Print(resultStr)
c.Assert(strings.Contains(resultStr, somewhatErrorTOML), IsTrue)
c.Assert(strings.Contains(resultStr, err8005TOML), IsTrue)
c.Assert(strings.Contains(resultStr, errUnavailableTOML), IsTrue)
}

func (*testTErrorSuite) TestLineAndFile(c *C) {
err := predefinedTextualErr.GenWithStackByArgs("everyday")
_, f, l, _ := runtime.Caller(0)
Expand All @@ -344,7 +281,7 @@ func (*testTErrorSuite) TestLineAndFile(c *C) {
func (*testTErrorSuite) TestWarpAndField(c *C) {
causeErr := errors.New("load from etcd meet error")
ErrGetLeader := ClassMember.DefineError().TextualCode("ErrGetLeader").MessageTemplate("fail to get leader").Build()
errWithWarpedCause := ErrGetLeader.WarpCauseError(causeErr)
c.Assert(errWithWarpedCause.FastGenWithCause().Error(), Equals, "[DB:member:ErrGetLeader] load from etcd meet error")
c.Assert(fmt.Sprintf("%v", errWithWarpedCause.FastGenWithCause()), Equals, "[DB:member:ErrGetLeader] fail to get leader")
errWithWarpedCause := ErrGetLeader.Wrap(causeErr)
c.Assert(errWithWarpedCause.FastGenWithCause().Error(), Equals, "[member:ErrGetLeader] load from etcd meet error")
c.Assert(fmt.Sprintf("%v", errWithWarpedCause.FastGenWithCause()), Equals, "[member:ErrGetLeader] fail to get leader")
}

0 comments on commit 04320b7

Please sign in to comment.