github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/context/context_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 context 15 16 import ( 17 "context" 18 "testing" 19 "time" 20 21 "github.com/pingcap/check" 22 "github.com/pingcap/errors" 23 "github.com/pingcap/ticdc/cdc/model" 24 "github.com/pingcap/ticdc/pkg/config" 25 "github.com/pingcap/ticdc/pkg/util/testleak" 26 ) 27 28 func TestSuite(t *testing.T) { check.TestingT(t) } 29 30 type contextSuite struct{} 31 32 var _ = check.Suite(&contextSuite{}) 33 34 func (s *contextSuite) TestVars(c *check.C) { 35 defer testleak.AfterTest(c)() 36 stdCtx := context.Background() 37 conf := config.GetDefaultReplicaConfig() 38 conf.Filter.Rules = []string{"hello.world"} 39 info := &model.ChangeFeedInfo{Config: conf} 40 ctx := NewContext(stdCtx, &GlobalVars{ 41 CaptureInfo: &model.CaptureInfo{ID: "capture1"}, 42 }) 43 ctx = WithChangefeedVars(ctx, &ChangefeedVars{ 44 Info: info, 45 }) 46 c.Assert(ctx.ChangefeedVars().Info, check.DeepEquals, info) 47 c.Assert(ctx.GlobalVars().CaptureInfo.ID, check.Equals, "capture1") 48 } 49 50 func (s *contextSuite) TestStdCancel(c *check.C) { 51 defer testleak.AfterTest(c)() 52 stdCtx := context.Background() 53 stdCtx, cancel := context.WithCancel(stdCtx) 54 ctx := NewContext(stdCtx, &GlobalVars{}) 55 cancel() 56 <-ctx.Done() 57 } 58 59 func (s *contextSuite) TestCancel(c *check.C) { 60 defer testleak.AfterTest(c)() 61 stdCtx := context.Background() 62 ctx := NewContext(stdCtx, &GlobalVars{}) 63 ctx, cancel := WithCancel(ctx) 64 cancel() 65 <-ctx.Done() 66 } 67 68 func (s *contextSuite) TestCancelCascade(c *check.C) { 69 defer testleak.AfterTest(c)() 70 startTime := time.Now() 71 stdCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Second)) 72 ctx := NewContext(stdCtx, &GlobalVars{}) 73 ctx1, _ := WithCancel(ctx) 74 ctx2, cancel2 := WithCancel(ctx) 75 cancel2() 76 <-ctx2.Done() 77 c.Assert(time.Since(startTime), check.Less, time.Second) 78 <-ctx1.Done() 79 c.Assert(time.Since(startTime), check.GreaterEqual, time.Second) 80 cancel() 81 } 82 83 func (s *contextSuite) TestThrow(c *check.C) { 84 defer testleak.AfterTest(c)() 85 stdCtx := context.Background() 86 ctx := NewContext(stdCtx, &GlobalVars{}) 87 ctx, cancel := WithCancel(ctx) 88 ctx = WithErrorHandler(ctx, func(err error) error { 89 c.Assert(err.Error(), check.Equals, "mock error") 90 cancel() 91 return nil 92 }) 93 ctx.Throw(nil) 94 ctx.Throw(errors.New("mock error")) 95 <-ctx.Done() 96 } 97 98 func (s *contextSuite) TestThrowCascade(c *check.C) { 99 defer testleak.AfterTest(c)() 100 stdCtx := context.Background() 101 ctx := NewContext(stdCtx, &GlobalVars{}) 102 ctx, cancel := WithCancel(ctx) 103 var errNum1, errNum2, errNum3 int 104 ctx = WithErrorHandler(ctx, func(err error) error { 105 if err.Error() == "mock error" { 106 errNum1++ 107 } else if err.Error() == "mock error2" { 108 errNum2++ 109 } else { 110 c.Fail() 111 } 112 return nil 113 }) 114 ctx2 := WithErrorHandler(ctx, func(err error) error { 115 if err.Error() == "mock error2" { 116 errNum2++ 117 return err 118 } else if err.Error() == "mock error3" { 119 errNum3++ 120 } else { 121 c.Fail() 122 } 123 return nil 124 }) 125 ctx2.Throw(errors.New("mock error2")) 126 ctx2.Throw(errors.New("mock error3")) 127 ctx.Throw(errors.New("mock error")) 128 c.Assert(errNum1, check.Equals, 1) 129 c.Assert(errNum2, check.Equals, 2) 130 c.Assert(errNum3, check.Equals, 1) 131 cancel() 132 <-ctx.Done() 133 } 134 135 func (s *contextSuite) TestThrowPanic(c *check.C) { 136 defer testleak.AfterTest(c)() 137 defer func() { 138 panicMsg := recover() 139 c.Assert(panicMsg, check.Equals, "an error has escaped, please report a bug{error 26 0 mock error}") 140 }() 141 stdCtx := context.Background() 142 ctx := NewContext(stdCtx, &GlobalVars{}) 143 ctx.Throw(nil) 144 ctx.Throw(errors.New("mock error")) 145 }