-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher_test.go
151 lines (126 loc) · 3.87 KB
/
watcher_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
package main
import (
"os"
"testing"
"github.com/fsnotify/fsnotify"
)
const (
testContent = `
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
this is a content this is a content this is a content this is a content
`
)
func TestInvalidWatcherInstanciation(t *testing.T) {
if _, err := newWatcher("", nil, nil); err == nil {
t.Fatal("watcher instanciation should have failed, invalid path: received empty string")
}
if _, err := newWatcher("dummy", nil, nil); err == nil {
t.Fatal("watcher instanciation should have failed, invalid events channel: received nil")
}
eventsChannel := make(chan *event)
if _, err := newWatcher("dummy", eventsChannel, nil); err == nil {
t.Fatal("watcher instanciation should have failed, invalid errors channel: received nil")
}
errorsChannel := make(chan error)
if _, err := newWatcher("dummy", eventsChannel, errorsChannel); err == nil {
t.Fatal("watcher instanciation should have failed, invalid path: does not exist or incorrect permissions")
}
}
func TestWatcherBehavior(t *testing.T) {
evch := make(chan *event)
errch := make(chan error)
testWatcher, err := newWatcher(testDataPath, evch, errch)
if err != nil {
t.Fatal(err)
}
triggerWrite := make(chan chan struct{}, 1)
triggerRename := make(chan chan struct{}, 1)
triggerRemove := make(chan chan struct{}, 1)
witness := make(chan struct{}, 1)
go testWatcher.watch()
go func(triggerWrite chan chan struct{}) {
writeDone := <-triggerWrite
writeFile(testDataPath+"/foo", testContent, errch)
writeDone <- struct{}{}
}(triggerWrite)
go func(triggerRename chan chan struct{}) {
renameDone := <-triggerRename
renameFile(testDataPath+"/foo", testDataPath+"/bar", errch)
renameDone <- struct{}{}
}(triggerRename)
go func(triggerRemove chan chan struct{}) {
removeDone := <-triggerRemove
removeFile(testDataPath+"/bar", errch)
removeDone <- struct{}{}
}(triggerRemove)
triggerWrite <- witness
<-witness
close(triggerWrite)
processWatcherResult(t, fsnotify.Create, evch)
triggerRename <- witness
<-witness
close(triggerRename)
processWatcherResult(t, fsnotify.Rename, evch)
triggerRemove <- witness
<-witness
close(triggerRemove)
processWatcherResult(t, fsnotify.Remove, evch)
if err := testWatcher.stop(); err != nil {
t.Fatal(err)
}
close(errch)
close(evch)
close(witness)
}
func processWatcherResult(t *testing.T, op fsnotify.Op, ch chan *event) {
event, ok := <-ch
if !ok || event == nil {
t.Fatal("unexpected error occurred on event channel")
}
if timestamp, ok := event.trace[op]; !ok || len(timestamp) == 0 {
t.Fatalf("event %s should have been detected and timestamp should not be empty", op.String())
}
}
func writeFile(path, content string, errchan chan error) {
var f *os.File
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
errchan <- err
return
}
if err := f.Sync(); err != nil {
errchan <- err
return
}
if _, err := f.WriteString(content); err != nil {
errchan <- err
return
}
if err := f.Sync(); err != nil {
errchan <- err
return
}
if err := f.Close(); err != nil {
errchan <- err
return
}
}
func renameFile(oldpath, newpath string, errchan chan error) {
if err := os.Rename(oldpath, newpath); err != nil {
errchan <- err
return
}
}
func removeFile(path string, errchan chan error) {
if err := os.Remove(path); err != nil {
errchan <- err
return
}
}