forked from divan/expvarmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expvars_test.go
79 lines (67 loc) · 1.57 KB
/
expvars_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
package main
import (
"os"
"testing"
"github.com/ezbuy/expvarmon/pkg/fetch"
)
const (
expvarsTestFile = "./expvars.json"
expvarsAdvTestFile = "./expvars_advanced.json"
)
func TestExpvars(t *testing.T) {
file, err := os.Open(expvarsTestFile)
if err != nil {
t.Fatalf("cannot open test file %v", err)
}
defer file.Close()
expvar, err := fetch.ParseExpvar(file)
if err != nil {
t.Fatal(err)
}
cmdline, err := expvar.GetStringArray("cmdline")
if err != nil {
t.Fatal(err)
}
if len(cmdline) != 3 {
t.Fatalf("Cmdline should have 3 items, but has %d", len(cmdline))
}
alloc, err := expvar.GetInt64(VarName("memstats.Alloc").ToSlice()...)
if err != nil {
t.Fatal(err)
}
if alloc == 0 {
t.Fatalf("Alloc should be greater than 0")
}
pauses, err := expvar.GetInt64Array(VarName("memstats.PauseNs").ToSlice()...)
if err != nil {
t.Fatal(err)
}
if len(pauses) == 0 {
t.Fatalf("Pauses length should be greater than 0")
}
}
func TestExpvarsAdvanced(t *testing.T) {
file, err := os.Open(expvarsAdvTestFile)
if err != nil {
t.Fatalf("cannot open test file %v", err)
}
defer file.Close()
expvar, err := fetch.ParseExpvar(file)
if err != nil {
t.Fatal(err)
}
goroutines, err := expvar.GetInt64("goroutines")
if err != nil {
t.Fatal(err)
}
if goroutines != 10 {
t.Fatalf("Expecting 'goroutines' to be %d, but got %d", 10, goroutines)
}
counterA, err := expvar.GetFloat64(VarName("counters.A").ToSlice()...)
if err != nil {
t.Fatal(err)
}
if counterA != 123.12 {
t.Fatalf("Expecting 'counters.A' to be %f, but got %f", 123.12, counterA)
}
}