-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathreqlatencycounter_test.go
63 lines (57 loc) · 1.87 KB
/
reqlatencycounter_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
package web
import (
"context"
"testing"
"time"
"github.com/signalfx/golib/v3/timekeeper/timekeepertest"
. "github.com/smartystreets/goconvey/convey"
)
type limitStubThing time.Duration
func (l *limitStubThing) Get() time.Duration {
return time.Duration(*l)
}
func TestReqLatencyCounter(t *testing.T) {
starttime := time.Date(1981, time.March, 19, 6, 0, 0, 0, time.UTC)
fastRequestLimit := 100 * time.Millisecond
limitStub := limitStubThing(fastRequestLimit)
Convey("When setup,", t, func() {
timeStub := timekeepertest.NewStubClock(starttime)
counter := NewReqLatencyCounter(&limitStub)
counter.timeKeeper = timeStub
ctx := context.Background()
Convey("having no requests results in 2 stat metrics.", func() {
stats := counter.Stats(map[string]string{})
So(stats, ShouldNotBeNil)
So(len(stats), ShouldEqual, 2)
})
Convey("having extra dimensions adds dimensions to metrics", func() {
stats := counter.Stats(map[string]string{"testDim": "testVal"})
So(stats, ShouldNotBeNil)
for _, stat := range stats {
So(stat.Dimensions, ShouldContainKey, "testDim")
So(stat.Dimensions["testDim"], ShouldEqual, "testVal")
}
})
Convey("slow increment the slow request count.", func() {
ctx = AddTime(ctx, starttime)
timeStub.Incr(fastRequestLimit + 1)
counter.ModStats(ctx)
So(counter.slowRequests, ShouldEqual, 1)
So(counter.fastRequests, ShouldEqual, 0)
})
Convey("fast increment the fast request count.", func() {
ctx = AddTime(ctx, starttime)
timeStub.Incr(fastRequestLimit - 1)
counter.ModStats(ctx)
So(counter.slowRequests, ShouldEqual, 0)
So(counter.fastRequests, ShouldEqual, 1)
})
Convey("having request results in 2 stat metrics.", func() {
counter.fastRequests++
counter.slowRequests++
stats := counter.Stats(map[string]string{})
So(stats, ShouldNotBeNil)
So(len(stats), ShouldEqual, 2)
})
})
}