github.com/kaydxh/golang@v0.0.131/go/time/exponential_backoff_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package time_test 23 24 import ( 25 "testing" 26 "time" 27 28 time_ "github.com/kaydxh/golang/go/time" 29 "gotest.tools/assert" 30 ) 31 32 func TestExponentialBackOff(t *testing.T) { 33 var ( 34 testInitialInterval = 500 * time.Millisecond 35 testRandomizationFactor = 0.1 36 testMultiplier = 2.0 37 testMaxInterval = 5 * time.Second 38 testMaxElapsedTime = 15 * time.Minute 39 testMaxElasedCount = 1 40 ) 41 42 exp := time_.NewExponentialBackOff( 43 time_.WithExponentialBackOffOptionInitialInterval(testInitialInterval), 44 time_.WithExponentialBackOffOptionRandomizationFactor(testRandomizationFactor), 45 time_.WithExponentialBackOffOptionMultiplier(testMultiplier), 46 time_.WithExponentialBackOffOptionMaxInterval(testMaxInterval), 47 time_.WithExponentialBackOffOptionMaxElapsedTime(testMaxElapsedTime), 48 time_.WithExponentialBackOffOptionMaxElapsedCount(testMaxElasedCount), 49 ) 50 51 expectedResults := []time.Duration{500, 1000, 2000, 4000, 5000, 5000, 5000, 5000, 5000, 5000} 52 for i, d := range expectedResults { 53 expectedResults[i] = d * time.Millisecond 54 } 55 for _, expected := range expectedResults { 56 assert.Equal(t, expected, exp.GetCurrentInterval()) 57 // Assert that the next backoff falls in the expected range. 58 var minInterval = expected - time.Duration(testRandomizationFactor*float64(expected)) 59 var maxInterval = expected + time.Duration(testRandomizationFactor*float64(expected)) 60 actualInterval, over := exp.NextBackOff() 61 t.Logf("over: %v, actualInterval: %v", over, actualInterval) 62 if !(minInterval <= actualInterval && actualInterval <= maxInterval) { 63 t.Error("error") 64 } 65 } 66 67 } 68 69 func TestExponentialBackOffMaxElaspedTimeFailOver(t *testing.T) { 70 var ( 71 testInitialInterval = 500 * time.Millisecond 72 testRandomizationFactor = 0.1 73 testMultiplier = 2.0 74 testMaxInterval = 5 * time.Second 75 testMaxElapsedTime = 10 * time.Second 76 ) 77 78 exp := time_.NewExponentialBackOff( 79 time_.WithExponentialBackOffOptionInitialInterval(testInitialInterval), 80 time_.WithExponentialBackOffOptionRandomizationFactor(testRandomizationFactor), 81 time_.WithExponentialBackOffOptionMultiplier(testMultiplier), 82 time_.WithExponentialBackOffOptionMaxInterval(testMaxInterval), 83 time_.WithExponentialBackOffOptionMaxElapsedTime(testMaxElapsedTime), 84 ) 85 86 expectedResults := []time.Duration{500, 1000, 2000, 4000, 5000, 5000, 5000, 5000, 5000, 5000} 87 for i, d := range expectedResults { 88 expectedResults[i] = d * time.Millisecond 89 } 90 for _, expected := range expectedResults { 91 assert.Equal(t, expected, exp.GetCurrentInterval()) 92 // Assert that the next backoff falls in the expected range. 93 var minInterval = expected - time.Duration(testRandomizationFactor*float64(expected)) 94 var maxInterval = expected + time.Duration(testRandomizationFactor*float64(expected)) 95 actualInterval, over := exp.NextBackOff() 96 t.Logf("over: %v, actualInterval: %v", over, actualInterval) 97 if !(minInterval <= actualInterval && actualInterval <= maxInterval) { 98 t.Error("error") 99 } 100 time.Sleep(actualInterval) 101 } 102 103 } 104 105 func TestDescExponentialBackOff(t *testing.T) { 106 var ( 107 testInitialInterval = 5 * time.Second 108 testRandomizationFactor = 0.1 109 testMultiplier = 0.5 110 testMaxInterval = testInitialInterval 111 testMinInterval = 100 * time.Millisecond 112 testMaxElapsedTime = time.Duration(0) 113 ) 114 115 exp := time_.NewExponentialBackOff( 116 time_.WithExponentialBackOffOptionInitialInterval(testInitialInterval), 117 time_.WithExponentialBackOffOptionRandomizationFactor(testRandomizationFactor), 118 time_.WithExponentialBackOffOptionMultiplier(testMultiplier), 119 time_.WithExponentialBackOffOptionMaxInterval(testMaxInterval), 120 time_.WithExponentialBackOffOptionMinInterval(testMinInterval), 121 time_.WithExponentialBackOffOptionMaxElapsedTime(testMaxElapsedTime), 122 ) 123 124 expectedResults := []time.Duration{500, 250, 125, 62, 31, 16, 8, 4, 2, 2, 2} 125 for i, d := range expectedResults { 126 expectedResults[i] = d * time.Millisecond 127 } 128 for _, expected := range expectedResults { 129 // assert.Equal(t, expected, exp.GetCurrentInterval()) 130 // Assert that the next backoff falls in the expected range. 131 var minInterval = expected - time.Duration(testRandomizationFactor*float64(expected)) 132 var maxInterval = expected + time.Duration(testRandomizationFactor*float64(expected)) 133 actualInterval, over := exp.NextBackOff() 134 t.Logf("over: %v, actualInterval: %v", over, actualInterval) 135 if !(minInterval <= actualInterval && actualInterval <= maxInterval) { 136 } 137 } 138 139 t.Logf("starting back...") 140 for _, expected := range expectedResults { 141 // assert.Equal(t, expected, exp.GetCurrentInterval()) 142 // Assert that the next backoff falls in the expected range. 143 var minInterval = expected - time.Duration(testRandomizationFactor*float64(expected)) 144 var maxInterval = expected + time.Duration(testRandomizationFactor*float64(expected)) 145 actualInterval, over := exp.PreBackOff() 146 t.Logf("over: %v, actualInterval: %v", over, actualInterval) 147 if !(minInterval <= actualInterval && actualInterval <= maxInterval) { 148 } 149 } 150 151 }