1
- package pester_test
1
+ package pester
2
2
3
3
import (
4
4
"fmt"
@@ -13,14 +13,13 @@ import (
13
13
14
14
"net/http"
15
15
"net/http/cookiejar"
16
-
17
- "github.com/sethgrid/pester"
16
+ "errors"
18
17
)
19
18
20
19
func TestConcurrentRequests (t * testing.T ) {
21
20
t .Parallel ()
22
21
23
- c := pester . New ()
22
+ c := New ()
24
23
c .Concurrency = 3
25
24
c .KeepLog = true
26
25
@@ -43,7 +42,7 @@ func TestConcurrentRequests(t *testing.T) {
43
42
func TestConcurrent2Retry0 (t * testing.T ) {
44
43
t .Parallel ()
45
44
46
- c := pester . New ()
45
+ c := New ()
47
46
c .Concurrency = 2
48
47
c .MaxRetries = 0
49
48
c .KeepLog = true
@@ -67,7 +66,7 @@ func TestConcurrent2Retry0(t *testing.T) {
67
66
func TestDefaultBackoff (t * testing.T ) {
68
67
t .Parallel ()
69
68
70
- c := pester . New ()
69
+ c := New ()
71
70
c .KeepLog = true
72
71
73
72
nonExistantURL := "http://localhost:9000/foo"
@@ -102,10 +101,90 @@ func TestDefaultBackoff(t *testing.T) {
102
101
103
102
}
104
103
104
+ func TestFormatError (t * testing.T ) {
105
+ t .Parallel ()
106
+ err := errors .New ("Get http://localhost:9000/foo: dial tcp 127.0.0.1:9000: getsockopt: connection refused" )
107
+ expected := "1491271979 Get [GET] http://localhost:9000/foo request-0 retry-2 error: " + err .Error ()+ "\n "
108
+
109
+ e := ErrEntry {
110
+ Time : time .Unix (1491271979 , 0 ),
111
+ Method : "Get" ,
112
+ URL : "http://localhost:9000/foo" ,
113
+ Verb : http .MethodGet ,
114
+ Request : 0 ,
115
+ Retry : 2 ,
116
+ Attempt : 1 ,
117
+ Err : err ,
118
+ }
119
+
120
+ c := New ()
121
+ formatted := c .FormatError (e )
122
+ if strings .Compare (expected , formatted ) != 0 {
123
+ t .Errorf ("\n Expected:\n %s\n Got:\n %s" , expected , formatted )
124
+ }
125
+ }
126
+
127
+ func TestCustomLogHook (t * testing.T ) {
128
+ t .Parallel ()
129
+
130
+ expectedRetries := 5
131
+ errorLines := []ErrEntry {}
132
+
133
+ c := New ()
134
+ //c.KeepLog = true
135
+ c .MaxRetries = expectedRetries
136
+ c .Backoff = func (_ int ) time.Duration {
137
+ return 10 * time .Microsecond
138
+ }
139
+
140
+ c .LogHook = func (e ErrEntry ) {
141
+ errorLines = append (errorLines , e )
142
+ }
143
+
144
+ nonExistantURL := "http://localhost:9000/foo"
145
+
146
+ _ , err := c .Get (nonExistantURL )
147
+ if err == nil {
148
+ t .Fatal ("expected to get an error" )
149
+ }
150
+ c .Wait ()
151
+
152
+ // in the event of an error, let's see what the logs were
153
+ if expectedRetries != len (errorLines ) {
154
+ t .Errorf ("Expected %d lines to be emitted. Got %d" , expectedRetries , errorLines )
155
+ }
156
+ }
157
+
158
+ func TestDefaultLogHook (t * testing.T ) {
159
+ t .Parallel ()
160
+
161
+ errorLines := 0
162
+
163
+ c := New ()
164
+ //c.KeepLog = true
165
+ c .MaxRetries = 5
166
+ c .Backoff = func (_ int ) time.Duration {
167
+ return 10 * time .Microsecond
168
+ }
169
+
170
+ nonExistantURL := "http://localhost:9000/foo"
171
+
172
+ _ , err := c .Get (nonExistantURL )
173
+ if err == nil {
174
+ t .Fatal ("expected to get an error" )
175
+ }
176
+ c .Wait ()
177
+
178
+ // in the event of an error, let's see what the logs were
179
+ if errorLines != 0 {
180
+ t .Errorf ("Expected 0 lines to be emitted. Got %d" , errorLines )
181
+ }
182
+ }
183
+
105
184
func TestLinearJitterBackoff (t * testing.T ) {
106
185
t .Parallel ()
107
- c := pester . New ()
108
- c .Backoff = pester . LinearJitterBackoff
186
+ c := New ()
187
+ c .Backoff = LinearJitterBackoff
109
188
c .KeepLog = true
110
189
111
190
nonExistantURL := "http://localhost:9000/foo"
@@ -142,9 +221,9 @@ func TestLinearJitterBackoff(t *testing.T) {
142
221
func TestExponentialBackoff (t * testing.T ) {
143
222
t .Parallel ()
144
223
145
- c := pester . New ()
224
+ c := New ()
146
225
c .MaxRetries = 4
147
- c .Backoff = pester . ExponentialBackoff
226
+ c .Backoff = ExponentialBackoff
148
227
c .KeepLog = true
149
228
150
229
nonExistantURL := "http://localhost:9000/foo"
@@ -193,7 +272,7 @@ func TestCookiesJarPersistence(t *testing.T) {
193
272
t .Fatal ("Cannot create cookiejar" , err )
194
273
}
195
274
196
- c := pester . New ()
275
+ c := New ()
197
276
c .Jar = jar
198
277
199
278
url := fmt .Sprintf ("http://localhost:%d" , port )
@@ -221,7 +300,7 @@ func TestEmbeddedClientTimeout(t *testing.T) {
221
300
hc := http .DefaultClient
222
301
hc .Timeout = clientTimeout
223
302
224
- c := pester . NewExtendedClient (hc )
303
+ c := NewExtendedClient (hc )
225
304
_ , err = c .Get (fmt .Sprintf ("http://localhost:%d/" , port ))
226
305
if err == nil {
227
306
t .Error ("expected a timeout error, did not get it" )
@@ -230,7 +309,7 @@ func TestEmbeddedClientTimeout(t *testing.T) {
230
309
231
310
func TestConcurrentRequestsNotRacyAndDontLeak_FailedRequest (t * testing.T ) {
232
311
goroStart := runtime .NumGoroutine ()
233
- c := pester . New ()
312
+ c := New ()
234
313
port , err := cookieServer ()
235
314
if err != nil {
236
315
t .Fatalf ("unable to start server %v" , err )
@@ -277,7 +356,7 @@ func TestConcurrentRequestsNotRacyAndDontLeak_FailedRequest(t *testing.T) {
277
356
278
357
func TestConcurrentRequestsNotRacyAndDontLeak_SuccessfulRequest (t * testing.T ) {
279
358
goroStart := runtime .NumGoroutine ()
280
- c := pester . New ()
359
+ c := New ()
281
360
nonExistantURL := "http://localhost:9000/foo"
282
361
conc := 5
283
362
errCh := make (chan error , conc )
@@ -343,7 +422,13 @@ func cookieServer() (int, error) {
343
422
log .Fatalf ("slow-server error %v" , err )
344
423
}
345
424
}()
346
- port , err := strconv .Atoi (strings .Replace (l .Addr ().String (), "[::]:" , "" , 1 ))
425
+
426
+ var port int
427
+ _ , sport , err := net .SplitHostPort (l .Addr ().String ())
428
+ if err == nil {
429
+ port , err = strconv .Atoi (sport )
430
+ }
431
+
347
432
if err != nil {
348
433
return - 1 , fmt .Errorf ("unable to determine port %v" , err )
349
434
}
@@ -365,9 +450,16 @@ func timeoutServer(timeout time.Duration) (int, error) {
365
450
log .Fatalf ("slow-server error %v" , err )
366
451
}
367
452
}()
368
- port , err := strconv .Atoi (strings .Replace (l .Addr ().String (), "[::]:" , "" , 1 ))
453
+
454
+ var port int
455
+ _ , sport , err := net .SplitHostPort (l .Addr ().String ())
456
+ if err == nil {
457
+ port , err = strconv .Atoi (sport )
458
+ }
459
+
369
460
if err != nil {
370
461
return - 1 , fmt .Errorf ("unable to determine port %v" , err )
371
462
}
463
+
372
464
return port , nil
373
465
}
0 commit comments