-
Notifications
You must be signed in to change notification settings - Fork 5
/
inotify_test.go
332 lines (249 loc) · 6.46 KB
/
inotify_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
package gonotify
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
"time"
)
func TestOpenClose(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
}
func TestReadFromClosed(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
cancel()
select {
case <-i.done:
case <-time.After(5 * time.Second):
t.Error("Inotify did not close")
// output traces of all goroutines of the current program
buf := make([]byte, 1<<16)
n := runtime.Stack(buf, true)
t.Logf("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===\n", buf[:n])
return
}
_, err = i.Read()
if err == nil {
t.Error("Expected error, but got nil")
}
}
func BenchmarkWatch(b *testing.B) {
for x := 0; x < b.N; x++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := NewInotify(ctx)
if err != nil {
b.Error(err)
}
cancel()
}
}
func TestInotify(t *testing.T) {
ctx := context.Background()
dir, err := ioutil.TempDir("", "TestInotify")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)
defer os.Remove(dir)
t.Run("OpenFile", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
i.AddWatch(dir, IN_ALL_EVENTS)
f, err := os.OpenFile(filepath.Join(dir, "hz"), os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
events, err := i.Read()
if err != nil {
t.Error(err)
}
event := events[0]
if event.Name != filepath.Join(dir, "hz") {
t.Fail()
}
if event.Mask&IN_CREATE == 0 {
t.Fail()
}
t.Logf("%#v", event)
})
t.Run("MultipleEvents", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
i.AddWatch(dir, IN_CLOSE_WRITE)
for x := 0; x < 10; x++ {
fileName := fmt.Sprintf("hz-%d", x)
f, err := os.OpenFile(filepath.Join(dir, fileName), os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
events, err := i.Read()
if err != nil {
t.Error(err)
}
event := events[0]
if event.Mask&IN_CLOSE_WRITE == 0 {
t.Error("Expected IN_CLOSE_WRITE event, but got", event.Mask)
}
if event.Name != filepath.Join(dir, fileName) {
t.Error("Expected event for file", fileName, "but got", event.Name)
}
t.Logf("%#v", event)
}
})
// This test should generate more events than the buffer passed to syscall.Read()
// can handle. This is to test the buffer handling in the ReadDeadline() method.
// The potential bug is that the buffer may contain patial event at the end of the buffer
// and the next syscall.Read() will not be able to read the rest of the event.
t.Run("MultipleEvents #2 - Reading leftover events", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
i.AddWatch(dir, IN_CLOSE_WRITE)
// generate 2* maxEvents events with long filenames (in range from syscall.NAME_MAX-10 to syscall.NAME_MAX)
for x := 0; x < 2*maxEvents; x++ {
fileNameLen := syscall.NAME_MAX - 10 + x%10
// make a filename with len = fileNameLen
fileName := fmt.Sprintf("%s-%d", "hz", x)
fileName = fmt.Sprintf("%s%s", fileName, strings.Repeat("a", fileNameLen-len(fileName)+1))
f, err := os.OpenFile(filepath.Join(dir, fileName), os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
}
// read all events until the expected number of events is reached or the deadline is reached
deadline := time.Now().Add(5 * time.Second)
events := make([]InotifyEvent, 0, 2*maxEvents)
for {
portion, err := i.Read()
if err != nil {
t.Error(err)
}
events = append(events, portion...)
if len(events) >= 2*maxEvents || time.Now().After(deadline) {
break
}
}
// check if all events were read
if len(events) != 2*maxEvents {
t.Errorf("Expected %d events, but got %d", 2*maxEvents, len(events))
}
})
t.Run("SelfFolderEvent", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
subdir := filepath.Join(dir, "subdir")
err = os.Mkdir(subdir, os.ModePerm)
if err != nil {
t.Error(err)
}
i.AddWatch(subdir, IN_ALL_EVENTS)
os.RemoveAll(subdir)
events, err := i.Read()
if err != nil {
t.Error(err)
}
event := events[0]
if event.Name != subdir {
t.Fail()
}
if event.Mask&IN_DELETE_SELF == 0 {
t.Fail()
}
t.Logf("%#v", event)
})
t.Run("Bug #2 Inotify.Read() discards solo events", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
subdir := filepath.Join(dir, "subdir#2_1")
err = os.Mkdir(subdir, os.ModePerm)
if err != nil {
t.Error(err)
}
i.AddWatch(subdir, IN_CREATE)
fileName := "single-file.txt"
f, err := os.OpenFile(filepath.Join(subdir, fileName), os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
events, err := i.Read()
if err != nil {
t.Error(err)
}
expected := 1
if len(events) != expected {
t.Errorf("Length of read events is %d, but extected %d", len(events), expected)
}
})
t.Run("Bug #2 Inotify.Read() discards solo events (case 2)", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
i, err := NewInotify(ctx)
if err != nil {
t.Error(err)
}
subdir := filepath.Join(dir, "subdir#2_2")
err = os.Mkdir(subdir, os.ModePerm)
if err != nil {
t.Error(err)
}
fileName := "single-file.txt"
fullPath := filepath.Join(subdir, fileName)
f, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
t.Error(err)
}
i.AddWatch(fullPath, IN_MODIFY)
f.Write([]byte("Hello world\n"))
f.Close()
events, err := i.Read()
if err != nil {
t.Error(err)
}
expected := 1
if len(events) != expected {
for _, event := range events {
fmt.Printf("Event %#v\n", event)
}
t.Errorf("Length of read events is %d, but extected %d", len(events), expected)
}
})
}