test-fs-event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. #include <fcntl.h>
  25. #ifndef HAVE_KQUEUE
  26. # if defined(__APPLE__) || \
  27. defined(__DragonFly__) || \
  28. defined(__FreeBSD__) || \
  29. defined(__OpenBSD__) || \
  30. defined(__NetBSD__)
  31. # define HAVE_KQUEUE 1
  32. # endif
  33. #endif
  34. static uv_fs_event_t fs_event;
  35. static uv_timer_t timer;
  36. static int timer_cb_called = 0;
  37. static int close_cb_called = 0;
  38. static int fs_event_cb_called = 0;
  39. static int timer_cb_touch_called = 0;
  40. static void create_dir(uv_loop_t* loop, const char* name) {
  41. int r;
  42. uv_fs_t req;
  43. r = uv_fs_mkdir(loop, &req, name, 0755, NULL);
  44. ASSERT(r == 0 || uv_last_error(loop).code == UV_EEXIST);
  45. uv_fs_req_cleanup(&req);
  46. }
  47. static void create_file(uv_loop_t* loop, const char* name) {
  48. int r;
  49. uv_file file;
  50. uv_fs_t req;
  51. r = uv_fs_open(loop, &req, name, O_WRONLY | O_CREAT,
  52. S_IWRITE | S_IREAD, NULL);
  53. ASSERT(r != -1);
  54. file = r;
  55. uv_fs_req_cleanup(&req);
  56. r = uv_fs_close(loop, &req, file, NULL);
  57. ASSERT(r == 0);
  58. uv_fs_req_cleanup(&req);
  59. }
  60. static void touch_file(uv_loop_t* loop, const char* name) {
  61. int r;
  62. uv_file file;
  63. uv_fs_t req;
  64. r = uv_fs_open(loop, &req, name, O_RDWR, 0, NULL);
  65. ASSERT(r != -1);
  66. file = r;
  67. uv_fs_req_cleanup(&req);
  68. r = uv_fs_write(loop, &req, file, "foo", 4, -1, NULL);
  69. ASSERT(r != -1);
  70. uv_fs_req_cleanup(&req);
  71. r = uv_fs_close(loop, &req, file, NULL);
  72. ASSERT(r != -1);
  73. uv_fs_req_cleanup(&req);
  74. }
  75. static void close_cb(uv_handle_t* handle) {
  76. ASSERT(handle != NULL);
  77. close_cb_called++;
  78. }
  79. static void fail_cb(uv_fs_event_t* handle,
  80. const char* path,
  81. int events,
  82. int status) {
  83. ASSERT(0 && "fail_cb called");
  84. }
  85. static void fs_event_cb_dir(uv_fs_event_t* handle, const char* filename,
  86. int events, int status) {
  87. ++fs_event_cb_called;
  88. ASSERT(handle == &fs_event);
  89. ASSERT(status == 0);
  90. ASSERT(events == UV_RENAME);
  91. ASSERT(filename == NULL || strcmp(filename, "file1") == 0);
  92. uv_close((uv_handle_t*)handle, close_cb);
  93. }
  94. static void fs_event_cb_file(uv_fs_event_t* handle, const char* filename,
  95. int events, int status) {
  96. ++fs_event_cb_called;
  97. ASSERT(handle == &fs_event);
  98. ASSERT(status == 0);
  99. ASSERT(events == UV_CHANGE);
  100. ASSERT(filename == NULL || strcmp(filename, "file2") == 0);
  101. uv_close((uv_handle_t*)handle, close_cb);
  102. }
  103. static void timer_cb_close_handle(uv_timer_t* timer, int status) {
  104. uv_handle_t* handle;
  105. ASSERT(timer != NULL);
  106. ASSERT(status == 0);
  107. handle = timer->data;
  108. uv_close((uv_handle_t*)timer, NULL);
  109. uv_close((uv_handle_t*)handle, close_cb);
  110. }
  111. static void fs_event_cb_file_current_dir(uv_fs_event_t* handle,
  112. const char* filename, int events, int status) {
  113. ASSERT(fs_event_cb_called == 0);
  114. ++fs_event_cb_called;
  115. ASSERT(handle == &fs_event);
  116. ASSERT(status == 0);
  117. ASSERT(events == UV_CHANGE);
  118. ASSERT(filename == NULL || strcmp(filename, "watch_file") == 0);
  119. /* Regression test for SunOS: touch should generate just one event. */
  120. {
  121. static uv_timer_t timer;
  122. uv_timer_init(handle->loop, &timer);
  123. timer.data = handle;
  124. uv_timer_start(&timer, timer_cb_close_handle, 250, 0);
  125. }
  126. }
  127. static void timer_cb_dir(uv_timer_t* handle, int status) {
  128. ++timer_cb_called;
  129. create_file(handle->loop, "watch_dir/file1");
  130. uv_close((uv_handle_t*)handle, close_cb);
  131. }
  132. static void timer_cb_file(uv_timer_t* handle, int status) {
  133. ++timer_cb_called;
  134. if (timer_cb_called == 1) {
  135. touch_file(handle->loop, "watch_dir/file1");
  136. } else {
  137. touch_file(handle->loop, "watch_dir/file2");
  138. uv_close((uv_handle_t*)handle, close_cb);
  139. }
  140. }
  141. static void timer_cb_touch(uv_timer_t* timer, int status) {
  142. ASSERT(status == 0);
  143. uv_close((uv_handle_t*)timer, NULL);
  144. touch_file(timer->loop, "watch_file");
  145. timer_cb_touch_called++;
  146. }
  147. static void timer_cb_watch_twice(uv_timer_t* handle, int status) {
  148. uv_fs_event_t* handles = handle->data;
  149. uv_close((uv_handle_t*) (handles + 0), NULL);
  150. uv_close((uv_handle_t*) (handles + 1), NULL);
  151. uv_close((uv_handle_t*) handle, NULL);
  152. }
  153. TEST_IMPL(fs_event_watch_dir) {
  154. uv_loop_t* loop = uv_default_loop();
  155. int r;
  156. /* Setup */
  157. remove("watch_dir/file2");
  158. remove("watch_dir/file1");
  159. remove("watch_dir/");
  160. create_dir(loop, "watch_dir");
  161. r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_dir, 0);
  162. ASSERT(r != -1);
  163. r = uv_timer_init(loop, &timer);
  164. ASSERT(r != -1);
  165. r = uv_timer_start(&timer, timer_cb_dir, 100, 0);
  166. ASSERT(r != -1);
  167. uv_run(loop, UV_RUN_DEFAULT);
  168. ASSERT(fs_event_cb_called == 1);
  169. ASSERT(timer_cb_called == 1);
  170. ASSERT(close_cb_called == 2);
  171. /* Cleanup */
  172. remove("watch_dir/file2");
  173. remove("watch_dir/file1");
  174. remove("watch_dir/");
  175. MAKE_VALGRIND_HAPPY();
  176. return 0;
  177. }
  178. TEST_IMPL(fs_event_watch_file) {
  179. uv_loop_t* loop = uv_default_loop();
  180. int r;
  181. /* Setup */
  182. remove("watch_dir/file2");
  183. remove("watch_dir/file1");
  184. remove("watch_dir/");
  185. create_dir(loop, "watch_dir");
  186. create_file(loop, "watch_dir/file1");
  187. create_file(loop, "watch_dir/file2");
  188. r = uv_fs_event_init(loop, &fs_event, "watch_dir/file2", fs_event_cb_file, 0);
  189. ASSERT(r != -1);
  190. r = uv_timer_init(loop, &timer);
  191. ASSERT(r != -1);
  192. r = uv_timer_start(&timer, timer_cb_file, 100, 100);
  193. ASSERT(r != -1);
  194. uv_run(loop, UV_RUN_DEFAULT);
  195. ASSERT(fs_event_cb_called == 1);
  196. ASSERT(timer_cb_called == 2);
  197. ASSERT(close_cb_called == 2);
  198. /* Cleanup */
  199. remove("watch_dir/file2");
  200. remove("watch_dir/file1");
  201. remove("watch_dir/");
  202. MAKE_VALGRIND_HAPPY();
  203. return 0;
  204. }
  205. TEST_IMPL(fs_event_watch_file_twice) {
  206. const char path[] = "test/fixtures/empty_file";
  207. uv_fs_event_t watchers[2];
  208. uv_timer_t timer;
  209. uv_loop_t* loop;
  210. loop = uv_default_loop();
  211. timer.data = watchers;
  212. ASSERT(0 == uv_fs_event_init(loop, watchers + 0, path, fail_cb, 0));
  213. ASSERT(0 == uv_fs_event_init(loop, watchers + 1, path, fail_cb, 0));
  214. ASSERT(0 == uv_timer_init(loop, &timer));
  215. ASSERT(0 == uv_timer_start(&timer, timer_cb_watch_twice, 10, 0));
  216. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  217. MAKE_VALGRIND_HAPPY();
  218. return 0;
  219. }
  220. TEST_IMPL(fs_event_watch_file_current_dir) {
  221. uv_timer_t timer;
  222. uv_loop_t* loop;
  223. int r;
  224. loop = uv_default_loop();
  225. /* Setup */
  226. remove("watch_file");
  227. create_file(loop, "watch_file");
  228. r = uv_fs_event_init(loop, &fs_event, "watch_file",
  229. fs_event_cb_file_current_dir, 0);
  230. ASSERT(r != -1);
  231. r = uv_timer_init(loop, &timer);
  232. ASSERT(r == 0);
  233. r = uv_timer_start(&timer, timer_cb_touch, 1, 0);
  234. ASSERT(r == 0);
  235. ASSERT(timer_cb_touch_called == 0);
  236. ASSERT(fs_event_cb_called == 0);
  237. ASSERT(close_cb_called == 0);
  238. uv_run(loop, UV_RUN_DEFAULT);
  239. ASSERT(timer_cb_touch_called == 1);
  240. ASSERT(fs_event_cb_called == 1);
  241. ASSERT(close_cb_called == 1);
  242. /* Cleanup */
  243. remove("watch_file");
  244. MAKE_VALGRIND_HAPPY();
  245. return 0;
  246. }
  247. TEST_IMPL(fs_event_no_callback_after_close) {
  248. uv_loop_t* loop = uv_default_loop();
  249. int r;
  250. /* Setup */
  251. remove("watch_dir/file1");
  252. remove("watch_dir/");
  253. create_dir(loop, "watch_dir");
  254. create_file(loop, "watch_dir/file1");
  255. r = uv_fs_event_init(loop,
  256. &fs_event,
  257. "watch_dir/file1",
  258. fs_event_cb_file,
  259. 0);
  260. ASSERT(r != -1);
  261. uv_close((uv_handle_t*)&fs_event, close_cb);
  262. touch_file(loop, "watch_dir/file1");
  263. uv_run(loop, UV_RUN_DEFAULT);
  264. ASSERT(fs_event_cb_called == 0);
  265. ASSERT(close_cb_called == 1);
  266. /* Cleanup */
  267. remove("watch_dir/file1");
  268. remove("watch_dir/");
  269. MAKE_VALGRIND_HAPPY();
  270. return 0;
  271. }
  272. TEST_IMPL(fs_event_no_callback_on_close) {
  273. uv_loop_t* loop = uv_default_loop();
  274. int r;
  275. /* Setup */
  276. remove("watch_dir/file1");
  277. remove("watch_dir/");
  278. create_dir(loop, "watch_dir");
  279. create_file(loop, "watch_dir/file1");
  280. r = uv_fs_event_init(loop,
  281. &fs_event,
  282. "watch_dir/file1",
  283. fs_event_cb_file,
  284. 0);
  285. ASSERT(r != -1);
  286. uv_close((uv_handle_t*)&fs_event, close_cb);
  287. uv_run(loop, UV_RUN_DEFAULT);
  288. ASSERT(fs_event_cb_called == 0);
  289. ASSERT(close_cb_called == 1);
  290. /* Cleanup */
  291. remove("watch_dir/file1");
  292. remove("watch_dir/");
  293. MAKE_VALGRIND_HAPPY();
  294. return 0;
  295. }
  296. static void fs_event_fail(uv_fs_event_t* handle, const char* filename,
  297. int events, int status) {
  298. ASSERT(0 && "should never be called");
  299. }
  300. static void timer_cb(uv_timer_t* handle, int status) {
  301. int r;
  302. ASSERT(status == 0);
  303. r = uv_fs_event_init(handle->loop, &fs_event, ".", fs_event_fail, 0);
  304. ASSERT(r == 0);
  305. uv_close((uv_handle_t*)&fs_event, close_cb);
  306. uv_close((uv_handle_t*)handle, close_cb);
  307. }
  308. TEST_IMPL(fs_event_immediate_close) {
  309. uv_timer_t timer;
  310. uv_loop_t* loop;
  311. int r;
  312. loop = uv_default_loop();
  313. r = uv_timer_init(loop, &timer);
  314. ASSERT(r == 0);
  315. r = uv_timer_start(&timer, timer_cb, 1, 0);
  316. ASSERT(r == 0);
  317. uv_run(loop, UV_RUN_DEFAULT);
  318. ASSERT(close_cb_called == 2);
  319. MAKE_VALGRIND_HAPPY();
  320. return 0;
  321. }
  322. TEST_IMPL(fs_event_close_with_pending_event) {
  323. uv_loop_t* loop;
  324. int r;
  325. loop = uv_default_loop();
  326. create_dir(loop, "watch_dir");
  327. create_file(loop, "watch_dir/file");
  328. r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_fail, 0);
  329. ASSERT(r == 0);
  330. /* Generate an fs event. */
  331. touch_file(loop, "watch_dir/file");
  332. uv_close((uv_handle_t*)&fs_event, close_cb);
  333. uv_run(loop, UV_RUN_DEFAULT);
  334. ASSERT(close_cb_called == 1);
  335. /* Clean up */
  336. remove("watch_dir/file");
  337. remove("watch_dir/");
  338. MAKE_VALGRIND_HAPPY();
  339. return 0;
  340. }
  341. #if defined(HAVE_KQUEUE)
  342. /* kqueue doesn't register fs events if you don't have an active watcher.
  343. * The file descriptor needs to be part of the kqueue set of interest and
  344. * that's not the case until we actually enter the event loop.
  345. */
  346. TEST_IMPL(fs_event_close_in_callback) {
  347. fprintf(stderr, "Skipping test, doesn't work with kqueue.\n");
  348. return 0;
  349. }
  350. #else /* !HAVE_KQUEUE */
  351. static void fs_event_cb_close(uv_fs_event_t* handle, const char* filename,
  352. int events, int status) {
  353. ASSERT(status == 0);
  354. ASSERT(fs_event_cb_called < 3);
  355. ++fs_event_cb_called;
  356. if (fs_event_cb_called == 3) {
  357. uv_close((uv_handle_t*) handle, close_cb);
  358. }
  359. }
  360. TEST_IMPL(fs_event_close_in_callback) {
  361. uv_loop_t* loop;
  362. int r;
  363. loop = uv_default_loop();
  364. create_dir(loop, "watch_dir");
  365. create_file(loop, "watch_dir/file1");
  366. create_file(loop, "watch_dir/file2");
  367. create_file(loop, "watch_dir/file3");
  368. create_file(loop, "watch_dir/file4");
  369. create_file(loop, "watch_dir/file5");
  370. r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_close, 0);
  371. ASSERT(r == 0);
  372. /* Generate a couple of fs events. */
  373. touch_file(loop, "watch_dir/file1");
  374. touch_file(loop, "watch_dir/file2");
  375. touch_file(loop, "watch_dir/file3");
  376. touch_file(loop, "watch_dir/file4");
  377. touch_file(loop, "watch_dir/file5");
  378. uv_run(loop, UV_RUN_DEFAULT);
  379. ASSERT(close_cb_called == 1);
  380. ASSERT(fs_event_cb_called == 3);
  381. /* Clean up */
  382. remove("watch_dir/file1");
  383. remove("watch_dir/file2");
  384. remove("watch_dir/file3");
  385. remove("watch_dir/file4");
  386. remove("watch_dir/file5");
  387. remove("watch_dir/");
  388. MAKE_VALGRIND_HAPPY();
  389. return 0;
  390. }
  391. #endif /* HAVE_KQUEUE */