Debian Codegraph @

registry/remote/retry/policy_test.go

raw
1 /*
2 Copyright The ORAS Authors.
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
16 package retry
17
18 import (
19 "testing"
20 "time"
21 )
22
23 func Test_ExponentialBackoff(t *testing.T) {
24 testCases := []struct {
25 name string
26 attempt int
27 expectedBackoff time.Duration
28 }{
29 {
30 name: "attempt 0 should have a backoff of 0,25s ± 10%",
31 attempt: 0, expectedBackoff: 250 * time.Millisecond,
32 },
33 {
34 name: "attempt 1 should have a backoff of 0,5s ± 10%",
35 attempt: 1, expectedBackoff: 500 * time.Millisecond,
36 },
37 {
38 name: "attempt 2 should have a backoff of 1s ± 10%",
39 attempt: 2, expectedBackoff: 1 * time.Second,
40 },
41 {
42 name: "attempt 3 should have a backoff of 2s ± 10%",
43 attempt: 3, expectedBackoff: 2 * time.Second,
44 },
45 {
46 name: "attempt 4 should have a backoff of 4s ± 10%",
47 attempt: 4, expectedBackoff: 4 * time.Second,
48 },
49 {
50 name: "attempt 5 should have a backoff of 8s ± 10%",
51 attempt: 5, expectedBackoff: 8 * time.Second,
52 },
53 }
54
55 for _, tc := range testCases {
56 t.Run(tc.name, func(t *testing.T) {
57 b := DefaultBackoff(tc.attempt, nil)
58 base := float64(tc.expectedBackoff)
59 if !(b >= time.Duration(base*0.9) && b <= time.Duration(base+base*0.1)) {
60 t.Errorf("expected backoff to be %s + jitter, got %s", time.Duration(base), b)
61 }
62 })
63 }
64 }