-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathpoints_writer_test.go
119 lines (109 loc) · 4.11 KB
/
points_writer_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
package storage_test
//WritePoints does nothing in error state
//the main WritePoints scenarios (large write, etc)
import (
"context"
"errors"
"testing"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/storage"
"github.com/influxdata/influxdb/v2/tsdb"
)
func TestBufferedPointsWriter(t *testing.T) {
t.Run("large empty write on empty buffer", func(t *testing.T) {
pw := &mock.PointsWriter{}
bpw := storage.NewBufferedPointsWriter(6, pw)
bpw.WritePoints(
context.Background(),
mockPoints(
1,
2,
`a day="Monday",humidity=1,ratio=2,temperature=2 11
a day="Tuesday",humidity=2,ratio=1,temperature=2 21
b day="Wednesday",humidity=4,ratio=0.25,temperature=1 21
a day="Thursday",humidity=3,ratio=1,temperature=3 31
c day="Friday",humidity=5,ratio=0,temperature=4 41
e day="Saturday",humidity=6,ratio=0.1,temperature=99 51
`))
if pw.Err != nil {
t.Error(pw.Err)
}
if len(pw.Points) != 24 {
t.Errorf("long writes on empty buffer should write all points but only wrote %d", len(pw.Points))
}
if pw.WritePointsCalled() != 1 {
t.Errorf("expected WritePoints to be called once, but was called %d times", pw.WritePointsCalled())
}
})
t.Run("do nothing in error state", func(t *testing.T) {
pw := &mock.PointsWriter{}
bpw := storage.NewBufferedPointsWriter(6, pw)
bpw.WritePoints(
context.Background(),
mockPoints(
1,
2,
`a day="Monday",humidity=1,ratio=2,temperature=2 11
`))
pw.ForceError(errors.New("OH NO! ERRORZ!"))
err := bpw.WritePoints(
context.Background(),
mockPoints(
1,
2,
`a day="Tuesday",humidity=2,ratio=1,temperature=2 21
b day="Wednesday",humidity=4,ratio=0.25,temperature=1 21
a day="Thursday",humidity=3,ratio=1,temperature=3 31
c day="Friday",humidity=5,ratio=0,temperature=4 41
e day="Saturday",humidity=6,ratio=0.1,temperature=99 51
`))
if pw.Err != err {
t.Error("expected the error returned to be the forced one, but it was not")
}
if pw.WritePointsCalled() != 1 {
t.Errorf("expected WritePoints to be called once, since it should do nothing in the error state, but was called %d times", pw.WritePointsCalled())
}
})
t.Run("flush on write when over limit", func(t *testing.T) {
pw := &mock.PointsWriter{}
bpw := storage.NewBufferedPointsWriter(6, pw)
bpw.WritePoints(context.Background(), mockPoints(1, 2, `a day="Monday",humidity=1,ratio=2,temperature=2 11`))
bpw.WritePoints(context.Background(), mockPoints(1, 2, `a day="Tuesday",humidity=2,ratio=1,temperature=2 21`))
bpw.WritePoints(context.Background(), mockPoints(1, 2, `b day="Wednesday",humidity=4,ratio=0.25,temperature=1 21`))
bpw.WritePoints(context.Background(), mockPoints(1, 2, `a day="Thursday",humidity=3,ratio=1,temperature=3 31`))
bpw.WritePoints(context.Background(), mockPoints(1, 2, `c day="Friday",humidity=5,ratio=0,temperature=4 41`))
bpw.WritePoints(context.Background(), mockPoints(1, 2, `e day="Saturday",humidity=6,ratio=0.1,temperature=99 51`))
if pw.Err != nil {
t.Errorf("expected no error, but got %v", pw.Err)
}
if pw.WritePointsCalled() != 3 {
t.Errorf("expected WritePoints to be called 3 times, but was called %d times", pw.WritePointsCalled())
}
bpw.Flush(context.Background())
if pw.WritePointsCalled() != 4 {
t.Errorf("expected WritePoints to be called 4 times, but was called %d times", pw.WritePointsCalled())
}
bpw.Flush(context.Background())
if pw.WritePointsCalled() != 4 {
t.Errorf("expected WritePoints to be called 4 times, but was called %d times", pw.WritePointsCalled())
}
})
t.Run("don't flush when empty", func(t *testing.T) {
pw := &mock.PointsWriter{}
bpw := storage.NewBufferedPointsWriter(6, pw)
bpw.Flush(context.Background())
if pw.WritePointsCalled() != 0 {
t.Errorf("expected WritePoints to not be falled but was called %d times", pw.WritePointsCalled())
}
})
}
func mockPoints(org, bucket influxdb.ID, pointdata string) []models.Point {
name := tsdb.EncodeName(org, bucket)
points, err := models.ParsePoints([]byte(pointdata), name[:])
if err != nil {
panic(err)
}
return points
}