-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfilewatcher_test.go
161 lines (123 loc) · 3.21 KB
/
filewatcher_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
package gonotify
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
func TestFileWatcher(t *testing.T) {
ctx := context.Background()
dir, err := ioutil.TempDir("", "TestFileWatcher")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)
defer os.Remove(dir)
t.Run("Simple", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
f1 := filepath.Join(dir, "/dir1/foo")
f2 := filepath.Join(dir, "/dir2/bar")
os.MkdirAll(filepath.Dir(f1), os.ModePerm)
os.MkdirAll(filepath.Dir(f2), os.ModePerm)
fw, err := NewFileWatcher(ctx, IN_ALL_EVENTS, f1, f2)
if err != nil {
t.Error(err)
}
{
f, err := os.OpenFile(f1, os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
}
{
f, err := os.OpenFile(f2, os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
}
// 3 events per file
//TODO checks
for x := 0; x < 6; x++ {
e := <-fw.C
t.Logf("%#v\n", e)
//fmt.Printf("%#v\n", e)
}
cancel()
select {
case <-fw.Done():
case <-time.After(5 * time.Second):
// 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])
t.Error("FileWatcher did not close")
}
})
t.Run("ClosedFileWatcherWithNotConsumedEvents", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
f1 := filepath.Join(dir, "/dir1/foo1")
f2 := filepath.Join(dir, "/dir2/bar2")
os.MkdirAll(filepath.Dir(f1), os.ModePerm)
os.MkdirAll(filepath.Dir(f2), os.ModePerm)
fw, err := NewFileWatcher(ctx, IN_ALL_EVENTS, f1, f2)
if err != nil {
t.Error(err)
}
{
f, err := os.OpenFile(f1, os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
}
{
f, err := os.OpenFile(f2, os.O_RDWR|os.O_CREATE, 0)
if err != nil {
t.Error(err)
}
f.Close()
}
cancel() // close the FileWatcher, but not consume all events
// the FileWatcher should close and the events should be discarded,
// so that fw.Done() should be closed very soon
select {
case <-fw.Done():
case <-time.After(5 * time.Second):
// 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])
t.Error("FileWatcher did not close")
}
})
t.Run("ClosedFileWatcherHasClosedChannel", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
fw, err := NewFileWatcher(ctx, IN_ALL_EVENTS, filepath.Join(dir, "foo"))
if err != nil {
t.Error(err)
}
go func() {
for e := range fw.C {
t.Logf("Event received: %v", e)
}
}()
cancel()
select {
case <-fw.Done():
case <-time.After(5 * time.Second):
// 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])
t.Error("FileWatcher did not close")
}
})
}