-
Notifications
You must be signed in to change notification settings - Fork 0
/
tail_test.go
210 lines (198 loc) · 4.35 KB
/
tail_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
package tail
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
"time"
)
func TestTailF(t *testing.T) {
file, _ := ioutil.TempFile("/tmp", "test")
defer os.Remove(file.Name())
if _, err := file.WriteString("this is 1 line\nthis is 2 line\nthis is the 3 line\n"); err != nil {
panic("write file error")
}
_ = file.Close()
eofCh := make(chan string)
lineCh, closeFunc, errCh := tailF(file.Name(), true, eofCh, &Config{
PollInterval: 500 * time.Millisecond,
Offset: SeekFromStart,
Follow: true,
})
buf := bytes.Buffer{}
done := make(chan int)
go func() {
for {
select {
case <-errCh:
t.FailNow()
return
case line := <-lineCh:
buf.WriteString(line)
case ev := <-eofCh:
if ev == EventEof {
closeFunc()
if "this is 1 linethis is 2 linethis is the 3 line" != buf.String() {
t.FailNow()
}
done <- 1
}
}
}
}()
<-done
}
func TestTailF_Remove(t *testing.T) {
file, _ := ioutil.TempFile("/tmp", "test")
if _, err := file.WriteString("this is 1 line\nthis is 2 line\nthis is the 3 line\n"); err != nil {
panic("write file error")
}
_ = file.Close()
inspectCh := make(chan string)
lineCh, closeFunc, errCh := tailF(file.Name(), true, inspectCh, &Config{
PollInterval: 500 * time.Millisecond,
Offset: SeekFromStart,
Follow: true,
})
buf := bytes.Buffer{}
done := make(chan int)
go func() {
l:
for {
select {
case <-errCh:
t.FailNow()
case <-lineCh:
case ev := <-inspectCh:
if ev == EventEof {
break l
}
}
}
filePath := file.Name()
_ = os.Remove(filePath)
file, err := os.Create(filePath)
if err != nil {
t.FailNow()
}
if _, err := file.WriteString("this is 11 line\nthis is 12 line\nthis is the 13 line\n"); err != nil {
panic("write file error")
}
_ = file.Close()
go func() {
restarted := false
for {
select {
case <-errCh:
t.FailNow()
case line := <-lineCh:
t.Log(line)
buf.WriteString(line)
case ev := <-inspectCh:
if ev == EventTailRestart {
restarted = true
} else if ev == EventEof {
if restarted {
closeFunc()
if "this is 11 linethis is 12 linethis is the 13 line" != buf.String() {
t.FailNow()
}
_ = os.Remove(filePath)
done <- 1
return
}
}
}
}
}()
}()
<-done
}
func TestTailF_Truncate(t *testing.T) {
file, _ := ioutil.TempFile("/tmp", "test")
lines := `
1
2
3
4
5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10a
10b
10c
11c
`
if _, err := file.WriteString(lines); err != nil {
panic("write file error")
}
_ = file.Close()
inspectCh := make(chan string)
lineCh, closeFunc, errCh := tailF(file.Name(), true, inspectCh, &Config{
PollInterval: 500 * time.Millisecond,
Offset: SeekFromStart,
Follow: true,
})
done := make(chan int)
go func() {
l:
for {
select {
case <-errCh:
t.FailNow()
case line := <-lineCh:
t.Log(line)
case ev := <-inspectCh:
if ev == EventEof {
break l
}
}
}
filePath := file.Name()
cmd := fmt.Sprintf(`echo ''> %s`, file.Name())
err := exec.Command("/bin/bash", "-c", cmd).Run()
if err != nil {
panic(err)
}
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
panic(err)
}
if _, err := file.WriteString("this is 11 line\nthis is 12 line\nthis is the 13 line\n"); err != nil {
panic("write file error: " + err.Error())
}
_ = file.Close()
go func() {
buf := bytes.Buffer{}
restarted := false
for {
select {
case <-errCh:
t.FailNow()
case line := <-lineCh:
t.Log(line)
buf.WriteString(line)
case ev := <-inspectCh:
if ev == EventTailRestart {
restarted = true
} else if ev == EventEof {
if restarted {
closeFunc()
_ = os.Remove(filePath)
if "this is 11 linethis is 12 linethis is the 13 line" != buf.String() {
t.FailNow()
}
done <- 1
return
}
}
}
}
}()
}()
<-done
}