github.com/cloudwego/hertz@v0.9.3/pkg/app/client/retry/retry_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package retry 18 19 import ( 20 "math" 21 "testing" 22 "time" 23 24 "github.com/cloudwego/hertz/pkg/common/test/assert" 25 ) 26 27 func TestApply(t *testing.T) { 28 delayPolicyFunc := func(attempts uint, err error, retryConfig *Config) time.Duration { 29 return time.Second 30 } 31 options := []Option{} 32 options = append(options, WithMaxAttemptTimes(100), WithInitDelay(time.Second), 33 WithMaxDelay(time.Second), WithDelayPolicy(delayPolicyFunc), WithMaxJitter(time.Second)) 34 35 config := Config{} 36 config.Apply(options) 37 38 assert.DeepEqual(t, uint(100), config.MaxAttemptTimes) 39 assert.DeepEqual(t, time.Second, config.Delay) 40 assert.DeepEqual(t, time.Second, config.MaxDelay) 41 assert.DeepEqual(t, time.Second, Delay(0, nil, &config)) 42 assert.DeepEqual(t, time.Second, config.MaxJitter) 43 } 44 45 func TestPolicy(t *testing.T) { 46 dur := DefaultDelayPolicy(0, nil, nil) 47 assert.DeepEqual(t, 0*time.Millisecond, dur) 48 49 config := Config{ 50 Delay: time.Second, 51 } 52 dur = FixedDelayPolicy(0, nil, &config) 53 assert.DeepEqual(t, time.Second, dur) 54 55 dur = RandomDelayPolicy(0, nil, &config) 56 assert.DeepEqual(t, 0*time.Millisecond, dur) 57 config.MaxJitter = time.Second * 1 58 dur = RandomDelayPolicy(0, nil, &config) 59 assert.NotEqual(t, time.Second*1, dur) 60 61 dur = BackOffDelayPolicy(0, nil, &config) 62 assert.DeepEqual(t, time.Second*1, dur) 63 config.Delay = time.Duration(-1) 64 dur = BackOffDelayPolicy(0, nil, &config) 65 assert.DeepEqual(t, time.Second*0, dur) 66 config.Delay = time.Duration(1) 67 dur = BackOffDelayPolicy(63, nil, &config) 68 durExp := config.Delay << 62 69 assert.DeepEqual(t, durExp, dur) 70 71 dur = Delay(0, nil, &config) 72 assert.DeepEqual(t, 0*time.Millisecond, dur) 73 delayPolicyFunc := func(attempts uint, err error, retryConfig *Config) time.Duration { 74 return time.Second 75 } 76 config.DelayPolicy = delayPolicyFunc 77 config.MaxDelay = time.Second / 2 78 dur = Delay(0, nil, &config) 79 assert.DeepEqual(t, config.MaxDelay, dur) 80 81 delayPolicyFunc2 := func(attempts uint, err error, retryConfig *Config) time.Duration { 82 return time.Duration(math.MaxInt64) 83 } 84 delayFunc := CombineDelay(delayPolicyFunc2, delayPolicyFunc) 85 dur = delayFunc(0, nil, &config) 86 assert.DeepEqual(t, time.Duration(math.MaxInt64), dur) 87 }