go.temporal.io/server@v1.23.0/common/backoff/jitter_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package backoff 26 27 import ( 28 "math/rand" 29 "testing" 30 "time" 31 32 "github.com/stretchr/testify/suite" 33 ) 34 35 type ( 36 jitterSuite struct { 37 suite.Suite 38 } 39 ) 40 41 func TestJitterSuite(t *testing.T) { 42 s := new(jitterSuite) 43 suite.Run(t, s) 44 } 45 46 func (s *jitterSuite) SetupSuite() { 47 } 48 49 func (s *jitterSuite) TestJitter_Int64() { 50 input := int64(1048576) 51 coefficient := float64(0.25) 52 lowerBound := int64(float64(input) * (1 - coefficient)) 53 upperBound := int64(float64(input) * (1 + coefficient)) 54 fullJitterUpperBound := int64(float64(input) * 2) 55 56 for i := 0; i < 1048576; i++ { 57 result := Jitter(input, coefficient) 58 s.True(result >= lowerBound) 59 s.True(result < upperBound) 60 61 result = FullJitter(input) 62 s.True(result >= 0) 63 s.True(result < fullJitterUpperBound) 64 } 65 } 66 67 func (s *jitterSuite) TestJitter_Float64() { 68 input := float64(1048576.1048576) 69 coefficient := float64(0.16) 70 lowerBound := float64(input) * (1 - coefficient) 71 upperBound := float64(input) * (1 + coefficient) 72 fullJitterUpperBound := float64(input) * 2 73 74 for i := 0; i < 1048576; i++ { 75 result := Jitter(input, coefficient) 76 s.True(result >= lowerBound) 77 s.True(result < upperBound) 78 79 result = FullJitter(input) 80 s.True(result >= 0) 81 s.True(result < fullJitterUpperBound) 82 } 83 } 84 85 func (s *jitterSuite) TestJitter_Duration() { 86 input := time.Duration(1099511627776) 87 coefficient := float64(0.1) 88 lowerBound := time.Duration(int64(float64(input.Nanoseconds()) * (1 - coefficient))) 89 upperBound := time.Duration(int64(float64(input.Nanoseconds()) * (1 + coefficient))) 90 fullJitterUpperBound := time.Duration(int64(float64(input.Nanoseconds()) * 2)) 91 92 for i := 0; i < 1048576; i++ { 93 result := Jitter(input, coefficient) 94 s.True(result >= lowerBound) 95 s.True(result < upperBound) 96 97 result = FullJitter(input) 98 s.True(result >= 0) 99 s.True(result < fullJitterUpperBound) 100 } 101 } 102 103 func (s *jitterSuite) TestJitter_InputZeroValue() { 104 s.Zero(Jitter(time.Duration(0), rand.Float64())) 105 s.Zero(Jitter(int64(0), rand.Float64())) 106 s.Zero(Jitter(float64(0), rand.Float64())) 107 } 108 109 func (s *jitterSuite) TestJitter_CoeffientZeroValue() { 110 s.Equal(time.Duration(1), Jitter(time.Duration(1), 0)) 111 s.Equal(int64(1), Jitter(int64(1), 0)) 112 s.Equal(float64(1), Jitter(float64(1), 0)) 113 }