inotify_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build linux
  5. package fsnotify
  6. import (
  7. "fmt"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. func TestInotifyCloseRightAway(t *testing.T) {
  15. w, err := NewWatcher()
  16. if err != nil {
  17. t.Fatalf("Failed to create watcher")
  18. }
  19. // Close immediately; it won't even reach the first unix.Read.
  20. w.Close()
  21. // Wait for the close to complete.
  22. <-time.After(50 * time.Millisecond)
  23. isWatcherReallyClosed(t, w)
  24. }
  25. func TestInotifyCloseSlightlyLater(t *testing.T) {
  26. w, err := NewWatcher()
  27. if err != nil {
  28. t.Fatalf("Failed to create watcher")
  29. }
  30. // Wait until readEvents has reached unix.Read, and Close.
  31. <-time.After(50 * time.Millisecond)
  32. w.Close()
  33. // Wait for the close to complete.
  34. <-time.After(50 * time.Millisecond)
  35. isWatcherReallyClosed(t, w)
  36. }
  37. func TestInotifyCloseSlightlyLaterWithWatch(t *testing.T) {
  38. testDir := tempMkdir(t)
  39. defer os.RemoveAll(testDir)
  40. w, err := NewWatcher()
  41. if err != nil {
  42. t.Fatalf("Failed to create watcher")
  43. }
  44. w.Add(testDir)
  45. // Wait until readEvents has reached unix.Read, and Close.
  46. <-time.After(50 * time.Millisecond)
  47. w.Close()
  48. // Wait for the close to complete.
  49. <-time.After(50 * time.Millisecond)
  50. isWatcherReallyClosed(t, w)
  51. }
  52. func TestInotifyCloseAfterRead(t *testing.T) {
  53. testDir := tempMkdir(t)
  54. defer os.RemoveAll(testDir)
  55. w, err := NewWatcher()
  56. if err != nil {
  57. t.Fatalf("Failed to create watcher")
  58. }
  59. err = w.Add(testDir)
  60. if err != nil {
  61. t.Fatalf("Failed to add .")
  62. }
  63. // Generate an event.
  64. os.Create(filepath.Join(testDir, "somethingSOMETHINGsomethingSOMETHING"))
  65. // Wait for readEvents to read the event, then close the watcher.
  66. <-time.After(50 * time.Millisecond)
  67. w.Close()
  68. // Wait for the close to complete.
  69. <-time.After(50 * time.Millisecond)
  70. isWatcherReallyClosed(t, w)
  71. }
  72. func isWatcherReallyClosed(t *testing.T, w *Watcher) {
  73. select {
  74. case err, ok := <-w.Errors:
  75. if ok {
  76. t.Fatalf("w.Errors is not closed; readEvents is still alive after closing (error: %v)", err)
  77. }
  78. default:
  79. t.Fatalf("w.Errors would have blocked; readEvents is still alive!")
  80. }
  81. select {
  82. case _, ok := <-w.Events:
  83. if ok {
  84. t.Fatalf("w.Events is not closed; readEvents is still alive after closing")
  85. }
  86. default:
  87. t.Fatalf("w.Events would have blocked; readEvents is still alive!")
  88. }
  89. }
  90. func TestInotifyCloseCreate(t *testing.T) {
  91. testDir := tempMkdir(t)
  92. defer os.RemoveAll(testDir)
  93. w, err := NewWatcher()
  94. if err != nil {
  95. t.Fatalf("Failed to create watcher: %v", err)
  96. }
  97. defer w.Close()
  98. err = w.Add(testDir)
  99. if err != nil {
  100. t.Fatalf("Failed to add testDir: %v", err)
  101. }
  102. h, err := os.Create(filepath.Join(testDir, "testfile"))
  103. if err != nil {
  104. t.Fatalf("Failed to create file in testdir: %v", err)
  105. }
  106. h.Close()
  107. select {
  108. case _ = <-w.Events:
  109. case err := <-w.Errors:
  110. t.Fatalf("Error from watcher: %v", err)
  111. case <-time.After(50 * time.Millisecond):
  112. t.Fatalf("Took too long to wait for event")
  113. }
  114. // At this point, we've received one event, so the goroutine is ready.
  115. // It's also blocking on unix.Read.
  116. // Now we try to swap the file descriptor under its nose.
  117. w.Close()
  118. w, err = NewWatcher()
  119. defer w.Close()
  120. if err != nil {
  121. t.Fatalf("Failed to create second watcher: %v", err)
  122. }
  123. <-time.After(50 * time.Millisecond)
  124. err = w.Add(testDir)
  125. if err != nil {
  126. t.Fatalf("Error adding testDir again: %v", err)
  127. }
  128. }
  129. // This test verifies the watcher can keep up with file creations/deletions
  130. // when under load.
  131. func TestInotifyStress(t *testing.T) {
  132. maxNumToCreate := 1000
  133. testDir := tempMkdir(t)
  134. defer os.RemoveAll(testDir)
  135. testFilePrefix := filepath.Join(testDir, "testfile")
  136. w, err := NewWatcher()
  137. if err != nil {
  138. t.Fatalf("Failed to create watcher: %v", err)
  139. }
  140. defer w.Close()
  141. err = w.Add(testDir)
  142. if err != nil {
  143. t.Fatalf("Failed to add testDir: %v", err)
  144. }
  145. doneChan := make(chan struct{})
  146. // The buffer ensures that the file generation goroutine is never blocked.
  147. errChan := make(chan error, 2*maxNumToCreate)
  148. go func() {
  149. for i := 0; i < maxNumToCreate; i++ {
  150. testFile := fmt.Sprintf("%s%d", testFilePrefix, i)
  151. handle, err := os.Create(testFile)
  152. if err != nil {
  153. errChan <- fmt.Errorf("Create failed: %v", err)
  154. continue
  155. }
  156. err = handle.Close()
  157. if err != nil {
  158. errChan <- fmt.Errorf("Close failed: %v", err)
  159. continue
  160. }
  161. }
  162. // If we delete a newly created file too quickly, inotify will skip the
  163. // create event and only send the delete event.
  164. time.Sleep(100 * time.Millisecond)
  165. for i := 0; i < maxNumToCreate; i++ {
  166. testFile := fmt.Sprintf("%s%d", testFilePrefix, i)
  167. err = os.Remove(testFile)
  168. if err != nil {
  169. errChan <- fmt.Errorf("Remove failed: %v", err)
  170. }
  171. }
  172. close(doneChan)
  173. }()
  174. creates := 0
  175. removes := 0
  176. finished := false
  177. after := time.After(10 * time.Second)
  178. for !finished {
  179. select {
  180. case <-after:
  181. t.Fatalf("Not done")
  182. case <-doneChan:
  183. finished = true
  184. case err := <-errChan:
  185. t.Fatalf("Got an error from file creator goroutine: %v", err)
  186. case err := <-w.Errors:
  187. t.Fatalf("Got an error from watcher: %v", err)
  188. case evt := <-w.Events:
  189. if !strings.HasPrefix(evt.Name, testFilePrefix) {
  190. t.Fatalf("Got an event for an unknown file: %s", evt.Name)
  191. }
  192. if evt.Op == Create {
  193. creates++
  194. }
  195. if evt.Op == Remove {
  196. removes++
  197. }
  198. }
  199. }
  200. // Drain remaining events from channels
  201. count := 0
  202. for count < 10 {
  203. select {
  204. case err := <-errChan:
  205. t.Fatalf("Got an error from file creator goroutine: %v", err)
  206. case err := <-w.Errors:
  207. t.Fatalf("Got an error from watcher: %v", err)
  208. case evt := <-w.Events:
  209. if !strings.HasPrefix(evt.Name, testFilePrefix) {
  210. t.Fatalf("Got an event for an unknown file: %s", evt.Name)
  211. }
  212. if evt.Op == Create {
  213. creates++
  214. }
  215. if evt.Op == Remove {
  216. removes++
  217. }
  218. count = 0
  219. default:
  220. count++
  221. // Give the watcher chances to fill the channels.
  222. time.Sleep(time.Millisecond)
  223. }
  224. }
  225. if creates-removes > 1 || creates-removes < -1 {
  226. t.Fatalf("Creates and removes should not be off by more than one: %d creates, %d removes", creates, removes)
  227. }
  228. if creates < 50 {
  229. t.Fatalf("Expected at least 50 creates, got %d", creates)
  230. }
  231. }
  232. func TestInotifyRemoveTwice(t *testing.T) {
  233. testDir := tempMkdir(t)
  234. defer os.RemoveAll(testDir)
  235. testFile := filepath.Join(testDir, "testfile")
  236. handle, err := os.Create(testFile)
  237. if err != nil {
  238. t.Fatalf("Create failed: %v", err)
  239. }
  240. handle.Close()
  241. w, err := NewWatcher()
  242. if err != nil {
  243. t.Fatalf("Failed to create watcher: %v", err)
  244. }
  245. defer w.Close()
  246. err = w.Add(testFile)
  247. if err != nil {
  248. t.Fatalf("Failed to add testFile: %v", err)
  249. }
  250. err = w.Remove(testFile)
  251. if err != nil {
  252. t.Fatalf("wanted successful remove but got: %v", err)
  253. }
  254. err = w.Remove(testFile)
  255. if err == nil {
  256. t.Fatalf("no error on removing invalid file")
  257. }
  258. w.mu.Lock()
  259. defer w.mu.Unlock()
  260. if len(w.watches) != 0 {
  261. t.Fatalf("Expected watches len is 0, but got: %d, %v", len(w.watches), w.watches)
  262. }
  263. if len(w.paths) != 0 {
  264. t.Fatalf("Expected paths len is 0, but got: %d, %v", len(w.paths), w.paths)
  265. }
  266. }
  267. func TestInotifyInnerMapLength(t *testing.T) {
  268. testDir := tempMkdir(t)
  269. defer os.RemoveAll(testDir)
  270. testFile := filepath.Join(testDir, "testfile")
  271. handle, err := os.Create(testFile)
  272. if err != nil {
  273. t.Fatalf("Create failed: %v", err)
  274. }
  275. handle.Close()
  276. w, err := NewWatcher()
  277. if err != nil {
  278. t.Fatalf("Failed to create watcher: %v", err)
  279. }
  280. defer w.Close()
  281. err = w.Add(testFile)
  282. if err != nil {
  283. t.Fatalf("Failed to add testFile: %v", err)
  284. }
  285. go func() {
  286. for err := range w.Errors {
  287. t.Fatalf("error received: %s", err)
  288. }
  289. }()
  290. err = os.Remove(testFile)
  291. if err != nil {
  292. t.Fatalf("Failed to remove testFile: %v", err)
  293. }
  294. _ = <-w.Events // consume Remove event
  295. <-time.After(50 * time.Millisecond) // wait IN_IGNORE propagated
  296. w.mu.Lock()
  297. defer w.mu.Unlock()
  298. if len(w.watches) != 0 {
  299. t.Fatalf("Expected watches len is 0, but got: %d, %v", len(w.watches), w.watches)
  300. }
  301. if len(w.paths) != 0 {
  302. t.Fatalf("Expected paths len is 0, but got: %d, %v", len(w.paths), w.paths)
  303. }
  304. }
  305. func TestInotifyOverflow(t *testing.T) {
  306. // We need to generate many more events than the
  307. // fs.inotify.max_queued_events sysctl setting.
  308. // We use multiple goroutines (one per directory)
  309. // to speed up file creation.
  310. numDirs := 128
  311. numFiles := 1024
  312. testDir := tempMkdir(t)
  313. defer os.RemoveAll(testDir)
  314. w, err := NewWatcher()
  315. if err != nil {
  316. t.Fatalf("Failed to create watcher: %v", err)
  317. }
  318. defer w.Close()
  319. for dn := 0; dn < numDirs; dn++ {
  320. testSubdir := fmt.Sprintf("%s/%d", testDir, dn)
  321. err := os.Mkdir(testSubdir, 0777)
  322. if err != nil {
  323. t.Fatalf("Cannot create subdir: %v", err)
  324. }
  325. err = w.Add(testSubdir)
  326. if err != nil {
  327. t.Fatalf("Failed to add subdir: %v", err)
  328. }
  329. }
  330. errChan := make(chan error, numDirs*numFiles)
  331. for dn := 0; dn < numDirs; dn++ {
  332. testSubdir := fmt.Sprintf("%s/%d", testDir, dn)
  333. go func() {
  334. for fn := 0; fn < numFiles; fn++ {
  335. testFile := fmt.Sprintf("%s/%d", testSubdir, fn)
  336. handle, err := os.Create(testFile)
  337. if err != nil {
  338. errChan <- fmt.Errorf("Create failed: %v", err)
  339. continue
  340. }
  341. err = handle.Close()
  342. if err != nil {
  343. errChan <- fmt.Errorf("Close failed: %v", err)
  344. continue
  345. }
  346. }
  347. }()
  348. }
  349. creates := 0
  350. overflows := 0
  351. after := time.After(10 * time.Second)
  352. for overflows == 0 && creates < numDirs*numFiles {
  353. select {
  354. case <-after:
  355. t.Fatalf("Not done")
  356. case err := <-errChan:
  357. t.Fatalf("Got an error from file creator goroutine: %v", err)
  358. case err := <-w.Errors:
  359. if err == ErrEventOverflow {
  360. overflows++
  361. } else {
  362. t.Fatalf("Got an error from watcher: %v", err)
  363. }
  364. case evt := <-w.Events:
  365. if !strings.HasPrefix(evt.Name, testDir) {
  366. t.Fatalf("Got an event for an unknown file: %s", evt.Name)
  367. }
  368. if evt.Op == Create {
  369. creates++
  370. }
  371. }
  372. }
  373. if creates == numDirs*numFiles {
  374. t.Fatalf("Could not trigger overflow")
  375. }
  376. if overflows == 0 {
  377. t.Fatalf("No overflow and not enough creates (expected %d, got %d)",
  378. numDirs*numFiles, creates)
  379. }
  380. }