-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate_test.go
63 lines (58 loc) · 1.33 KB
/
evaluate_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 conval
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCounter_EvaluateAll_ResultSize(t *testing.T) {
definition := Definition{
Duration: 12 * time.Hour,
}
setup := Setup{}
counter := NewCounter(definition, setup, nil)
startTime := time.Now().Truncate(time.Hour)
perHour := counter.EvaluateAll(startTime, time.Hour)
assert.Equal(t, 12, len(perHour))
per5Minute := counter.EvaluateAll(startTime, 5*time.Minute)
assert.Equal(t, 144, len(per5Minute))
}
func TestToBinIndex(t *testing.T) {
startTime := time.Now().Truncate(time.Hour)
tt := []struct {
desc string
offset time.Duration
resolution time.Duration
expected int
}{
{
desc: "zero",
offset: 0,
resolution: 0,
expected: 0,
},
{
desc: "first bin ends with resolution",
offset: time.Hour - time.Second,
resolution: time.Hour,
expected: 0,
},
{
desc: "second bin contains resolution",
offset: time.Hour,
resolution: time.Hour,
expected: 1,
},
{
desc: "the past is -1",
offset: -time.Second,
resolution: time.Hour,
expected: -1,
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
actual := toBinIndex(startTime.Add(tc.offset), startTime, tc.resolution)
assert.Equal(t, tc.expected, actual)
})
}
}