forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentsdb_test.go
66 lines (59 loc) · 1.66 KB
/
opentsdb_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
package metrics
import (
"fmt"
"net"
"os"
"strings"
"testing"
"time"
)
func ExampleOpenTSDB() {
addr, _ := net.ResolveTCPAddr("net", ":2003")
go OpenTSDB(DefaultRegistry, 1*time.Second, "some.prefix", addr)
}
func ExampleOpenTSDBWithConfig() {
addr, _ := net.ResolveTCPAddr("net", ":2003")
go OpenTSDBWithConfig(OpenTSDBConfig{
Addr: addr,
Registry: DefaultRegistry,
FlushInterval: 1 * time.Second,
DurationUnit: time.Millisecond,
})
}
func TestExampleOpenTSB(t *testing.T) {
r := NewOrderedRegistry()
NewRegisteredGaugeInfo("foo", r).Update(GaugeInfoValue{"chain_id": "5"})
NewRegisteredGaugeFloat64("pi", r).Update(3.14)
NewRegisteredCounter("months", r).Inc(12)
NewRegisteredCounterFloat64("tau", r).Inc(1.57)
NewRegisteredMeter("elite", r).Mark(1337)
NewRegisteredTimer("second", r).Update(time.Second)
NewRegisteredCounterFloat64("tau", r).Inc(1.57)
NewRegisteredCounterFloat64("tau", r).Inc(1.57)
w := new(strings.Builder)
(&OpenTSDBConfig{
Registry: r,
DurationUnit: time.Millisecond,
Prefix: "pre",
}).writeRegistry(w, 978307200, "hal9000")
wantB, err := os.ReadFile("./testdata/opentsb.want")
if err != nil {
t.Fatal(err)
}
if have, want := w.String(), string(wantB); have != want {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", have, want)
t.Logf("have vs want:\n%v", findFirstDiffPos(have, want))
}
}
func findFirstDiffPos(a, b string) string {
yy := strings.Split(b, "\n")
for i, x := range strings.Split(a, "\n") {
if i >= len(yy) {
return fmt.Sprintf("have:%d: %s\nwant:%d: <EOF>", i, x, i)
}
if y := yy[i]; x != y {
return fmt.Sprintf("have:%d: %s\nwant:%d: %s", i, x, i, y)
}
}
return ""
}