-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer_test.go
65 lines (53 loc) · 1.31 KB
/
timer_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
package metrics
import (
"testing"
"time"
"errors"
"github.com/stretchr/testify/assert"
)
func TestTimeIt(t *testing.T) {
sub, env := subscribe(t)
defer sub.Unsubscribe()
timer := env.NewTimer("something", nil)
start := timer.Start()
<-time.After(time.Millisecond * 100)
stop := time.Now()
_, err := timer.Stop(nil)
assert.Nil(t, err)
m := readOne(t, sub)
if assert.NotNil(t, m) {
measured := start.Add(time.Duration(m.Value))
assert.WithinDuration(t, stop, measured, time.Millisecond*10)
}
}
func TestTimeBlock(t *testing.T) {
sub, env := subscribe(t)
defer sub.Unsubscribe()
wasCalled := false
env.timeBlock("something", DimMap{"pokemon": "pikachu"}, func() {
wasCalled = true
})
m := readOne(t, sub)
if assert.NotNil(t, m) {
assert.True(t, wasCalled)
assert.Equal(t, "pikachu", m.Dims["pokemon"])
assert.NotZero(t, m.Value)
}
}
func TestTimeBlockErr(t *testing.T) {
sub, env := subscribe(t)
defer sub.Unsubscribe()
wasCalled := false
madeErr := errors.New("garbage error")
_, err := env.timeBlockErr("something", DimMap{"pokemon": "pikachu"}, func() error {
wasCalled = true
return madeErr
})
m := readOne(t, sub)
if assert.NotNil(t, m) {
assert.True(t, wasCalled)
assert.Equal(t, madeErr, err)
assert.Equal(t, "pikachu", m.Dims["pokemon"])
assert.NotZero(t, m.Value)
}
}