-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
62 lines (53 loc) · 1.22 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
package main
import (
"errors"
"net/http"
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestKubeRetryTransport(t *testing.T) {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
t.Run("retry connection refused error", func(t *testing.T) {
r := require.New(t)
next := &mockRoundTripper{
err: syscall.ECONNREFUSED,
}
rt := kubeRetryTransport{
log: log,
next: next,
maxRetries: 3,
retryInterval: 100 * time.Millisecond,
}
_, err := rt.RoundTrip(nil)
r.EqualError(err, "connection refused")
r.Equal(int32(4), next.calls)
})
t.Run("do not retry non connection refused errors", func(t *testing.T) {
r := require.New(t)
next := &mockRoundTripper{
err: errors.New("ups"),
}
rt := kubeRetryTransport{
log: log,
next: next,
maxRetries: 3,
retryInterval: 100 * time.Millisecond,
}
_, err := rt.RoundTrip(nil)
r.EqualError(err, "ups")
r.Equal(int32(1), next.calls)
})
}
type mockRoundTripper struct {
err error
calls int32
}
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&m.calls, 1)
return nil, m.err
}