forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_test.go
77 lines (59 loc) · 1.47 KB
/
window_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
package kapacitor
import (
"log"
"os"
"testing"
"time"
"github.com/influxdb/kapacitor/models"
"github.com/stretchr/testify/assert"
)
var logger = log.New(os.Stderr, "[window] ", log.LstdFlags|log.Lshortfile)
func TestWindowBuffer(t *testing.T) {
assert := assert.New(t)
buf := &windowBuffer{logger: logger}
size := 100
// fill buffer
for i := 1; i <= size; i++ {
t := time.Unix(int64(i), 0)
p := models.Point{
Time: t,
}
buf.insert(p)
assert.Equal(i, buf.size)
assert.Equal(0, buf.start)
assert.Equal(i, buf.stop)
}
// purge entire buffer
for i := 0; i <= size; i++ {
oldest := time.Unix(int64(i+1), 0)
buf.purge(oldest)
assert.Equal(size-i, buf.size, "i: %d", i)
assert.Equal(i, buf.start, "i: %d", i)
assert.Equal(size, buf.stop, "i: %d", i)
batch := buf.batch()
if assert.Equal(size-i, len(batch.Points)) {
for _, p := range batch.Points {
assert.True(!p.Time.Before(oldest), "Point %s is not after oldest time %s", p.Time, oldest)
}
}
}
assert.Equal(0, buf.size)
// fill buffer again
oldest := time.Unix(int64(size), 0)
for i := 1; i <= size*2; i++ {
t := time.Unix(int64(i+size), 0)
p := models.Point{
Time: t,
}
buf.insert(p)
assert.Equal(i, buf.size)
batch := buf.batch()
if assert.Equal(i, len(batch.Points)) {
for _, p := range batch.Points {
if assert.NotNil(p, "i:%d", i) {
assert.True(!p.Time.Before(oldest), "Point %s is not after oldest time %s", p.Time, oldest)
}
}
}
}
}