notify_test.go 7.4 KB

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