Debian Codegraph @

notify_test.go

raw symbols SCIP
1 // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 // +build darwin linux freebsd dragonfly netbsd openbsd windows solaris
6
7 package notify
8
9 import (
10 "io/ioutil"
11 "os"
12 "path/filepath"
13 "testing"
14 "time"
15 )
16
17 func TestNotifyExample(t *testing.T) {
18 n := NewNotifyTest(t, "testdata/vfs.txt")
19 defer n.Close()
20
21 ch := NewChans(3)
22
23 // Watch-points can be set explicitly via Watch/Stop calls...
24 n.Watch("src/github.com/rjeczalik/fs", ch[0], Write)
25 n.Watch("src/github.com/pblaszczyk/qttu", ch[0], Write)
26 n.Watch("src/github.com/pblaszczyk/qttu/...", ch[1], Create)
27 n.Watch("src/github.com/rjeczalik/fs/cmd/...", ch[2], Remove)
28
29 cases := []NCase{
30 // i=0
31 {
32 Event: write(n.W(), "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
33 Receiver: Chans{ch[0]},
34 },
35 // TODO(rjeczalik): #62
36 // i=1
37 // {
38 // Event: write(n.W(), "src/github.com/pblaszczyk/qttu/README.md", []byte("XD")),
39 // Receiver: Chans{ch[0]},
40 // },
41 // i=2
42 {
43 Event: write(n.W(), "src/github.com/rjeczalik/fs/cmd/gotree/go.go", []byte("XD")),
44 Receiver: nil,
45 },
46 // i=3
47 {
48 Event: create(n.W(), "src/github.com/pblaszczyk/qttu/src/.main.cc.swp"),
49 Receiver: Chans{ch[1]},
50 },
51 // i=4
52 {
53 Event: create(n.W(), "src/github.com/pblaszczyk/qttu/src/.main.cc.swo"),
54 Receiver: Chans{ch[1]},
55 },
56 // i=5
57 {
58 Event: remove(n.W(), "src/github.com/rjeczalik/fs/cmd/gotree/go.go"),
59 Receiver: Chans{ch[2]},
60 },
61 }
62
63 n.ExpectNotifyEvents(cases, ch)
64
65 // ...or using Call structures.
66 stops := [...]Call{
67 // i=0
68 {
69 F: FuncStop,
70 C: ch[0],
71 },
72 // i=1
73 {
74 F: FuncStop,
75 C: ch[1],
76 },
77 }
78
79 n.Call(stops[:]...)
80
81 cases = []NCase{
82 // i=0
83 {
84 Event: write(n.W(), "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
85 Receiver: nil,
86 },
87 // i=1
88 {
89 Event: write(n.W(), "src/github.com/pblaszczyk/qttu/README.md", []byte("XD")),
90 Receiver: nil,
91 },
92 // i=2
93 {
94 Event: create(n.W(), "src/github.com/pblaszczyk/qttu/src/.main.cc.swr"),
95 Receiver: nil,
96 },
97 // i=3
98 {
99 Event: remove(n.W(), "src/github.com/rjeczalik/fs/cmd/gotree/main.go"),
100 Receiver: Chans{ch[2]},
101 },
102 }
103
104 n.ExpectNotifyEvents(cases, ch)
105 }
106
107 func TestStop(t *testing.T) {
108 t.Skip("TODO(rjeczalik)")
109 }
110
111 func TestRenameInRoot(t *testing.T) {
112 tmpDir, err := ioutil.TempDir("", "notify_test-")
113 if err != nil {
114 t.Fatal(err)
115 }
116 defer os.RemoveAll(tmpDir)
117
118 c := make(chan EventInfo, 100)
119 first := filepath.Join(tmpDir, "foo")
120 second := filepath.Join(tmpDir, "bar")
121 file := filepath.Join(second, "file")
122
123 mustT(t, os.Mkdir(first, 0777))
124
125 if err := Watch(tmpDir+"/...", c, All); err != nil {
126 t.Fatal(err)
127 }
128 defer Stop(c)
129
130 mustT(t, os.Rename(first, second))
131 time.Sleep(50 * time.Millisecond) // Need some time to process rename.
132 fd, err := os.Create(file)
133 mustT(t, err)
134 fd.Close()
135
136 timeout := time.After(time.Second)
137 for {
138 select {
139 case ev := <-c:
140 if ev.Path() == file {
141 return
142 }
143 t.Log(ev.Path())
144 case <-timeout:
145 t.Fatal("timed out before receiving event")
146 }
147 }
148 }
149
150 func TestRecreated(t *testing.T) {
151 tmpDir, err := ioutil.TempDir("", "notify_test-")
152 if err != nil {
153 t.Fatal(err)
154 }
155 defer os.RemoveAll(tmpDir)
156
157 dir := filepath.Join(tmpDir, "folder")
158 file := filepath.Join(dir, "file")
159
160 // Start watching
161 eventChan := make(chan EventInfo, 1000)
162 mustT(t, Watch(tmpDir+"/...", eventChan, All))
163 defer Stop(eventChan)
164
165 recreateFolder := func() {
166 // Give the sync some time to process events
167 _ = os.RemoveAll(dir)
168 mustT(t, os.Mkdir(dir, 0777))
169 time.Sleep(100 * time.Millisecond)
170
171 // Create a file
172 mustT(t, ioutil.WriteFile(file, []byte("abc"), 0666))
173 }
174 timeout := time.After(5 * time.Second)
175 checkCreated := func() {
176 for {
177 select {
178 case ev := <-eventChan:
179 t.Log(ev.Path(), ev.Event())
180 if ev.Path() == file && ev.Event() == Create {
181 return
182 }
183 case <-timeout:
184 t.Fatal("timed out before receiving event")
185 }
186 }
187 }
188
189 // 1. Create a folder and a file within it
190 // This will create a node in the internal tree for the subfolder test/folder
191 // Will create a new inotify watch for the folder
192 t.Log("######## First ########")
193 recreateFolder()
194 checkCreated()
195
196 // 2. Create a folder and a file within it again
197 // This will set the events for the subfolder test/folder in the internal tree
198 // Will create a new inotify watch for the folder because events differ
199 t.Log("######## Second ########")
200 recreateFolder()
201 checkCreated()
202
203 // 3. Create a folder and a file within it yet again
204 // This time no new inotify watch will be created, because the events
205 // and node already exist in the internal tree and all subsequent events
206 // are lost, hence there is no event for the created file here anymore
207 t.Log("######## Third ########")
208 recreateFolder()
209 checkCreated()
210 }
211
212 func mustT(t testing.TB, err error) {
213 t.Helper()
214 if err != nil {
215 t.Fatal(err)
216 }
217 }