-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain_test.go
140 lines (127 loc) · 5.54 KB
/
main_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"net"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
const (
TEST_MCROUTER = "localhost:5000"
)
// Expect incoming message: stats all
// Return Example stats
func handleRequestStats(conn net.Conn) {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
ret := []byte("STAT start_time 1\r\nSTAT version 0.0\r\nSTAT fibers_allocated 1\r\nSTAT fibers_pool_size 1\r\nSTAT commandargs --debug-fifo-root /var/lib/mcrouter/fifos --test-mode\r\nEND\r\n")
conn.Write(ret)
conn.Close()
}
// Expect incoming message: stats all
// Return Example stats
func handleRequestServerStats(conn net.Conn, full bool) {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
ret := []byte("STAT 10.1.1.1:11211:ascii:plain:notcompressed-1000 avg_latency_us:302.991 pending_reqs:0 inflight_reqs:0 avg_retrans_ratio:0 max_retrans_ratio:0 min_retrans_ratio:0 up:5\r\n" +
"STAT 10.1.1.2:11211:ascii:plain:notcompressed-1000 avg_latency_us:303.4 pending_reqs:0 inflight_reqs:0 avg_retrans_ratio:2 max_retrans_ratio:10 min_retrans_ratio:0 up:5\r\n" +
"END\r\n")
if full {
ret = []byte("STAT 10.1.1.1:11211:ascii:plain:notcompressed-1000 avg_latency_us:302.991 pending_reqs:0 inflight_reqs:0 avg_retrans_ratio:0 max_retrans_ratio:0 min_retrans_ratio:0 up:5 soft_tko; " +
"deleted:4875 touched:33069 found:112675373 notfound:3493823 notstored:149776 stored:3250883 exists:2653 remote_error:32\r\n" +
"STAT 10.1.1.2:11211:ascii:plain:notcompressed-1000 avg_latency_us:303.4 pending_reqs:0 inflight_reqs:0 avg_retrans_ratio:2 max_retrans_ratio:10 min_retrans_ratio:0 up:5 hard_tko; " +
"deleted:42 touched:3304 found:1175373 notfound:33823 notstored:0 stored:3250883 remote_error:55\r\n" +
"END\r\n")
}
conn.Write(ret)
conn.Close()
}
func TestStatsParsing(t *testing.T) {
Convey("Given a remote mcrouter stats endpoint", t, func() {
server, client := net.Pipe()
go func() {
go handleRequestStats(server)
}()
Convey("When scraped by our client", func() {
stats, err := getStats(client)
if err != nil {
t.Fatal(err)
}
Convey("It should parse the stats into a string map", func() {
expected := make(map[string]string)
expected["start_time"] = "1"
expected["version"] = "0.0"
expected["fibers_allocated"] = "1"
expected["fibers_pool_size"] = "1"
expected["commandargs"] = "--debug-fifo-root /var/lib/mcrouter/fifos --test-mode"
So(stats, ShouldResemble, expected)
})
})
})
}
func TestFullServerStatsParsing(t *testing.T) {
Convey("Given a remote mcrouter stats server endpoint", t, func() {
server, client := net.Pipe()
go func() {
go handleRequestServerStats(server, true)
}()
Convey("When scraped by our client", func() {
stats, err := getServerStats(client)
if err != nil {
t.Fatal(err)
}
Convey("It should parse the stats into a string map", func() {
expected := make(map[string]map[string]string)
expected["10.1.1.1:11211:ascii:plain:notcompressed-1000"] = map[string]string{
"avg_latency_us": "302.991", "avg_retrans_ratio": "0", "connect_timeout": "0", "deleted": "4875",
"exists": "2653", "found": "112675373", "inflight_reqs": "0", "max_retrans_ratio": "0", "min_retrans_ratio": "0",
"notfound": "3493823", "notstored": "149776", "pending_reqs": "0", "remote_error": "32", "stored": "3250883",
"timeout": "0", "soft_tko": "1", "hard_tko": "0", "touched": "33069", "up": "5",
}
expected["10.1.1.2:11211:ascii:plain:notcompressed-1000"] = map[string]string{
"avg_latency_us": "303.4", "avg_retrans_ratio": "2", "connect_timeout": "0", "deleted": "42", "exists": "0",
"found": "1175373", "inflight_reqs": "0", "max_retrans_ratio": "10", "min_retrans_ratio": "0", "notfound": "33823",
"notstored": "0", "pending_reqs": "0", "remote_error": "55", "stored": "3250883", "timeout": "0", "soft_tko": "0",
"hard_tko": "1", "touched": "3304", "up": "5",
}
So(stats, ShouldResemble, expected)
})
})
})
}
func TestServerStatsParsingAfterMcrouterBootstrap(t *testing.T) {
Convey("Given a remote mcrouter (without any commands processed yet) stats server server endpoint", t, func() {
server, client := net.Pipe()
go func() {
go handleRequestServerStats(server, false)
}()
Convey("When scraped by our client", func() {
stats, err := getServerStats(client)
if err != nil {
t.Fatal(err)
}
Convey("It should parse the stats into a string map", func() {
expected := make(map[string]map[string]string)
expected["10.1.1.1:11211:ascii:plain:notcompressed-1000"] = map[string]string{
"avg_latency_us": "302.991", "avg_retrans_ratio": "0", "connect_timeout": "0", "deleted": "0",
"exists": "0", "found": "0", "inflight_reqs": "0", "max_retrans_ratio": "0",
"min_retrans_ratio": "0", "notfound": "0", "notstored": "0", "pending_reqs": "0",
"remote_error": "0", "stored": "0", "timeout": "0", "soft_tko": "0", "hard_tko": "0", "touched": "0",
"up": "5",
}
expected["10.1.1.2:11211:ascii:plain:notcompressed-1000"] = map[string]string{
"avg_latency_us": "303.4", "avg_retrans_ratio": "2", "connect_timeout": "0", "deleted": "0", "exists": "0",
"found": "0", "inflight_reqs": "0", "max_retrans_ratio": "10", "min_retrans_ratio": "0", "notfound": "0",
"notstored": "0", "pending_reqs": "0", "remote_error": "0", "stored": "0", "timeout": "0", "soft_tko": "0",
"hard_tko": "0", "touched": "0", "up": "5",
}
So(stats, ShouldResemble, expected)
})
})
})
}