-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbk_mocks_test.go
127 lines (102 loc) · 2.15 KB
/
bk_mocks_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
package bk
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type httpclient struct {
delay time.Duration
requests int
status int
retries int
attempts int
concurrency int
cancel bool
}
func (client *httpclient) RoundTrip(r *http.Request) (*http.Response, error) {
return client.Do(r)
}
func (client *httpclient) Do(*http.Request) (*http.Response, error) {
if client.delay > 0 {
time.Sleep(client.delay)
}
status := client.status
if client.retries > 0 {
client.attempts++
if client.attempts < client.retries {
status = http.StatusBadRequest
}
}
return &http.Response{
StatusCode: status,
Body: &fakeReadCloser{},
}, nil
}
type fakeReadCloser struct{}
func (rc *fakeReadCloser) Read([]byte) (int, error) { return 0, io.EOF }
func (rc *fakeReadCloser) Close() error { return nil }
type badclient struct {
panic bool
delay time.Duration
requests int
status int
retries int
attempts int
concurrency int
}
func (client *badclient) RoundTrip(r *http.Request) (*http.Response, error) {
return client.Do(r)
}
func (client *badclient) Do(*http.Request) (*http.Response, error) {
if client.panic {
panic("panic")
}
return nil, errors.New("error")
}
type tstruct struct {
error bool
}
func (t *tstruct) correct(err error, _ bool) error {
if t.error && err == nil {
return errors.New("expected error but success instead")
}
if !t.error && err != nil {
return fmt.Errorf("expected success but errored instead | %s", err)
}
return nil
}
func newGetReqWOutCtx() *http.Request {
r, _ := http.NewRequest(
http.MethodGet,
"",
strings.NewReader(""),
)
return r
}
func newGetReqWCtx() *http.Request {
r, _ := http.NewRequestWithContext(
context.TODO(),
http.MethodGet,
"",
strings.NewReader(""),
)
return r
}
type passthrough struct {
ctx context.Context
out chan *http.Request
}
func (pass *passthrough) RoundTrip(r *http.Request) (*http.Response, error) {
return pass.Do(r)
}
func (pass *passthrough) Do(r *http.Request) (*http.Response, error) {
select {
case <-pass.ctx.Done():
case pass.out <- r:
}
return nil, nil
}