-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailer_test.go
108 lines (82 loc) · 2.42 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
package tailer
import (
"bytes"
"context"
"io/ioutil"
"log"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var tmpFileName string
func write(t *testing.T, f *os.File, content string) *os.File {
var err error
if f == nil {
f, err = os.OpenFile(tmpFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
assert.NoError(t, err)
}
if len(content) > 0 {
_, err = f.Write([]byte(content))
assert.NoError(t, err)
}
return f
}
func TestExplicitStop(t *testing.T) {
os.RemoveAll(tmpFileName)
bb := bytes.Buffer{}
tt := NewFileTailer(nil, tmpFileName, &bb).WithPoll(100 * time.Millisecond)
tt.Start()
f := write(t, nil, "Hello from TestExplicitStop\n")
time.Sleep(110 * time.Millisecond)
f.Write([]byte("wut?"))
f.Close()
time.Sleep(110 * time.Millisecond)
tt.Stop()
assert.Equal(t, "Hello from TestExplicitStop\nwut?", bb.String())
time.Sleep(50 * time.Millisecond)
assert.False(t, tt.IsRunning())
}
func TestContextStop(t *testing.T) {
os.RemoveAll(tmpFileName)
bb := bytes.Buffer{}
ctx, ctxCancel := context.WithCancel(context.Background())
tt := NewFileTailer(ctx, tmpFileName, &bb).WithPoll(100 * time.Millisecond).Start()
write(t, nil, "Hello from TestContextStop\n").Close()
time.Sleep(150 * time.Millisecond)
ctxCancel()
assert.Equal(t, "Hello from TestContextStop\n", bb.String())
time.Sleep(50 * time.Millisecond)
assert.False(t, tt.IsRunning())
}
func TestContextTimeoutStop(t *testing.T) {
os.RemoveAll(tmpFileName)
bb := bytes.Buffer{}
ctx, _ := context.WithTimeout(context.Background(), 150*time.Millisecond)
tt := NewFileTailer(ctx, tmpFileName, &bb).WithPoll(100 * time.Millisecond).Start()
write(t, nil, "Hello from TestContextTimeoutStop\n").Close()
time.Sleep(200 * time.Millisecond)
assert.Equal(t, "Hello from TestContextTimeoutStop\n", bb.String())
assert.False(t, tt.IsRunning())
}
func TestReaderTailer(t *testing.T) {
bbI, bbO := bytes.Buffer{}, bytes.Buffer{}
tt := NewReaderTailer(nil, &bbI, &bbO).WithPoll(100 * time.Millisecond)
tt.Start()
bbI.WriteString("Hello from TestReaderTailer\n")
time.Sleep(150 * time.Millisecond)
tt.Stop()
assert.Equal(t, "Hello from TestReaderTailer\n", bbO.String())
time.Sleep(50 * time.Millisecond)
assert.False(t, tt.IsRunning())
}
func TestMain(m *testing.M) {
tf, err := ioutil.TempFile("", "tailer")
if err != nil {
log.Fatal(err)
}
tmpFileName = tf.Name()
tf.Close()
defer os.RemoveAll(tmpFileName)
os.Exit(m.Run())
}