-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin_test.go
62 lines (50 loc) · 1.46 KB
/
plugin_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 sugar
import (
"github.com/stretchr/testify/assert"
"net"
"net/http"
"testing"
"time"
)
type mockTransporter struct {
count int
response *http.Response
error error
}
func (t *mockTransporter) Do(req *http.Request) (*http.Response, error) {
t.count++
return t.response, t.error
}
func TestRetryer_Retry_If_Transporter_Returns_An_Error_Of_NetError(t *testing.T) {
const attempts = 3
p := PluginFunc(Retryer(attempts, time.Duration(1)*time.Second, 1.5, time.Duration(3)*time.Second))
m := &mockTransporter{response: nil, error: &net.OpError{}}
c := Context{
transporter: m,
plugins: []Plugin{p},
}
c.Next()
assert.Equal(t, attempts, m.count)
}
func TestRetryer_Not_Retry_If_Transporter_Returns_An_Error_Which_Is_Not_Instance_Of_NetError(t *testing.T) {
const attempts = 1
p := PluginFunc(Retryer(attempts, time.Duration(1)*time.Second, 1.5, time.Duration(3)*time.Second))
m := &mockTransporter{response: nil, error: EncoderNotFound}
c := Context{
transporter: m,
plugins: []Plugin{p},
}
c.Next()
assert.Equal(t, attempts, m.count)
}
func TestRetryer_Not_Retry_If_Request_Is_Handled_Correctly(t *testing.T) {
const attempts = 1
p := PluginFunc(Retryer(attempts, time.Duration(1)*time.Second, 1.5, time.Duration(3)*time.Second))
m := &mockTransporter{response: &http.Response{StatusCode: http.StatusOK}, error: nil}
c := Context{
transporter: m,
plugins: []Plugin{p},
}
c.Next()
assert.Equal(t, attempts, m.count)
}