inotify_test.go 10 KB

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