2019-04-11 12:59:53 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-04-15 15:34:02 +03:00
|
|
|
func TestGaugeError(t *testing.T) {
|
2023-12-20 15:06:44 +03:00
|
|
|
expectPanic(t, "NewGauge_Set_non-nil-callback", func() {
|
|
|
|
g := NewGauge("NewGauge_non_nil_callback", func() float64 { return 123 })
|
|
|
|
g.Set(12.35)
|
2019-04-15 15:34:02 +03:00
|
|
|
})
|
2023-12-20 15:06:44 +03:00
|
|
|
expectPanic(t, "GetOrCreateGauge_Set_non-nil-callback", func() {
|
|
|
|
g := GetOrCreateGauge("GetOrCreateGauge_nil_callback", func() float64 { return 123 })
|
|
|
|
g.Set(42)
|
2019-04-15 15:34:02 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-20 15:06:44 +03:00
|
|
|
func TestGaugeSet(t *testing.T) {
|
|
|
|
s := NewSet()
|
|
|
|
g := s.NewGauge("foo", nil)
|
|
|
|
if n := g.Get(); n != 0 {
|
|
|
|
t.Fatalf("unexpected gauge value: %g; expecting 0", n)
|
|
|
|
}
|
|
|
|
g.Set(1.234)
|
|
|
|
if n := g.Get(); n != 1.234 {
|
|
|
|
t.Fatalf("unexpected gauge value %g; expecting 1.234", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:59:53 +03:00
|
|
|
func TestGaugeSerial(t *testing.T) {
|
|
|
|
name := "GaugeSerial"
|
|
|
|
n := 1.23
|
|
|
|
var nLock sync.Mutex
|
|
|
|
g := NewGauge(name, func() float64 {
|
|
|
|
nLock.Lock()
|
|
|
|
defer nLock.Unlock()
|
|
|
|
n++
|
|
|
|
return n
|
|
|
|
})
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if nn := g.Get(); nn != n {
|
|
|
|
t.Fatalf("unexpected gauge value; got %v; want %v", nn, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify marshalTo
|
|
|
|
testMarshalTo(t, g, "foobar", "foobar 12.23\n")
|
|
|
|
|
|
|
|
// Verify big numbers marshaling
|
|
|
|
n = 1234567899
|
|
|
|
testMarshalTo(t, g, "prefix", "prefix 1234567900\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeConcurrent(t *testing.T) {
|
|
|
|
name := "GaugeConcurrent"
|
|
|
|
var n int
|
|
|
|
var nLock sync.Mutex
|
|
|
|
g := NewGauge(name, func() float64 {
|
|
|
|
nLock.Lock()
|
|
|
|
defer nLock.Unlock()
|
|
|
|
n++
|
|
|
|
return float64(n)
|
|
|
|
})
|
|
|
|
err := testConcurrent(func() error {
|
|
|
|
nPrev := g.Get()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if n := g.Get(); n <= nPrev {
|
|
|
|
return fmt.Errorf("gauge value must be greater than %v; got %v", nPrev, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|