test-fs-poll.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_stat_t* prev,
  30. const uv_stat_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_stat_t* prev,
  60. const uv_stat_t* curr) {
  61. uv_stat_t zero_statbuf;
  62. memset(&zero_statbuf, 0, sizeof(zero_statbuf));
  63. ASSERT(handle == &poll_handle);
  64. ASSERT(1 == 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 == UV_ENOENT);
  70. ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  71. ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  72. touch_file(FIXTURE);
  73. break;
  74. case 1:
  75. ASSERT(status == 0);
  76. ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  77. ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  78. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 20, 0));
  79. break;
  80. case 2:
  81. ASSERT(status == 0);
  82. ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  83. ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  84. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 200, 0));
  85. break;
  86. case 3:
  87. ASSERT(status == 0);
  88. ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  89. ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  90. remove(FIXTURE);
  91. break;
  92. case 4:
  93. ASSERT(status == UV_ENOENT);
  94. ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
  95. ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
  96. uv_close((uv_handle_t*)handle, close_cb);
  97. break;
  98. default:
  99. ASSERT(0);
  100. }
  101. }
  102. TEST_IMPL(fs_poll) {
  103. loop = uv_default_loop();
  104. remove(FIXTURE);
  105. ASSERT(0 == uv_timer_init(loop, &timer_handle));
  106. ASSERT(0 == uv_fs_poll_init(loop, &poll_handle));
  107. ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb, FIXTURE, 100));
  108. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  109. ASSERT(poll_cb_called == 5);
  110. ASSERT(timer_cb_called == 2);
  111. ASSERT(close_cb_called == 1);
  112. MAKE_VALGRIND_HAPPY();
  113. return 0;
  114. }