github.com/cloudwego/kitex@v0.9.0/pkg/retry/failure_test.go (about) 1 /* 2 * Copyright 2021 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 "testing" 21 "time" 22 23 "github.com/cloudwego/kitex/internal/test" 24 ) 25 26 func BenchmarkRandomBackOff_Wait(b *testing.B) { 27 min := 10 28 max := 100 29 bk := &randomBackOff{ 30 minMS: min, 31 maxMS: max, 32 } 33 b.RunParallel(func(pb *testing.PB) { 34 for pb.Next() { 35 ms := int(bk.randomDuration().Milliseconds()) 36 test.Assert(b, ms <= max && ms >= min, ms) 37 } 38 }) 39 } 40 41 func TestRandomBackOff_Wait(t *testing.T) { 42 const ( 43 min = 99 44 max = 100 45 ) 46 bk := &randomBackOff{ 47 minMS: min, 48 maxMS: max, 49 } 50 startTime := time.Now() 51 bk.Wait(1) 52 waitTime := time.Since(startTime) 53 test.Assert(t, time.Millisecond*min <= waitTime) 54 test.Assert(t, waitTime < time.Millisecond*(max+5)) 55 } 56 57 func TestRandomBackOff_String(t *testing.T) { 58 min := 10 59 max := 100 60 bk := &randomBackOff{ 61 minMS: min, 62 maxMS: max, 63 } 64 msg := "RandomBackOff(10ms-100ms)" 65 test.Assert(t, bk.String() == msg) 66 } 67 68 func TestFixedBackOff_Wait(t *testing.T) { 69 const fix = 50 70 bk := &fixedBackOff{ 71 fixMS: fix, 72 } 73 startTime := time.Now() 74 bk.Wait(1) 75 waitTime := time.Since(startTime) 76 test.Assert(t, time.Millisecond*fix <= waitTime) 77 test.Assert(t, waitTime < time.Millisecond*(fix+5)) 78 } 79 80 func TestFixedBackOff_String(t *testing.T) { 81 fix := 50 82 bk := &fixedBackOff{ 83 fixMS: fix, 84 } 85 msg := "FixedBackOff(50ms)" 86 test.Assert(t, bk.String() == msg) 87 } 88 89 func TestNoneBackOff_String(t *testing.T) { 90 bk := &noneBackOff{} 91 msg := "NoneBackOff" 92 test.Assert(t, bk.String() == msg) 93 }