forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udf_test.go
286 lines (251 loc) · 5.92 KB
/
udf_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package kapacitor_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"reflect"
"testing"
"time"
"github.com/influxdata/kapacitor"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/udf"
udf_test "github.com/influxdata/kapacitor/udf/test"
)
func newUDFSocket(name string) (*kapacitor.UDFSocket, *udf_test.IO) {
uio := udf_test.NewIO()
l := log.New(os.Stderr, fmt.Sprintf("[%s] ", name), log.LstdFlags)
u := kapacitor.NewUDFSocket(newTestSocket(uio), l, 0, nil)
return u, uio
}
func newUDFProcess(name string) (*kapacitor.UDFProcess, *udf_test.IO) {
uio := udf_test.NewIO()
cmd := newTestCommander(uio)
l := log.New(os.Stderr, fmt.Sprintf("[%s] ", name), log.LstdFlags)
u := kapacitor.NewUDFProcess(cmd, l, 0, nil)
return u, uio
}
func TestUDFSocket_OpenClose(t *testing.T) {
u, uio := newUDFSocket("OpenClose")
testUDF_OpenClose(u, uio, t)
}
func TestUDFProcess_OpenClose(t *testing.T) {
u, uio := newUDFProcess("OpenClose")
testUDF_OpenClose(u, uio, t)
}
func testUDF_OpenClose(u udf.Interface, uio *udf_test.IO, t *testing.T) {
u.Open()
close(uio.Responses)
u.Close()
// read all requests and wait till the chan is closed
for range uio.Requests {
}
if err := <-uio.ErrC; err != nil {
t.Error(err)
}
}
func TestUDFSocket_WritePoint(t *testing.T) {
u, uio := newUDFSocket("WritePoint")
testUDF_WritePoint(u, uio, t)
}
func TestUDFProcess_WritePoint(t *testing.T) {
u, uio := newUDFProcess("WritePoint")
testUDF_WritePoint(u, uio, t)
}
func testUDF_WritePoint(u udf.Interface, uio *udf_test.IO, t *testing.T) {
go func() {
req := <-uio.Requests
_, ok := req.Message.(*udf.Request_Init)
if !ok {
t.Errorf("expected init message got %T", req.Message)
}
res := &udf.Response{
Message: &udf.Response_Init{
Init: &udf.InitResponse{
Success: true,
},
},
}
uio.Responses <- res
req = <-uio.Requests
pt, ok := req.Message.(*udf.Request_Point)
if !ok {
t.Errorf("expected point message got %T", req.Message)
}
res = &udf.Response{
Message: &udf.Response_Point{
Point: pt.Point,
},
}
uio.Responses <- res
close(uio.Responses)
}()
err := u.Open()
if err != nil {
t.Fatal(err)
}
err = u.Init(nil)
if err != nil {
t.Fatal(err)
}
// Write point to server
pt := models.Point{
Name: "test",
Database: "db",
RetentionPolicy: "rp",
Tags: models.Tags{"t1": "v1", "t2": "v2"},
Fields: models.Fields{"f1": 1.0, "f2": 2.0},
Time: time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC),
}
u.PointIn() <- pt
rpt := <-u.PointOut()
if !reflect.DeepEqual(rpt, pt) {
t.Errorf("unexpected returned point got: %v exp %v", rpt, pt)
}
u.Close()
// read all requests and wait till the chan is closed
for range uio.Requests {
}
if err := <-uio.ErrC; err != nil {
t.Error(err)
}
}
func TestUDFSocket_WriteBatch(t *testing.T) {
u, uio := newUDFSocket("WriteBatch")
testUDF_WriteBatch(u, uio, t)
}
func TestUDFProcess_WriteBatch(t *testing.T) {
u, uio := newUDFProcess("WriteBatch")
testUDF_WriteBatch(u, uio, t)
}
func testUDF_WriteBatch(u udf.Interface, uio *udf_test.IO, t *testing.T) {
go func() {
req := <-uio.Requests
_, ok := req.Message.(*udf.Request_Init)
if !ok {
t.Errorf("expected init message got %T", req.Message)
}
res := &udf.Response{
Message: &udf.Response_Init{
Init: &udf.InitResponse{
Success: true,
},
},
}
uio.Responses <- res
// Begin batch
req = <-uio.Requests
bb, ok := req.Message.(*udf.Request_Begin)
if !ok {
t.Errorf("expected begin message got %T", req.Message)
}
res = &udf.Response{
Message: &udf.Response_Begin{
Begin: bb.Begin,
},
}
uio.Responses <- res
// Point
req = <-uio.Requests
pt, ok := req.Message.(*udf.Request_Point)
if !ok {
t.Errorf("expected point message got %T", req.Message)
}
res = &udf.Response{
Message: &udf.Response_Point{
Point: pt.Point,
},
}
uio.Responses <- res
// End batch
req = <-uio.Requests
eb, ok := req.Message.(*udf.Request_End)
if !ok {
t.Errorf("expected end message got %T", req.Message)
}
res = &udf.Response{
Message: &udf.Response_End{
End: eb.End,
},
}
uio.Responses <- res
close(uio.Responses)
}()
err := u.Open()
if err != nil {
t.Fatal(err)
}
err = u.Init(nil)
if err != nil {
t.Fatal(err)
}
// Write point to server
b := models.Batch{
Name: "test",
Tags: models.Tags{"t1": "v1"},
TMax: time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC),
Points: []models.BatchPoint{{
Fields: models.Fields{"f1": 1.0, "f2": 2.0, "f3": int64(1), "f4": "str"},
Time: time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC),
Tags: models.Tags{"t1": "v1", "t2": "v2"},
}},
}
u.BatchIn() <- b
rb := <-u.BatchOut()
if !reflect.DeepEqual(b, rb) {
t.Errorf("unexpected returned batch got: %v exp %v", rb, b)
}
u.Close()
// read all requests and wait till the chan is closed
for range uio.Requests {
}
if err := <-uio.ErrC; err != nil {
t.Error(err)
}
}
type testCommander struct {
uio *udf_test.IO
}
func newTestCommander(uio *udf_test.IO) kapacitor.Commander {
return &testCommander{
uio: uio,
}
}
func (c *testCommander) NewCommand() kapacitor.Command {
return c
}
func (c *testCommander) Start() error { return nil }
func (c *testCommander) Wait() error { return nil }
func (c *testCommander) StdinPipe() (io.WriteCloser, error) {
return c.uio.In(), nil
}
func (c *testCommander) StdoutPipe() (io.Reader, error) {
return c.uio.Out(), nil
}
func (c *testCommander) StderrPipe() (io.Reader, error) {
return &bytes.Buffer{}, nil
}
func (c *testCommander) Kill() {
c.uio.Kill()
}
type testSocket struct {
uio *udf_test.IO
}
func newTestSocket(uio *udf_test.IO) kapacitor.Socket {
return &testSocket{
uio: uio,
}
}
func (s *testSocket) Open() error {
return nil
}
func (s *testSocket) Close() error {
return nil
}
func (s *testSocket) In() io.WriteCloser {
return s.uio.In()
}
func (s *testSocket) Out() io.Reader {
return s.uio.Out()
}