-
Notifications
You must be signed in to change notification settings - Fork 15
/
helper_test.go
112 lines (91 loc) · 2.83 KB
/
helper_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
package nakadi
import (
"encoding/json"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewHTTPClient(t *testing.T) {
timeout := 20 * time.Second
client := newHTTPClient(timeout, func(transport *http.Transport) http.RoundTripper { return transport })
require.NotNil(t, client)
assert.Equal(t, timeout, client.Timeout)
}
func TestNewHTTPStream(t *testing.T) {
timeout := 20 * time.Second
client := newHTTPStream(timeout)
require.NotNil(t, client)
assert.Equal(t, 0*time.Second, client.Timeout)
}
func TestProblemJSON_Marshal(t *testing.T) {
problem := &problemJSON{}
expected := helperLoadTestData(t, "problem-json.json", problem)
serialized, err := json.Marshal(problem)
require.NoError(t, err)
assert.JSONEq(t, string(expected), string(serialized))
}
func TestErrorJSON_Marshal(t *testing.T) {
errJSON := &errorJSON{}
expected := helperLoadTestData(t, "error-json.json", errJSON)
serialized, err := json.Marshal(errJSON)
require.NoError(t, err)
assert.JSONEq(t, string(expected), string(serialized))
}
func TestBackOffConfiguration_createBackOff(t *testing.T) {
t.Run("stop backoff", func(t *testing.T) {
backOffConf := backOffConfiguration{
Retry: false,
InitialRetryInterval: 1 * time.Millisecond,
MaxRetryInterval: 1 * time.Second,
MaxElapsedTime: 1 * time.Minute}
backOff := backOffConf.create()
assert.IsType(t, &backoff.StopBackOff{}, backOff)
})
t.Run("exponential backoff", func(t *testing.T) {
backOffConf := backOffConfiguration{
Retry: true,
InitialRetryInterval: 1 * time.Millisecond,
MaxRetryInterval: 1 * time.Second,
MaxElapsedTime: 1 * time.Minute}
backOff := backOffConf.create()
require.IsType(t, &backoff.ExponentialBackOff{}, backOff)
expBackOff := backOff.(*backoff.ExponentialBackOff)
assert.Equal(t, 1*time.Millisecond, expBackOff.InitialInterval)
assert.Equal(t, 1*time.Second, expBackOff.MaxInterval)
assert.Equal(t, 1*time.Minute, expBackOff.MaxElapsedTime)
})
}
func helperLoadTestData(t *testing.T, name string, target interface{}) []byte {
path := filepath.Join("testdata", name)
bytes, err := os.ReadFile(path)
require.NoError(t, err)
if target != nil {
err = json.Unmarshal(bytes, target)
require.NoError(t, err)
}
return bytes
}
func helperMakeCounter(n int) chan int {
counter := make(chan int)
go func() {
for i := 0; i <= n; i++ {
counter <- i
}
close(counter)
}()
return counter
}
// brokenBodyReader is an implementation of ReadCloser interface to be used for
// mocking errors while reading from body
type brokenBodyReader struct{}
func (brokenBodyReader) Read(_ []byte) (n int, err error) {
return 0, assert.AnError
}
func (brokenBodyReader) Close() error {
return nil
}