-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhttp_worker_test.go
More file actions
81 lines (72 loc) · 2.06 KB
/
http_worker_test.go
File metadata and controls
81 lines (72 loc) · 2.06 KB
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
// http_worker_test.go
// Test cases for HttpbenchWorker
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestHttpbenchWorkerDo verifies that the worker performs N requests and aggregates results properly.
func TestHttpbenchWorkerDo(t *testing.T) {
// Setup an echo server
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
defer srv.Close()
time.Sleep(100 * time.Millisecond)
params := HttpbenchParameters{
Url: srv.URL,
RequestMethod: http.MethodGet,
RequestBody: "",
RequestBodyType: "",
N: 10,
C: 2,
Timeout: 1000 * time.Millisecond,
Qps: 0,
SequenceId: 1,
RequestType: protocolHTTP1,
}
w := HttpbenchWorker{stopChan: make(chan bool, 1)}
w.Start(params)
res := w.GetResult()
if len(res.ErrorDist) != 0 {
t.Errorf("expected no errors; got %v", res.ErrorDist)
}
}
// TestHttpbenchWorkerStop verifies that Stop aborts the worker before all requests complete.
func TestHttpbenchWorkerStop(t *testing.T) {
*verbose = 0
// Setup server that delays response
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
w.Write([]byte("ok"))
}))
defer srv.Close()
time.Sleep(100 * time.Millisecond)
params := HttpbenchParameters{
Url: srv.URL,
RequestMethod: http.MethodGet,
RequestBody: "",
RequestBodyType: "",
N: 100,
C: 1,
Timeout: 1000 * time.Millisecond,
Qps: 0,
SequenceId: 2,
RequestType: protocolHTTP1,
}
w := HttpbenchWorker{stopChan: make(chan bool, 1)}
go w.Start(params)
// Let some requests proceed
time.Sleep(1 * time.Second)
err := w.Stop()
if err != nil {
t.Errorf("unexpected error on Stop: %v", err)
}
res := w.GetResult()
// Should complete fewer requests than requested
if res.LatsTotal >= int64(params.N) {
t.Errorf("expected fewer than %d requests; got %d", params.N, res.LatsTotal)
}
}