aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/gauge_test.go
blob: 1f2603d33985c2e6bddcd07662c79168e499c11c (plain) (blame)
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
package metrics

import (
    "fmt"
    "testing"
)

func BenchmarkGuage(b *testing.B) {
    g := NewGauge()
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        g.Update(int64(i))
    }
}

func TestGauge(t *testing.T) {
    g := NewGauge()
    g.Update(int64(47))
    if v := g.Value(); 47 != v {
        t.Errorf("g.Value(): 47 != %v\n", v)
    }
}

func TestGaugeSnapshot(t *testing.T) {
    g := NewGauge()
    g.Update(int64(47))
    snapshot := g.Snapshot()
    g.Update(int64(0))
    if v := snapshot.Value(); 47 != v {
        t.Errorf("g.Value(): 47 != %v\n", v)
    }
}

func TestGetOrRegisterGauge(t *testing.T) {
    r := NewRegistry()
    NewRegisteredGauge("foo", r).Update(47)
    if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
        t.Fatal(g)
    }
}

func TestFunctionalGauge(t *testing.T) {
    var counter int64
    fg := NewFunctionalGauge(func() int64 {
        counter++
        return counter
    })
    fg.Value()
    fg.Value()
    if counter != 2 {
        t.Error("counter != 2")
    }
}

func TestGetOrRegisterFunctionalGauge(t *testing.T) {
    r := NewRegistry()
    NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 })
    if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
        t.Fatal(g)
    }
}

func ExampleGetOrRegisterGauge() {
    m := "server.bytes_sent"
    g := GetOrRegisterGauge(m, nil)
    g.Update(47)
    fmt.Println(g.Value()) // Output: 47
}