2015-04-21 17:51:01 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-04-25 20:54:17 +08:00
|
|
|
const (
|
|
|
|
expvarsTestFile = "./expvars.json"
|
|
|
|
expvarsAdvTestFile = "./expvars_advanced.json"
|
|
|
|
)
|
2015-04-21 17:51:01 +08:00
|
|
|
|
|
|
|
func TestExpvars(t *testing.T) {
|
|
|
|
file, err := os.Open(expvarsTestFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot open test file %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
expvar, err := ParseExpvar(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 19:37:28 +08:00
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2015-05-01 23:48:34 +08:00
|
|
|
alloc, err := expvar.GetInt64(VarName("memstats.Alloc").ToSlice()...)
|
2015-05-01 21:49:19 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if alloc == 0 {
|
|
|
|
t.Fatalf("Alloc should be greater than 0")
|
|
|
|
}
|
2015-04-21 17:51:01 +08:00
|
|
|
}
|
2015-04-25 20:54:17 +08:00
|
|
|
|
|
|
|
func TestExpvarsAdvanced(t *testing.T) {
|
|
|
|
file, err := os.Open(expvarsAdvTestFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot open test file %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2015-05-01 19:37:28 +08:00
|
|
|
|
2015-05-02 01:28:51 +08:00
|
|
|
expvar, err := ParseExpvar(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 19:37:28 +08:00
|
|
|
|
2015-05-02 01:28:51 +08:00
|
|
|
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)
|
|
|
|
}
|
2015-05-01 19:37:28 +08:00
|
|
|
|
2015-05-02 01:52:17 +08:00
|
|
|
counterA, err := expvar.GetFloat64(VarName("counters.A").ToSlice()...)
|
2015-05-02 01:28:51 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-02 01:52:17 +08:00
|
|
|
if counterA != 123.12 {
|
|
|
|
t.Fatalf("Expecting 'counters.A' to be %f, but got %f", 123.12, counterA)
|
2015-05-02 01:28:51 +08:00
|
|
|
}
|
2015-04-25 20:54:17 +08:00
|
|
|
}
|