-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_test.go
83 lines (66 loc) · 2.05 KB
/
stats_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
package nsqd
import (
"encoding/json"
"fmt"
"os"
"strconv"
"testing"
"time"
"github.com/mreiferson/go-snappystream"
)
func TestStats(t *testing.T) {
opts := NewOptions()
opts.Logger = newTestLogger(t)
tcpAddr, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_stats" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := NewMessage(<-nsqd.idChan, []byte("test body"))
topic.PutMessage(msg)
conn, err := mustConnectNSQD(tcpAddr)
equal(t, err, nil)
defer conn.Close()
identify(t, conn, nil, frameTypeResponse)
sub(t, conn, topicName, "ch")
stats := nsqd.GetStats()
t.Logf("stats: %+v", stats)
equal(t, len(stats), 1)
equal(t, len(stats[0].Channels), 1)
equal(t, len(stats[0].Channels[0].Clients), 1)
}
func TestClientAttributes(t *testing.T) {
userAgent := "Test User Agent"
opts := NewOptions()
opts.Logger = newTestLogger(t)
opts.Verbose = true
opts.SnappyEnabled = true
tcpAddr, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
conn, err := mustConnectNSQD(tcpAddr)
equal(t, err, nil)
defer conn.Close()
data := identify(t, conn, map[string]interface{}{
"snappy": true,
"user_agent": userAgent,
}, frameTypeResponse)
resp := struct {
Snappy bool `json:"snappy"`
UserAgent string `json:"user_agent"`
}{}
err = json.Unmarshal(data, &resp)
equal(t, err, nil)
equal(t, resp.Snappy, true)
r := snappystream.NewReader(conn, snappystream.SkipVerifyChecksum)
w := snappystream.NewWriter(conn)
readValidate(t, r, frameTypeResponse, "OK")
topicName := "test_client_attributes" + strconv.Itoa(int(time.Now().Unix()))
sub(t, readWriter{r, w}, topicName, "ch")
testURL := fmt.Sprintf("http://127.0.0.1:%d/stats?format=json", httpAddr.Port)
statsData, err := API(testURL)
equal(t, err, nil)
client := statsData.Get("topics").GetIndex(0).Get("channels").GetIndex(0).Get("clients").GetIndex(0)
equal(t, client.Get("user_agent").MustString(), userAgent)
equal(t, client.Get("snappy").MustBool(), true)
}