File: backoffs_test.go

package info (click to toggle)
golang-gopkg-eapache-go-resiliency.v1 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: makefile: 2
file content (85 lines) | stat: -rw-r--r-- 1,682 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package retrier

import (
	"testing"
	"time"
)

func TestConstantBackoff(t *testing.T) {
	b := ConstantBackoff(1, 10*time.Millisecond)
	if len(b) != 1 {
		t.Error("incorrect length")
	}
	for i := range b {
		if b[i] != 10*time.Millisecond {
			t.Error("incorrect value at", i)
		}
	}

	b = ConstantBackoff(10, 250*time.Hour)
	if len(b) != 10 {
		t.Error("incorrect length")
	}
	for i := range b {
		if b[i] != 250*time.Hour {
			t.Error("incorrect value at", i)
		}
	}
}

func TestExponentialBackoff(t *testing.T) {
	b := ExponentialBackoff(1, 10*time.Millisecond)
	if len(b) != 1 {
		t.Error("incorrect length")
	}
	if b[0] != 10*time.Millisecond {
		t.Error("incorrect value")
	}

	b = ExponentialBackoff(4, 1*time.Minute)
	if len(b) != 4 {
		t.Error("incorrect length")
	}
	if b[0] != 1*time.Minute {
		t.Error("incorrect value")
	}
	if b[1] != 2*time.Minute {
		t.Error("incorrect value")
	}
	if b[2] != 4*time.Minute {
		t.Error("incorrect value")
	}
	if b[3] != 8*time.Minute {
		t.Error("incorrect value")
	}
}

func TestLimitedExponentialBackoff(t *testing.T) {
	b := LimitedExponentialBackoff(1, 10*time.Millisecond, 11*time.Millisecond)
	if len(b) != 1 {
		t.Error("incorrect length")
	}
	if b[0] != 10*time.Millisecond {
		t.Error("incorrect value")
	}

	b = LimitedExponentialBackoff(5, 1*time.Minute, 4*time.Minute)
	if len(b) != 5 {
		t.Error("incorrect length")
	}
	if b[0] != 1*time.Minute {
		t.Error("incorrect value")
	}
	if b[1] != 2*time.Minute {
		t.Error("incorrect value")
	}
	if b[2] != 4*time.Minute {
		t.Error("incorrect value")
	}
	if b[3] != 4*time.Minute {
		t.Error("incorrect value")
	}
	if b[4] != 4*time.Minute {
		t.Error("incorrect value")
	}
}