github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/relay/remote_retry_test.go (about) 1 // Copyright 2019 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 relay 15 16 import ( 17 "context" 18 "errors" 19 "time" 20 21 gmysql "github.com/go-mysql-org/go-mysql/mysql" 22 . "github.com/pingcap/check" 23 ) 24 25 var _ = Suite(&testReaderRetrySuite{}) 26 27 type testReaderRetrySuite struct{} 28 29 func (t *testReaderRetrySuite) TestRetry(c *C) { 30 rr, err := NewReaderRetry(ReaderRetryConfig{ 31 BackoffRollback: 200 * time.Millisecond, 32 BackoffMax: 1 * time.Second, 33 BackoffMin: 1 * time.Millisecond, 34 BackoffFactor: 2, 35 }) 36 c.Assert(err, IsNil) 37 c.Assert(rr, NotNil) 38 39 retryableErr := gmysql.ErrBadConn 40 unRetryableErr := errors.New("custom error") 41 ctx := context.Background() 42 43 // check some times 44 for i := 0; i < 3; i++ { 45 c.Assert(rr.Check(ctx, retryableErr), IsTrue) 46 } 47 c.Assert(rr.bf.Current(), Equals, 8*time.Millisecond) 48 49 // check more times, until reach Max 50 for i := 0; i < 10; i++ { 51 c.Assert(rr.Check(ctx, retryableErr), IsTrue) 52 } 53 c.Assert(rr.bf.Current(), Equals, rr.cfg.BackoffMax) 54 55 // sleep 1s 56 time.Sleep(1 * time.Second) 57 58 // check with rollback, rollback 5 times, forward 1 time 59 c.Assert(rr.Check(ctx, retryableErr), IsTrue) 60 c.Assert(rr.bf.Current(), Equals, 64*time.Millisecond) 61 62 // check un-retryable error 63 c.Assert(rr.Check(ctx, unRetryableErr), IsFalse) 64 65 // check with context timeout 66 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) 67 defer cancel() 68 c.Assert(rr.Check(ctx, retryableErr), IsFalse) 69 }