notify_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package watch
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/windmilleng/fsnotify"
  12. )
  13. // Each implementation of the notify interface should have the same basic
  14. // behavior.
  15. func TestNoEvents(t *testing.T) {
  16. f := newNotifyFixture(t)
  17. defer f.tearDown()
  18. f.fsync()
  19. f.assertEvents()
  20. }
  21. func TestEventOrdering(t *testing.T) {
  22. f := newNotifyFixture(t)
  23. defer f.tearDown()
  24. count := 8
  25. dirs := make([]string, count)
  26. for i, _ := range dirs {
  27. dir, err := f.root.NewDir("watched")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. dirs[i] = dir.Path()
  32. err = f.notify.Add(dir.Path())
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. f.fsync()
  38. f.events = nil
  39. var expected []fsnotify.Event
  40. for i, dir := range dirs {
  41. base := fmt.Sprintf("%d.txt", i)
  42. p := filepath.Join(dir, base)
  43. err := ioutil.WriteFile(p, []byte(base), os.FileMode(0777))
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. expected = append(expected, create(filepath.Join(dir, base)))
  48. }
  49. f.fsync()
  50. f.filterJustCreateEvents()
  51. f.assertEvents(expected...)
  52. // Check to make sure that the files appeared in the right order.
  53. createEvents := make([]fsnotify.Event, 0, count)
  54. for _, e := range f.events {
  55. if e.Op == fsnotify.Create {
  56. createEvents = append(createEvents, e)
  57. }
  58. }
  59. if len(createEvents) != count {
  60. t.Fatalf("Expected %d create events. Actual: %+v", count, createEvents)
  61. }
  62. for i, event := range createEvents {
  63. base := fmt.Sprintf("%d.txt", i)
  64. p := filepath.Join(dirs[i], base)
  65. if event.Name != p {
  66. t.Fatalf("Expected event %q at %d. Actual: %+v", base, i, createEvents)
  67. }
  68. }
  69. }
  70. func TestWatchesAreRecursive(t *testing.T) {
  71. f := newNotifyFixture(t)
  72. defer f.tearDown()
  73. root, err := f.root.NewDir("root")
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. // add a sub directory
  78. subPath := filepath.Join(root.Path(), "sub")
  79. err = os.MkdirAll(subPath, os.ModePerm)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. // watch parent
  84. err = f.notify.Add(root.Path())
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. f.fsync()
  89. f.events = nil
  90. // change sub directory
  91. changeFilePath := filepath.Join(subPath, "change")
  92. _, err = os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. // we should get notified
  97. f.fsync()
  98. f.assertEvents(create(changeFilePath))
  99. }
  100. func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
  101. f := newNotifyFixture(t)
  102. defer f.tearDown()
  103. root, err := f.root.NewDir("root")
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. // watch parent
  108. err = f.notify.Add(root.Path())
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. f.fsync()
  113. f.events = nil
  114. // add a sub directory
  115. subPath := filepath.Join(root.Path(), "sub")
  116. err = os.MkdirAll(subPath, os.ModePerm)
  117. if err != nil {
  118. f.t.Fatal(err)
  119. }
  120. // change something inside sub directory
  121. changeFilePath := filepath.Join(subPath, "change")
  122. _, err = os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. // we should get notified
  127. f.fsync()
  128. // assert events
  129. f.assertEvents(create(subPath), create(changeFilePath))
  130. }
  131. func TestWatchNonExistentPath(t *testing.T) {
  132. f := newNotifyFixture(t)
  133. defer f.tearDown()
  134. root, err := f.root.NewDir("root")
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. path := filepath.Join(root.Path(), "change")
  139. err = f.notify.Add(path)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. d1 := []byte("hello\ngo\n")
  144. err = ioutil.WriteFile(path, d1, 0644)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. f.fsync()
  149. if runtime.GOOS == "darwin" {
  150. f.assertEvents(create(path))
  151. } else {
  152. f.assertEvents(create(path), write(path))
  153. }
  154. }
  155. func TestRemove(t *testing.T) {
  156. f := newNotifyFixture(t)
  157. defer f.tearDown()
  158. root, err := f.root.NewDir("root")
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. path := filepath.Join(root.Path(), "change")
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. d1 := []byte("hello\ngo\n")
  167. err = ioutil.WriteFile(path, d1, 0644)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. err = f.notify.Add(path)
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. f.fsync()
  176. f.events = nil
  177. err = os.Remove(path)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. f.fsync()
  182. f.assertEvents(remove(path))
  183. }
  184. func TestRemoveAndAddBack(t *testing.T) {
  185. t.Skip("Skipping broken test for now")
  186. f := newNotifyFixture(t)
  187. defer f.tearDown()
  188. root, err := f.root.NewDir("root")
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. path := filepath.Join(root.Path(), "change")
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. d1 := []byte("hello\ngo\n")
  197. err = ioutil.WriteFile(path, d1, 0644)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. err = f.notify.Add(path)
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. err = os.Remove(path)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. f.fsync()
  210. f.assertEvents(remove(path))
  211. f.events = nil
  212. err = ioutil.WriteFile(path, d1, 0644)
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. f.assertEvents(create(path))
  217. }
  218. func TestSingleFile(t *testing.T) {
  219. if runtime.GOOS != "darwin" {
  220. t.Skip("Broken on Linux")
  221. }
  222. f := newNotifyFixture(t)
  223. defer f.tearDown()
  224. root, err := f.root.NewDir("root")
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. path := filepath.Join(root.Path(), "change")
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. d1 := []byte("hello\ngo\n")
  233. err = ioutil.WriteFile(path, d1, 0644)
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. err = f.notify.Add(path)
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. d2 := []byte("hello\nworld\n")
  242. err = ioutil.WriteFile(path, d2, 0644)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. f.fsync()
  247. f.assertEvents(create(path))
  248. }
  249. type notifyFixture struct {
  250. t *testing.T
  251. root *TempDir
  252. watched *TempDir
  253. notify Notify
  254. events []fsnotify.Event
  255. }
  256. func newNotifyFixture(t *testing.T) *notifyFixture {
  257. SetLimitChecksEnabled(false)
  258. notify, err := NewWatcher()
  259. if err != nil {
  260. t.Fatal(err)
  261. }
  262. root, err := NewDir(t.Name())
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. watched, err := root.NewDir("watched")
  267. if err != nil {
  268. t.Fatal(err)
  269. }
  270. err = notify.Add(watched.Path())
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. return &notifyFixture{
  275. t: t,
  276. root: root,
  277. watched: watched,
  278. notify: notify,
  279. }
  280. }
  281. func (f *notifyFixture) filterJustCreateEvents() {
  282. var r []fsnotify.Event
  283. for _, ev := range f.events {
  284. if ev.Op != fsnotify.Create {
  285. continue
  286. }
  287. r = append(r, ev)
  288. }
  289. f.events = r
  290. }
  291. func (f *notifyFixture) assertEvents(expected ...fsnotify.Event) {
  292. if len(f.events) != len(expected) {
  293. f.t.Fatalf("Got %d events (expected %d): %v %v", len(f.events), len(expected), f.events, expected)
  294. }
  295. for i, actual := range f.events {
  296. if actual != expected[i] {
  297. f.t.Fatalf("Got event %v (expected %v)", actual, expected[i])
  298. }
  299. }
  300. }
  301. func create(f string) fsnotify.Event {
  302. return fsnotify.Event{
  303. Name: f,
  304. Op: fsnotify.Create,
  305. }
  306. }
  307. func write(f string) fsnotify.Event {
  308. return fsnotify.Event{
  309. Name: f,
  310. Op: fsnotify.Write,
  311. }
  312. }
  313. func remove(f string) fsnotify.Event {
  314. return fsnotify.Event{
  315. Name: f,
  316. Op: fsnotify.Remove,
  317. }
  318. }
  319. func (f *notifyFixture) fsync() {
  320. syncPathBase := fmt.Sprintf("sync-%d.txt", time.Now().UnixNano())
  321. syncPath := filepath.Join(f.watched.Path(), syncPathBase)
  322. anySyncPath := filepath.Join(f.watched.Path(), "sync-")
  323. timeout := time.After(time.Second)
  324. err := ioutil.WriteFile(syncPath, []byte(fmt.Sprintf("%s", time.Now())), os.FileMode(0777))
  325. if err != nil {
  326. f.t.Fatal(err)
  327. }
  328. F:
  329. for {
  330. select {
  331. case err := <-f.notify.Errors():
  332. f.t.Fatal(err)
  333. case event := <-f.notify.Events():
  334. if strings.Contains(event.Name, syncPath) {
  335. break F
  336. }
  337. if strings.Contains(event.Name, anySyncPath) {
  338. continue
  339. }
  340. f.events = append(f.events, event)
  341. case <-timeout:
  342. f.t.Fatalf("fsync: timeout")
  343. }
  344. }
  345. if err != nil {
  346. f.t.Fatal(err)
  347. }
  348. }
  349. func (f *notifyFixture) tearDown() {
  350. SetLimitChecksEnabled(true)
  351. err := f.root.TearDown()
  352. if err != nil {
  353. f.t.Fatal(err)
  354. }
  355. err = f.notify.Close()
  356. if err != nil {
  357. f.t.Fatal(err)
  358. }
  359. }