| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 31 |
{ |
| 32 |
Event: write(n.W(), "src/github.com/rjeczalik/fs/fs.go", []byte("XD")), |
| 33 |
Receiver: Chans{ch[0]}, |
| 34 |
}, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
{ |
| 43 |
Event: write(n.W(), "src/github.com/rjeczalik/fs/cmd/gotree/go.go", []byte("XD")), |
| 44 |
Receiver: nil, |
| 45 |
}, |
| 46 |
|
| 47 |
{ |
| 48 |
Event: create(n.W(), "src/github.com/pblaszczyk/qttu/src/.main.cc.swp"), |
| 49 |
Receiver: Chans{ch[1]}, |
| 50 |
}, |
| 51 |
|
| 52 |
{ |
| 53 |
Event: create(n.W(), "src/github.com/pblaszczyk/qttu/src/.main.cc.swo"), |
| 54 |
Receiver: Chans{ch[1]}, |
| 55 |
}, |
| 56 |
|
| 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 |
|
| 66 |
stops := [...]Call{ |
| 67 |
|
| 68 |
{ |
| 69 |
F: FuncStop, |
| 70 |
C: ch[0], |
| 71 |
}, |
| 72 |
|
| 73 |
{ |
| 74 |
F: FuncStop, |
| 75 |
C: ch[1], |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
n.Call(stops[:]...) |
| 80 |
|
| 81 |
cases = []NCase{ |
| 82 |
|
| 83 |
{ |
| 84 |
Event: write(n.W(), "src/github.com/rjeczalik/fs/fs.go", []byte("XD")), |
| 85 |
Receiver: nil, |
| 86 |
}, |
| 87 |
|
| 88 |
{ |
| 89 |
Event: write(n.W(), "src/github.com/pblaszczyk/qttu/README.md", []byte("XD")), |
| 90 |
Receiver: nil, |
| 91 |
}, |
| 92 |
|
| 93 |
{ |
| 94 |
Event: create(n.W(), "src/github.com/pblaszczyk/qttu/src/.main.cc.swr"), |
| 95 |
Receiver: nil, |
| 96 |
}, |
| 97 |
|
| 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) |
| 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 |
|
| 161 |
eventChan := make(chan EventInfo, 1000) |
| 162 |
mustT(t, Watch(tmpDir+"/...", eventChan, All)) |
| 163 |
defer Stop(eventChan) |
| 164 |
|
| 165 |
recreateFolder := func() { |
| 166 |
|
| 167 |
_ = os.RemoveAll(dir) |
| 168 |
mustT(t, os.Mkdir(dir, 0777)) |
| 169 |
time.Sleep(100 * time.Millisecond) |
| 170 |
|
| 171 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
t.Log("######## First ########") |
| 193 |
recreateFolder() |
| 194 |
checkCreated() |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
t.Log("######## Second ########") |
| 200 |
recreateFolder() |
| 201 |
checkCreated() |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 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 |
} |