-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
94 lines (77 loc) · 2.01 KB
/
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package locust
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
const (
locusturl = "http://localhost:8089"
locustStatsResponce = `{
"current_response_time_percentile_50": 11,
"current_response_time_percentile_95": 22,
"errors": [],
"fail_ratio": 0.31311475409836065,
"state": "running",
"stats": [],
"total_rps": 9.9,
"user_count": 5
}`
locustTestStoppedResponce = `{
"message": "Test stopped",
"success": true
}`
locustTestStartedResponce = `{
"message": "Swarming started",
"success": true
}`
)
func TestNewClientURLSetting(t *testing.T) {
c, err := New(locusturl)
assert.Nil(t, err)
url := c.BaseURL.String()
assert.Equal(t, locusturl, url)
}
func TestGenerateLoad(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
fmt.Fprint(w, locustTestStartedResponce)
}))
// Close the server when test finishes.
defer server.Close()
c, err := New(server.URL)
assert.Nil(t, err)
s, err := c.GenerateLoad(5, 1)
assert.Nil(t, err)
assert.Equal(t, "Swarming started", s.Message)
assert.Equal(t, true, s.Success)
}
func TestStopLoad(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
fmt.Fprint(w, locustTestStoppedResponce)
}))
// Close the server when test finishes.
defer server.Close()
c, err := New(server.URL)
assert.Nil(t, err)
s, err := c.StopLoad()
assert.Nil(t, err)
assert.Equal(t, "Test stopped", s.Message)
assert.Equal(t, true, s.Success)
}
func TestGetStatus(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
fmt.Fprint(w, locustStatsResponce)
}))
// Close the server when test finishes.
defer server.Close()
client, err := New(server.URL)
assert.Nil(t, err)
s, err := client.Stats()
assert.Nil(t, err)
assert.Equal(t, 5, s.UserCount)
assert.Equal(t, "running", s.State)
}