-
Notifications
You must be signed in to change notification settings - Fork 19
/
default_client_test.go
68 lines (58 loc) · 1.67 KB
/
default_client_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
package retryablehttp_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httptrace"
"sync"
"sync/atomic"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/projectdiscovery/retryablehttp-go"
"github.com/stretchr/testify/require"
)
// This test is just to make sure that the default client is initialized
// correctly.
func Test_DefaultHttpClient(t *testing.T) {
require.NotNil(t, retryablehttp.DefaultHTTPClient)
resp, err := retryablehttp.DefaultHTTPClient.Get("https://scanme.sh")
require.Nil(t, err)
require.NotNil(t, resp)
}
func TestConnectionReuse(t *testing.T) {
opts := retryablehttp.DefaultOptionsSingle
client := retryablehttp.NewClient(opts)
router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "this is a test")
})
ts := httptest.NewServer(router)
defer ts.Close()
trace := &httptrace.ClientTrace{}
totalConns := &atomic.Uint32{}
trace.ConnectStart = func(network, addr string) {
_ = totalConns.Add(1)
}
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 20; i++ {
req, err := retryablehttp.NewRequest("GET", ts.URL, nil)
require.Nil(t, err)
req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
resp, err := client.Do(req)
require.Nil(t, err)
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
}()
}
wg.Wait()
// total number of connections depends on various factors
// like idle timeout and network condtions etc but in any case
// it should be less than 10
require.LessOrEqual(t, totalConns.Load(), uint32(10), "connection reuse failed")
}