|
| 1 | +// Copyright 2025 The etcd Authors |
| 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 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package clientv3 |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "math" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestExpBackoff(t *testing.T) { |
| 27 | + testCases := []struct { |
| 28 | + generation uint |
| 29 | + exponent float64 |
| 30 | + minDelay time.Duration |
| 31 | + maxDelay time.Duration |
| 32 | + expectedBackoff time.Duration |
| 33 | + }{ |
| 34 | + // exponential backoff with 2.0 exponent |
| 35 | + {generation: 0, exponent: 2.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 100 * time.Millisecond}, |
| 36 | + {generation: 1, exponent: 2.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 200 * time.Millisecond}, |
| 37 | + {generation: 2, exponent: 2.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 400 * time.Millisecond}, |
| 38 | + {generation: 3, exponent: 2.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 500 * time.Millisecond}, |
| 39 | + {generation: math.MaxUint, exponent: 2.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 500 * time.Millisecond}, |
| 40 | + |
| 41 | + // exponential backoff with 1.0 exponent |
| 42 | + {generation: 0, exponent: 1.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 100 * time.Millisecond}, |
| 43 | + {generation: 1, exponent: 1.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 100 * time.Millisecond}, |
| 44 | + {generation: 2, exponent: 1.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 100 * time.Millisecond}, |
| 45 | + {generation: 3, exponent: 1.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 100 * time.Millisecond}, |
| 46 | + {generation: math.MaxUint, exponent: 1.0, minDelay: 100 * time.Millisecond, maxDelay: 500 * time.Millisecond, expectedBackoff: 100 * time.Millisecond}, |
| 47 | + } |
| 48 | + |
| 49 | + for _, testCase := range testCases { |
| 50 | + testName := fmt.Sprintf("%+v", testCase) |
| 51 | + t.Run(testName, func(t *testing.T) { |
| 52 | + backoff := expBackoff(testCase.generation, testCase.exponent, testCase.minDelay, testCase.maxDelay) |
| 53 | + require.InDelta(t, backoff, testCase.expectedBackoff, 0.01) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
0 commit comments