-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathtailer_test.go
515 lines (491 loc) · 12.8 KB
/
tailer_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package tailer_test
import (
"bufio"
"bytes"
"fmt"
"io"
"sync"
"time"
"github.com/juju/testing"
gc "gopkg.in/check.v1"
"github.com/juju/utils/v4/tailer"
)
type tailerSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&tailerSuite{})
var alphabetData = []string{
"alpha alpha\n",
"bravo bravo\n",
"charlie charlie\n",
"delta delta\n",
"echo echo\n",
"foxtrott foxtrott\n",
"golf golf\n",
"hotel hotel\n",
"india india\n",
"juliet juliet\n",
"kilo kilo\n",
"lima lima\n",
"mike mike\n",
"november november\n",
"oscar oscar\n",
"papa papa\n",
"quebec quebec\n",
"romeo romeo\n",
"sierra sierra\n",
"tango tango\n",
"uniform uniform\n",
"victor victor\n",
"whiskey whiskey\n",
"x-ray x-ray\n",
"yankee yankee\n",
"zulu zulu\n",
}
var tests = []struct {
description string
data []string
initialLinesWritten int
initialLinesRequested uint
bufferSize int
filter tailer.TailerFilterFunc
injector func(*tailer.Tailer, *readSeeker) func([]string)
initialCollectedData []string
appendedCollectedData []string
fromStart bool
err string
}{{
description: "lines are longer than buffer size",
data: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
"0123456789012345678901234567890123456789012345678901\n",
},
initialLinesWritten: 1,
initialLinesRequested: 1,
bufferSize: 5,
initialCollectedData: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
},
appendedCollectedData: []string{
"0123456789012345678901234567890123456789012345678901\n",
},
}, {
description: "lines are longer than buffer size, missing termination of last line",
data: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
"0123456789012345678901234567890123456789012345678901\n",
"the quick brown fox ",
},
initialLinesWritten: 1,
initialLinesRequested: 1,
bufferSize: 5,
initialCollectedData: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
},
appendedCollectedData: []string{
"0123456789012345678901234567890123456789012345678901\n",
},
}, {
description: "lines are longer than buffer size, last line is terminated later",
data: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
"0123456789012345678901234567890123456789012345678901\n",
"the quick brown fox ",
"jumps over the lazy dog\n",
},
initialLinesWritten: 1,
initialLinesRequested: 1,
bufferSize: 5,
initialCollectedData: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
},
appendedCollectedData: []string{
"0123456789012345678901234567890123456789012345678901\n",
"the quick brown fox jumps over the lazy dog\n",
},
}, {
description: "missing termination of last line",
data: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
"0123456789012345678901234567890123456789012345678901\n",
"the quick brown fox ",
},
initialLinesWritten: 1,
initialLinesRequested: 1,
initialCollectedData: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
},
appendedCollectedData: []string{
"0123456789012345678901234567890123456789012345678901\n",
},
}, {
description: "last line is terminated later",
data: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
"0123456789012345678901234567890123456789012345678901\n",
"the quick brown fox ",
"jumps over the lazy dog\n",
},
initialLinesWritten: 1,
initialLinesRequested: 1,
initialCollectedData: []string{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n",
},
appendedCollectedData: []string{
"0123456789012345678901234567890123456789012345678901\n",
"the quick brown fox jumps over the lazy dog\n",
},
}, {
description: "more lines already written than initially requested",
data: alphabetData,
initialLinesWritten: 5,
initialLinesRequested: 3,
initialCollectedData: []string{
"charlie charlie\n",
"delta delta\n",
"echo echo\n",
},
appendedCollectedData: alphabetData[5:],
}, {
description: "less lines already written than initially requested",
data: alphabetData,
initialLinesWritten: 3,
initialLinesRequested: 5,
initialCollectedData: []string{
"alpha alpha\n",
"bravo bravo\n",
"charlie charlie\n",
},
appendedCollectedData: alphabetData[3:],
}, {
description: "lines are longer than buffer size, more lines already written than initially requested",
data: alphabetData,
initialLinesWritten: 5,
initialLinesRequested: 3,
bufferSize: 5,
initialCollectedData: []string{
"charlie charlie\n",
"delta delta\n",
"echo echo\n",
},
appendedCollectedData: alphabetData[5:],
}, {
description: "ignore current lines",
data: alphabetData,
initialLinesWritten: 5,
bufferSize: 5,
appendedCollectedData: alphabetData[5:],
}, {
description: "start from the start",
data: alphabetData,
initialLinesWritten: 5,
bufferSize: 5,
appendedCollectedData: alphabetData,
fromStart: true,
}, {
description: "lines are longer than buffer size, less lines already written than initially requested",
data: alphabetData,
initialLinesWritten: 3,
initialLinesRequested: 5,
bufferSize: 5,
initialCollectedData: []string{
"alpha alpha\n",
"bravo bravo\n",
"charlie charlie\n",
},
appendedCollectedData: alphabetData[3:],
}, {
description: "filter lines which contain the char 'e'",
data: alphabetData,
initialLinesWritten: 10,
initialLinesRequested: 3,
filter: func(line []byte) bool {
return bytes.Contains(line, []byte{'e'})
},
initialCollectedData: []string{
"echo echo\n",
"hotel hotel\n",
"juliet juliet\n",
},
appendedCollectedData: []string{
"mike mike\n",
"november november\n",
"quebec quebec\n",
"romeo romeo\n",
"sierra sierra\n",
"whiskey whiskey\n",
"yankee yankee\n",
},
}, {
description: "stop tailing after 10 collected lines",
data: alphabetData,
initialLinesWritten: 5,
initialLinesRequested: 3,
injector: func(t *tailer.Tailer, rs *readSeeker) func([]string) {
return func(lines []string) {
if len(lines) == 10 {
t.Stop()
}
}
},
initialCollectedData: []string{
"charlie charlie\n",
"delta delta\n",
"echo echo\n",
},
appendedCollectedData: alphabetData[5:],
}, {
description: "generate an error after 10 collected lines",
data: alphabetData,
initialLinesWritten: 5,
initialLinesRequested: 3,
injector: func(t *tailer.Tailer, rs *readSeeker) func([]string) {
return func(lines []string) {
if len(lines) == 10 {
rs.setError(fmt.Errorf("ouch after 10 lines"))
}
}
},
initialCollectedData: []string{
"charlie charlie\n",
"delta delta\n",
"echo echo\n",
},
appendedCollectedData: alphabetData[5:],
err: "ouch after 10 lines",
}, {
description: "more lines already written than initially requested, some empty, unfiltered",
data: []string{
"one one\n",
"two two\n",
"\n",
"\n",
"three three\n",
"four four\n",
"\n",
"\n",
"five five\n",
"six six\n",
},
initialLinesWritten: 3,
initialLinesRequested: 2,
initialCollectedData: []string{
"two two\n",
"\n",
},
appendedCollectedData: []string{
"\n",
"three three\n",
"four four\n",
"\n",
"\n",
"five five\n",
"six six\n",
},
}, {
description: "more lines already written than initially requested, some empty, those filtered",
data: []string{
"one one\n",
"two two\n",
"\n",
"\n",
"three three\n",
"four four\n",
"\n",
"\n",
"five five\n",
"six six\n",
},
initialLinesWritten: 3,
initialLinesRequested: 2,
filter: func(line []byte) bool {
return len(bytes.TrimSpace(line)) > 0
},
initialCollectedData: []string{
"one one\n",
"two two\n",
},
appendedCollectedData: []string{
"three three\n",
"four four\n",
"five five\n",
"six six\n",
},
}}
func (s *tailerSuite) TestTailer(c *gc.C) {
for i, test := range tests {
c.Logf("Test #%d) %s", i, test.description)
bufferSize := test.bufferSize
if bufferSize == 0 {
// Default value.
bufferSize = 4096
}
s.PatchValue(tailer.BufferSize, bufferSize)
reader, writer := io.Pipe()
sigc := make(chan struct{}, 1)
rs := startReadSeeker(c, test.data, test.initialLinesWritten, sigc)
if !test.fromStart {
err := tailer.SeekLastLines(rs, test.initialLinesRequested, test.filter)
c.Assert(err, gc.IsNil)
}
tailer := tailer.NewTestTailer(rs, writer, test.filter, 2*time.Millisecond)
linec := startReading(c, tailer, reader, writer)
// Collect initial data.
assertCollected(c, linec, test.initialCollectedData, nil)
sigc <- struct{}{}
// Collect remaining data, possibly with injection to stop
// earlier or generate an error.
var injection func([]string)
if test.injector != nil {
injection = test.injector(tailer, rs)
}
assertCollected(c, linec, test.appendedCollectedData, injection)
if test.err == "" {
c.Assert(tailer.Stop(), gc.IsNil)
} else {
c.Assert(tailer.Err(), gc.ErrorMatches, test.err)
}
}
}
// startReading starts a goroutine receiving the lines out of the reader
// in the background and passing them to a created string channel. This
// will used in the assertions.
func startReading(c *gc.C, tailer *tailer.Tailer, reader *io.PipeReader, writer *io.PipeWriter) chan string {
linec := make(chan string)
// Start goroutine for reading.
go func() {
defer close(linec)
reader := bufio.NewReader(reader)
for {
line, err := reader.ReadString('\n')
switch err {
case nil:
linec <- line
case io.EOF:
return
default:
c.Fail()
}
}
}()
// Close writer when tailer is stopped or has an error. Tailer using
// components can do it the same way.
go func() {
tailer.Wait()
writer.Close()
}()
return linec
}
// assertCollected reads lines from the string channel linec. It compares if
// those are the one passed with compare until a timeout. If the timeout is
// reached earlier than all lines are collected the assertion fails. The
// injection function allows to interrupt the processing with a function
// generating an error or a regular stopping during the tailing. In case the
// linec is closed due to stopping or an error only the values so far care
// compared. Checking the reason for termination is done in the test.
func assertCollected(c *gc.C, linec chan string, compare []string, injection func([]string)) {
if len(compare) == 0 {
return
}
timeout := time.After(10 * time.Second)
lines := []string{}
for {
select {
case line, ok := <-linec:
if ok {
lines = append(lines, line)
if injection != nil {
injection(lines)
}
if len(lines) == len(compare) {
// All data received.
c.Assert(lines, gc.DeepEquals, compare)
return
}
} else {
// linec closed after stopping or error.
c.Assert(lines, gc.DeepEquals, compare[:len(lines)])
return
}
case <-timeout:
if injection == nil {
c.Fatalf("timeout during tailer collection")
}
return
}
}
}
// startReadSeeker returns a ReadSeeker for the Tailer. It simulates
// reading and seeking inside a file and also simulating an error.
// The goroutine waits for a signal that it can start writing the
// appended lines.
func startReadSeeker(c *gc.C, data []string, initialLeg int, sigc chan struct{}) *readSeeker {
// Write initial lines into the buffer.
var rs readSeeker
var i int
for i = 0; i < initialLeg; i++ {
rs.write(data[i])
}
go func() {
<-sigc
for ; i < len(data); i++ {
time.Sleep(5 * time.Millisecond)
rs.write(data[i])
}
}()
return &rs
}
type readSeeker struct {
mux sync.Mutex
buffer []byte
pos int
err error
}
func (r *readSeeker) write(s string) {
r.mux.Lock()
defer r.mux.Unlock()
r.buffer = append(r.buffer, []byte(s)...)
}
func (r *readSeeker) setError(err error) {
r.mux.Lock()
defer r.mux.Unlock()
r.err = err
}
func (r *readSeeker) Read(p []byte) (n int, err error) {
r.mux.Lock()
defer r.mux.Unlock()
if r.err != nil {
return 0, r.err
}
if r.pos >= len(r.buffer) {
return 0, io.EOF
}
n = copy(p, r.buffer[r.pos:])
r.pos += n
return n, nil
}
func (r *readSeeker) Seek(offset int64, whence int) (ret int64, err error) {
r.mux.Lock()
defer r.mux.Unlock()
var newPos int64
switch whence {
case 0:
newPos = offset
case 1:
newPos = int64(r.pos) + offset
case 2:
newPos = int64(len(r.buffer)) + offset
default:
return 0, fmt.Errorf("invalid whence: %d", whence)
}
if newPos < 0 {
return 0, fmt.Errorf("negative position: %d", newPos)
}
if newPos >= 1<<31 {
return 0, fmt.Errorf("position out of range: %d", newPos)
}
r.pos = int(newPos)
return newPos, nil
}