forked from dploeger/fsevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsevents_test.go
173 lines (148 loc) · 3.24 KB
/
fsevents_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
//go:build darwin
// +build darwin
package fsevents
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestBasicExample(t *testing.T) {
path, err := ioutil.TempDir("", "fsexample")
if err != nil {
t.Fatal(err)
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(path)
dev, err := DeviceForPath(path)
if err != nil {
t.Fatal(err)
}
es := &EventStream{
Paths: []string{path},
Latency: 500 * time.Millisecond,
Device: dev,
Flags: FileEvents,
}
err = es.Start()
if err != nil {
t.Fatal(err)
}
wait := make(chan Event)
go func() {
for msg := range es.Events {
for _, event := range msg {
t.Logf("Event: %#v", event)
wait <- event
es.Stop()
return
}
}
}()
err = ioutil.WriteFile(filepath.Join(path, "example.txt"), []byte("example"), 0700)
if err != nil {
t.Fatal(err)
}
select {
case <-wait:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for event")
}
}
func TestIssue48(t *testing.T) {
// FSEvents fails to start when watching >4096 paths
// This test validates that limit and checks that the error is propagated
path, err := ioutil.TempDir("", "fsmanyfiles")
if err != nil {
t.Fatal(err)
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(path)
// TODO: using this value fails to start
// dev, err := DeviceForPath(path)
// if err != nil {
// t.Fatal(err)
// }
var filenames []string
for i := 0; i < 4096; i++ {
newFilename := filepath.Join(path, fmt.Sprint("test", i))
err = ioutil.WriteFile(newFilename, []byte("test"), 0700)
if err != nil {
t.Fatal(err)
}
filenames = append(filenames, newFilename)
}
es := &EventStream{
Paths: filenames,
Latency: 500 * time.Millisecond,
Device: 0, //dev,
Flags: FileEvents,
}
err = es.Start()
if err != nil {
t.Fatal(err)
}
wait := make(chan Event)
go func() {
for msg := range es.Events {
for _, event := range msg {
t.Logf("Event: %#v", event)
wait <- event
es.Stop()
return
}
}
}()
// write some new contents to test42 in the watchlist
err = ioutil.WriteFile(filenames[42], []byte("special"), 0700)
if err != nil {
t.Fatal(err)
}
// should be reported as expected
<-wait
/////
// create one more file that puts it over the edge
newFilename := filepath.Join(path, fmt.Sprint("test", 4096))
err = ioutil.WriteFile(newFilename, []byte("test"), 0700)
if err != nil {
t.Fatal(err)
}
filenames = append(filenames, newFilename)
// create an all-new instances to avoid problems
es = &EventStream{
Paths: filenames,
Latency: 500 * time.Millisecond,
Device: 0, //dev,
Flags: FileEvents,
}
err = es.Start()
if err == nil {
es.Stop()
t.Fatal("eventstream error was not detected on >4096 files in watchlist")
}
}
func TestRegistry(t *testing.T) {
if registry.m == nil {
t.Fatal("registry not initialized at start")
}
es := &EventStream{}
i := registry.Add(es)
if registry.Get(i) == nil {
t.Fatal("failed to retrieve es from registry")
}
if es != registry.Get(i) {
t.Errorf("eventstream did not match what was found in the registry")
}
registry.Delete(i)
if registry.Get(i) != nil {
t.Error("failed to delete registry")
}
}