forked from VictoriaMetrics/metrics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
floatcounter_test.go
76 lines (69 loc) · 1.69 KB
/
floatcounter_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
package metrics
import (
"fmt"
"testing"
)
func TestFloatCounterSerial(t *testing.T) {
name := "FloatCounterSerial"
c := NewFloatCounter(name)
c.Add(0.1)
if n := c.Get(); n != 0.1 {
t.Fatalf("unexpected counter value; got %f; want 0.1", n)
}
c.Set(123.00001)
if n := c.Get(); n != 123.00001 {
t.Fatalf("unexpected counter value; got %f; want 123.00001", n)
}
c.Sub(0.00001)
if n := c.Get(); n != 123 {
t.Fatalf("unexpected counter value; got %f; want 123", n)
}
c.Add(2.002)
if n := c.Get(); n != 125.002 {
t.Fatalf("unexpected counter value; got %f; want 125.002", n)
}
// Verify MarshalTo
testMarshalTo(t, c, "foobar", "foobar 125.002\n")
}
func TestFloatCounterConcurrent(t *testing.T) {
name := "FloatCounterConcurrent"
c := NewFloatCounter(name)
err := testConcurrent(func() error {
nPrev := c.Get()
for i := 0; i < 10; i++ {
c.Add(1.001)
if n := c.Get(); n <= nPrev {
return fmt.Errorf("counter value must be greater than %f; got %f", nPrev, n)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
func TestGetOrCreateFloatCounterSerial(t *testing.T) {
name := "GetOrCreateFloatCounterSerial"
if err := testGetOrCreateCounter(name); err != nil {
t.Fatal(err)
}
}
func TestGetOrCreateFloatCounterConcurrent(t *testing.T) {
name := "GetOrCreateFloatCounterConcurrent"
err := testConcurrent(func() error {
return testGetOrCreateFloatCounter(name)
})
if err != nil {
t.Fatal(err)
}
}
func testGetOrCreateFloatCounter(name string) error {
c1 := GetOrCreateFloatCounter(name)
for i := 0; i < 10; i++ {
c2 := GetOrCreateFloatCounter(name)
if c1 != c2 {
return fmt.Errorf("unexpected counter returned; got %p; want %p", c2, c1)
}
}
return nil
}