-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemaphore_client_test.go
74 lines (54 loc) · 1.46 KB
/
semaphore_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
69
70
71
72
73
74
package gosrm
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewHTTPClient(t *testing.T) {
cfg := HTTPClientConfig{}
c := NewHTTPClient(cfg).(httpClient)
assert.Equal(t, http.DefaultClient, c.client)
assert.Equal(t, 0, cap(c.pool))
cfg.MaxConcurrency = 100
c = NewHTTPClient(cfg).(httpClient)
assert.Equal(t, 100, cap(c.pool))
cfg.HTTPClient = &http.Client{}
c = NewHTTPClient(cfg).(httpClient)
assert.Equal(t, cfg.HTTPClient, c.client)
}
func TestHTTPClient_acquire_and_release(t *testing.T) {
cfg := HTTPClientConfig{}
c := NewHTTPClient(cfg).(httpClient)
for i := 0; i < 5; i++ {
c.acquire()
}
// chan is not used since it's disabled.
assert.Len(t, c.pool, 0)
c.release()
assert.Len(t, c.pool, 0)
cfg.MaxConcurrency = 2
c = NewHTTPClient(cfg).(httpClient)
for i := 0; i < int(cfg.MaxConcurrency); i++ {
c.acquire()
}
// chan is full.
assert.Len(t, c.pool, 2)
for i := cfg.MaxConcurrency; i > 0; i-- {
c.release()
assert.Len(t, c.pool, int(i-1))
}
// chan is empty.
assert.Len(t, c.pool, 0)
}
func TestHTTPClient_Do(t *testing.T) {
client := NewHTTPClient(HTTPClientConfig{MaxConcurrency: 1})
testsrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
}))
req, err := http.NewRequest("GET", testsrv.URL, nil)
assert.NoError(t, err)
res, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 201, res.StatusCode)
}