-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
kqueue_test.go
90 lines (75 loc) · 1.64 KB
/
kqueue_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
// +build freebsd netbsd openbsd dragonfly darwin
package immortal
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"time"
)
func TestWatchDir(t *testing.T) {
ch := make(chan string)
dir, err := ioutil.TempDir("", "TestWatchDir")
if err != nil {
t.Error(err)
}
go WatchDir(dir, ch)
defer os.RemoveAll(dir) // clean up
time.Sleep(100 * time.Millisecond)
tmpfn := filepath.Join(dir, "tmpfile")
if err = ioutil.WriteFile(tmpfn, []byte("something"), 0640); err != nil {
t.Error(err)
}
select {
case <-ch:
return
case <-time.After(time.Second):
t.Error("Expecting struct on channel")
}
}
func TestWatchDirBadDir(t *testing.T) {
ch := make(chan string)
err := WatchDir("/dev/null/none/existent", ch)
if err == nil {
t.Error("Expecting error")
}
}
func TestWatchFile(t *testing.T) {
ch := make(chan string, 1)
tmpfile, err := ioutil.TempFile("", "TestWatchFile")
if err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Write([]byte("something")); err != nil {
t.Error(err)
}
if err := tmpfile.Close(); err != nil {
t.Error(err)
}
defer os.Remove(tmpfile.Name()) // clean up
go func() {
err := WatchFile(tmpfile.Name(), ch)
if err != nil {
t.Error(err)
}
}()
time.Sleep(100 * time.Millisecond)
err = ioutil.WriteFile(tmpfile.Name(), []byte("foo"), 0644)
if err != nil {
t.Error(err)
}
select {
case file := <-ch:
expect(t, file, tmpfile.Name())
case <-time.After(time.Second):
t.Error("timeout waiting for channel")
}
}
func TestWatchFileBadFile(t *testing.T) {
ch := make(chan string, 1)
err := WatchFile("/dev/null/none-existen", ch)
if err == nil {
t.Error("Expecting error")
}
}