1
0

test-fs-poll.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #define FIXTURE "testfile"
  25. static void timer_cb(uv_timer_t* handle, int status);
  26. static void close_cb(uv_handle_t* handle);
  27. static void poll_cb(uv_fs_poll_t* handle,
  28. int status,
  29. const uv_statbuf_t* prev,
  30. const uv_statbuf_t* curr);
  31. static uv_fs_poll_t poll_handle;
  32. static uv_timer_t timer_handle;
  33. static uv_loop_t* loop;
  34. static int poll_cb_called;
  35. static int timer_cb_called;
  36. static int close_cb_called;
  37. static void touch_file(const char* path) {
  38. static int count;
  39. FILE* fp;
  40. int i;
  41. ASSERT((fp = fopen(FIXTURE, "w+")));
  42. /* Need to change the file size because the poller may not pick up
  43. * sub-second mtime changes.
  44. */
  45. i = ++count;
  46. while (i--)
  47. fputc('*', fp);
  48. fclose(fp);
  49. }
  50. static void close_cb(uv_handle_t* handle) {
  51. close_cb_called++;
  52. }
  53. static void timer_cb(uv_timer_t* handle, int status) {
  54. touch_file(FIXTURE);
  55. timer_cb_called++;
  56. }
  57. static void poll_cb(uv_fs_poll_t* handle,
  58. int status,
  59. const uv_statbuf_t* prev,
  60. const uv_statbuf_t* curr) {
  61. uv_statbuf_t zero_statbuf;
  62. memset(&zero_statbuf, 0, sizeof(zero_statbuf));
  63. ASSERT(handle == &poll_handle);
  64. ASSERT(uv_is_active((uv_handle_t*)handle));
  65. ASSERT(prev != NULL);
  66. ASSERT(curr != NULL);
  67. switch (poll_cb_called++) {
  68. case 0:
  69. ASSERT(status == -1);
  70. ASSERT(uv_last_error(loop).code == UV_ENOENT);
  71. ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  72. ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  73. touch_file(FIXTURE);
  74. break;
  75. case 1:
  76. ASSERT(status == 0);
  77. ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  78. ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  79. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 20, 0));
  80. break;
  81. case 2:
  82. ASSERT(status == 0);
  83. ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  84. ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  85. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 200, 0));
  86. break;
  87. case 3:
  88. ASSERT(status == 0);
  89. ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  90. ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  91. remove(FIXTURE);
  92. break;
  93. case 4:
  94. ASSERT(status == -1);
  95. ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  96. ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  97. ASSERT(uv_last_error(loop).code == UV_ENOENT);
  98. uv_close((uv_handle_t*)handle, close_cb);
  99. break;
  100. default:
  101. ASSERT(0);
  102. }
  103. }
  104. TEST_IMPL(fs_poll) {
  105. loop = uv_default_loop();
  106. remove(FIXTURE);
  107. ASSERT(0 == uv_timer_init(loop, &timer_handle));
  108. ASSERT(0 == uv_fs_poll_init(loop, &poll_handle));
  109. ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb, FIXTURE, 100));
  110. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  111. ASSERT(poll_cb_called == 5);
  112. ASSERT(timer_cb_called == 2);
  113. ASSERT(close_cb_called == 1);
  114. MAKE_VALGRIND_HAPPY();
  115. return 0;
  116. }