-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_test.go
140 lines (116 loc) · 2.8 KB
/
func_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package metrics
import (
"sync"
"testing"
"go.withmatt.com/metrics/internal/assert"
)
func TestFuncNew(t *testing.T) {
t.Run("float", func(t *testing.T) {
NewSet().NewFloat64Func("foo", nil)
NewSet().NewFloat64Func("foo", nil, "bar", "baz")
// invalid label pairs
assert.Panics(t, func() { NewSet().NewFloat64Func("foo", nil, "bar") })
// duplicate
set := NewSet()
set.NewFloat64Func("foo", nil)
assert.Panics(t, func() { set.NewFloat64Func("foo", nil) })
})
t.Run("int", func(t *testing.T) {
NewSet().NewInt64Func("foo", nil)
NewSet().NewInt64Func("foo", nil, "bar", "baz")
// invalid label pairs
assert.Panics(t, func() { NewSet().NewInt64Func("foo", nil, "bar") })
// duplicate
set := NewSet()
set.NewInt64Func("foo", nil)
assert.Panics(t, func() { set.NewInt64Func("foo", nil) })
})
t.Run("uint", func(t *testing.T) {
NewSet().NewUint64Func("foo", nil)
NewSet().NewUint64Func("foo", nil, "bar", "baz")
// invalid label pairs
assert.Panics(t, func() { NewSet().NewUint64Func("foo", nil, "bar") })
// duplicate
set := NewSet()
set.NewUint64Func("foo", nil)
assert.Panics(t, func() { set.NewUint64Func("foo", nil) })
})
}
func TestFuncCallback(t *testing.T) {
t.Run("float", func(t *testing.T) {
set := NewSet()
set.NewFloat64Func("foo", func() float64 {
return 1.1
})
assertMarshal(t, set, []string{"foo 1.1"})
})
t.Run("int", func(t *testing.T) {
set := NewSet()
set.NewInt64Func("foo", func() int64 {
return -2
})
assertMarshal(t, set, []string{"foo -2"})
})
t.Run("uint", func(t *testing.T) {
set := NewSet()
set.NewUint64Func("foo", func() uint64 {
return 100
})
assertMarshal(t, set, []string{"foo 100"})
})
}
func TestFuncConcurrent(t *testing.T) {
const n = 1000
const inner = 5
t.Run("float", func(t *testing.T) {
var x float64
var nLock sync.Mutex
g := NewSet().NewFloat64Func("x", func() float64 {
nLock.Lock()
defer nLock.Unlock()
x++
return x
})
hammer(t, n, func(_ int) {
prevX := g.Get()
for range inner {
assert.Greater(t, g.Get(), prevX)
}
})
assert.Equal(t, g.Get(), (n*inner)+n+1)
})
t.Run("int", func(t *testing.T) {
var x int64
var nLock sync.Mutex
g := NewSet().NewInt64Func("x", func() int64 {
nLock.Lock()
defer nLock.Unlock()
x++
return x
})
hammer(t, n, func(_ int) {
prevX := g.Get()
for range inner {
assert.Greater(t, g.Get(), prevX)
}
})
assert.Equal(t, g.Get(), (n*inner)+n+1)
})
t.Run("uint", func(t *testing.T) {
var x uint64
var nLock sync.Mutex
g := NewSet().NewUint64Func("x", func() uint64 {
nLock.Lock()
defer nLock.Unlock()
x++
return x
})
hammer(t, n, func(_ int) {
prevX := g.Get()
for range inner {
assert.Greater(t, g.Get(), prevX)
}
})
assert.Equal(t, g.Get(), (n*inner)+n+1)
})
}