forked from sethgrid/pester
/
main_test.go
151 lines (119 loc) · 3.08 KB
/
main_test.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package pester_test
import (
"testing"
"github.com/sethgrid/pester"
)
func TestConcurrentRequests(t *testing.T) {
t.Parallel()
c := pester.New()
c.Concurrency = 4
c.KeepLog = true
nonExistantURL := "http://localhost:9000/foo"
_, err := c.Get(nonExistantURL)
if err == nil {
t.Fatal("expected to get an error")
}
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())
if got, want := len(c.ErrLog), c.Concurrency*c.MaxRetries; got != want {
t.Error("got %d attempts, want %d", got, want)
}
}
func TestDefaultBackoff(t *testing.T) {
t.Parallel()
c := pester.New()
c.KeepLog = true
nonExistantURL := "http://localhost:9000/foo"
_, err := c.Get(nonExistantURL)
if err == nil {
t.Fatal("expected to get an error")
}
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())
if got, want := c.Concurrency, 1; got != want {
t.Error("got %d, want %d for concurrency", got, want)
}
if got, want := len(c.ErrLog), c.MaxRetries; got != want {
t.Fatalf("got %d errors, want %d", got, want)
}
var startTime int64
for i, e := range c.ErrLog {
if i == 0 {
startTime = e.Time.Unix()
continue
}
if got, want := e.Time.Unix(), startTime+int64(i); got != want {
t.Errorf("got time %d, want %d (%d greater than start time %d)", got, want, i, startTime)
}
}
}
func TestLinearJitterBackoff(t *testing.T) {
t.Parallel()
c := pester.New()
c.Backoff = pester.LinearJitterBackoff
c.KeepLog = true
nonExistantURL := "http://localhost:9000/foo"
_, err := c.Get(nonExistantURL)
if err == nil {
t.Fatal("expected to get an error")
}
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())
var startTime int64
var delta int64
for i, e := range c.ErrLog {
switch i {
case 0:
startTime = e.Time.Unix()
case 1:
delta += 1
case 2:
delta += 2
case 3:
delta += 3
}
if got, want := e.Time.Unix(), startTime+delta; withinEpsilon(got, want, 0.0) {
t.Errorf("got time %d, want %d (within epsilon of start time %d)", got, want, startTime)
}
}
}
func TestExponentialBackoff(t *testing.T) {
t.Parallel()
c := pester.New()
c.MaxRetries = 4
c.Backoff = pester.ExponentialBackoff
c.KeepLog = true
nonExistantURL := "http://localhost:9000/foo"
_, err := c.Get(nonExistantURL)
if err == nil {
t.Fatal("expected to get an error")
}
// in the event of an error, let's see what the logs were
t.Log("\n", c.LogString())
if got, want := len(c.ErrLog), c.MaxRetries; got != want {
t.Fatalf("got %d errors, want %d", got, want)
}
var startTime int64
var delta int64
for i, e := range c.ErrLog {
switch i {
case 0:
startTime = e.Time.Unix()
case 1:
delta += 1
case 2:
delta += 2
case 3:
delta += 4
}
if got, want := e.Time.Unix(), startTime+delta; got != want {
t.Errorf("got time %d, want %d (%d greater than start time %d)", got, want, delta, startTime)
}
}
}
func withinEpsilon(got, want int64, epslion float64) bool {
if want <= int64(epslion*float64(got)) || want >= int64(epslion*float64(got)) {
return false
}
return true
}