File: jitter_backoff_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (61 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package retry

import (
	"math"
	"testing"
	"time"

	"github.com/aws/aws-sdk-go-v2/internal/timeconv"
)

func TestExponentialJitterBackoff_AttemptDelay(t *testing.T) {
	maxB := 1 - 1/float64(1<<53)

	cases := map[string]struct {
		MaxBackoff time.Duration
		RandFloat  func() (float64, error)
		Attempt    int
		Expect     time.Duration
	}{
		"min delay floor": {
			MaxBackoff: 20 * time.Second,
			RandFloat:  func() (float64, error) { return 0, nil },
			Attempt:    1,
			Expect:     0,
		},
		"min delay ceiling": {
			MaxBackoff: 20 * time.Second,
			RandFloat:  func() (float64, error) { return maxB, nil },
			Attempt:    1,
			Expect:     timeconv.FloatSecondsDur(maxB * float64(2)),
		},
		"attempt delay": {
			MaxBackoff: 20 * time.Second,
			RandFloat:  func() (float64, error) { return 0.5, nil },
			Attempt:    2,
			Expect:     timeconv.FloatSecondsDur(0.5 * float64(1<<2)),
		},
		"max delay": {
			MaxBackoff: 20 * time.Second,
			RandFloat:  func() (float64, error) { return maxB, nil },
			Attempt:    2147483647,
			Expect:     timeconv.FloatSecondsDur(maxB * math.Exp2(math.Log2(20))),
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			j := NewExponentialJitterBackoff(c.MaxBackoff)
			j.randFloat64 = c.RandFloat

			d, err := j.BackoffDelay(c.Attempt, nil)
			if err != nil {
				t.Fatalf("expect not error, %v", err)
			}

			if e, a := c.Expect, d; e != a {
				t.Errorf("expect %v delay, got %v", e, a)
			}
		})
	}
}