-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_test.go
254 lines (235 loc) · 5.42 KB
/
init_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
package tail //nolint:testpackage // TODO
import (
"context"
"errors"
"io"
"os"
"strconv"
"syscall"
"testing"
"time"
"github.com/powerman/check"
)
var (
testTimeFactor = floatGetenv("GO_TEST_TIME_FACTOR", 1.0)
testSecond = time.Duration(float64(time.Second) * testTimeFactor) //nolint:revive // It's actually a second.
pollDelay = testSecond / 10
pollTimeout = testSecond / 2
)
func floatGetenv(name string, def float64) float64 {
if v, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
return v
}
return def
}
type testTail struct {
t *check.C
f *os.File
path string
symlink string
bufc chan []byte
errc chan error
created []string
opened []*os.File
Cancel context.CancelFunc
*Tail
}
func newTestTail(t *check.C) *testTail {
t.Helper()
f, err := os.CreateTemp("", "gotest")
t.Nil(err)
tail := &testTail{
t: t,
f: f,
path: f.Name(),
symlink: "",
bufc: make(chan []byte),
errc: make(chan error), // must not be buffered to sync on errors
created: []string{f.Name()},
opened: []*os.File{f},
Cancel: nil,
Tail: nil,
}
return tail
}
func (tail *testTail) Run() {
if tail.Tail != nil {
panic("tail.Run() must be called only once")
}
ctx, cancel := context.WithCancel(context.Background())
tail.Cancel = cancel
options := []Option{PollDelay(pollDelay), PollTimeout(pollTimeout)}
if tail.symlink == "" {
tail.Tail = Follow(ctx, LoggerFunc(tail.t.Logf), tail.path, options...)
} else {
tail.Tail = Follow(ctx, LoggerFunc(tail.t.Logf), tail.symlink, options...)
}
go tail.reader()
}
func (tail *testTail) Close() {
if tail.Tail == nil {
panic("tail.Run() must be called before tail.Close()")
}
t := tail.t
t.Helper()
tail.Cancel()
WAIT_READER:
for {
select {
case <-tail.bufc:
case _, ok := <-tail.errc:
if !ok {
break WAIT_READER
}
}
}
for _, f := range tail.opened {
t.Nil(f.Close())
}
for _, path := range tail.created {
t.Nil(os.Remove(path))
}
}
func (tail *testTail) reader() {
for {
buf := make([]byte, 8) // use small buffer to ease test large reads
n, err := tail.Read(buf)
switch {
case errors.Is(err, io.EOF):
tail.errc <- err
close(tail.errc)
return
case err != nil:
tail.errc <- err
default:
tail.bufc <- buf[:n]
}
}
}
func (tail *testTail) Want(timeout time.Duration, want string, wanterr error) {
if tail.Tail == nil {
panic("tail.Run() must be called before tail.Want()")
}
t := tail.t
t.Helper()
timeoutc := time.After(timeout)
var s string
for {
select {
case buf := <-tail.bufc:
s += string(buf)
case err := <-tail.errc:
if err == nil {
err = io.ErrClosedPipe
}
t.Equal(s, want)
if perr := (*os.PathError)(nil); errors.As(err, &perr) {
err = perr.Err
}
t.Err(err, wanterr)
return
case <-timeoutc:
t.Equal(s, want)
t.Nil(wanterr)
return
}
}
}
func (tail *testTail) Remove() {
if _, err := os.Stat(tail.path); os.IsNotExist(err) {
panic("tail.Remove() or tail.Rename() must not be called before tail.Remove()")
}
t := tail.t
t.Helper()
t.Nil(os.Remove(tail.path))
for i := range tail.created {
if tail.created[i] == tail.path {
tail.created[i] = tail.created[0]
tail.created = tail.created[1:]
break
}
}
}
func (tail *testTail) Rename() {
if _, err := os.Stat(tail.path); os.IsNotExist(err) {
panic("tail.Remove() or tail.Rename() must not be called before tail.Rename()")
}
t := tail.t
t.Helper()
path := tail.tempPath()
t.Nil(os.Rename(tail.path, path))
for i := range tail.created {
if tail.created[i] == tail.path {
tail.created[i] = path
break
}
}
}
func (tail *testTail) Create() {
if _, err := os.Stat(tail.path); !os.IsNotExist(err) {
panic("tail.Remove() or tail.Rename() must be called before tail.Create()")
}
t := tail.t
t.Helper()
f, err := os.Create(tail.path)
t.Nil(err)
tail.f = f
tail.created = append(tail.created, tail.path)
tail.opened = append(tail.opened, f)
}
func (tail *testTail) CreateFIFO() {
if _, err := os.Stat(tail.path); !os.IsNotExist(err) {
panic("tail.Remove() or tail.Rename() must be called before tail.CreateFIFO()")
}
t := tail.t
t.Helper()
t.Nil(syscall.Mkfifo(tail.path, 0o600))
f, err := os.OpenFile(tail.path, os.O_RDWR, 0o600)
t.Nil(err)
tail.f = f
tail.created = append(tail.created, tail.path)
tail.opened = append(tail.opened, f)
}
func (tail *testTail) CreateSymlink() {
if _, err := os.Stat(tail.path); os.IsNotExist(err) {
panic("tail.Remove() or tail.Rename() must not be called before tail.CreateSymlink()")
}
t := tail.t
t.Helper()
if tail.symlink == "" {
tail.symlink = tail.tempPath()
}
t.Nil(os.Symlink(tail.path, tail.symlink))
tail.created = append(tail.created, tail.symlink)
}
func (tail *testTail) RemoveSymlink() {
if _, err := os.Stat(tail.symlink); os.IsNotExist(err) {
panic("tail.RemoveSymlink() must not be called before tail.CreateSymlink()")
}
t := tail.t
t.Helper()
t.Nil(os.Remove(tail.symlink))
for i := range tail.created {
if tail.created[i] == tail.symlink {
tail.created[i] = tail.created[0]
tail.created = tail.created[1:]
break
}
}
}
func (tail *testTail) Write(s string) {
t := tail.t
t.Helper()
_, err := tail.f.Write([]byte(s))
t.Nil(err)
}
func (tail *testTail) tempPath() string {
t := tail.t
t.Helper()
f, err := os.CreateTemp("", "gotest")
t.Nil(err)
t.Nil(f.Close())
t.Nil(os.Remove(f.Name()))
return f.Name()
}
func TestMain(m *testing.M) { check.TestMain(m) }