10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos
250e552da7Schristos #include <string.h>
260e552da7Schristos
270e552da7Schristos #define FIXTURE "testfile"
280e552da7Schristos
290e552da7Schristos static void timer_cb(uv_timer_t* handle);
300e552da7Schristos static void close_cb(uv_handle_t* handle);
310e552da7Schristos static void poll_cb(uv_fs_poll_t* handle,
320e552da7Schristos int status,
330e552da7Schristos const uv_stat_t* prev,
340e552da7Schristos const uv_stat_t* curr);
350e552da7Schristos
360e552da7Schristos static void poll_cb_fail(uv_fs_poll_t* handle,
370e552da7Schristos int status,
380e552da7Schristos const uv_stat_t* prev,
390e552da7Schristos const uv_stat_t* curr);
400e552da7Schristos static void poll_cb_noop(uv_fs_poll_t* handle,
410e552da7Schristos int status,
420e552da7Schristos const uv_stat_t* prev,
430e552da7Schristos const uv_stat_t* curr);
440e552da7Schristos
450e552da7Schristos static uv_fs_poll_t poll_handle;
460e552da7Schristos static uv_timer_t timer_handle;
470e552da7Schristos static uv_loop_t* loop;
480e552da7Schristos
490e552da7Schristos static int poll_cb_called;
500e552da7Schristos static int timer_cb_called;
510e552da7Schristos static int close_cb_called;
520e552da7Schristos
530e552da7Schristos
touch_file(const char * path)540e552da7Schristos static void touch_file(const char* path) {
550e552da7Schristos static int count;
560e552da7Schristos FILE* fp;
570e552da7Schristos int i;
580e552da7Schristos
590e552da7Schristos ASSERT((fp = fopen(FIXTURE, "w+")));
600e552da7Schristos
610e552da7Schristos /* Need to change the file size because the poller may not pick up
620e552da7Schristos * sub-second mtime changes.
630e552da7Schristos */
640e552da7Schristos i = ++count;
650e552da7Schristos
660e552da7Schristos while (i--)
670e552da7Schristos fputc('*', fp);
680e552da7Schristos
690e552da7Schristos fclose(fp);
700e552da7Schristos }
710e552da7Schristos
720e552da7Schristos
close_cb(uv_handle_t * handle)730e552da7Schristos static void close_cb(uv_handle_t* handle) {
740e552da7Schristos close_cb_called++;
750e552da7Schristos }
760e552da7Schristos
770e552da7Schristos
timer_cb(uv_timer_t * handle)780e552da7Schristos static void timer_cb(uv_timer_t* handle) {
790e552da7Schristos touch_file(FIXTURE);
800e552da7Schristos timer_cb_called++;
810e552da7Schristos }
820e552da7Schristos
830e552da7Schristos
poll_cb_fail(uv_fs_poll_t * handle,int status,const uv_stat_t * prev,const uv_stat_t * curr)840e552da7Schristos static void poll_cb_fail(uv_fs_poll_t* handle,
850e552da7Schristos int status,
860e552da7Schristos const uv_stat_t* prev,
870e552da7Schristos const uv_stat_t* curr) {
880e552da7Schristos ASSERT(0 && "fail_cb called");
890e552da7Schristos }
900e552da7Schristos
poll_cb_noop(uv_fs_poll_t * handle,int status,const uv_stat_t * prev,const uv_stat_t * curr)910e552da7Schristos static void poll_cb_noop(uv_fs_poll_t* handle,
920e552da7Schristos int status,
930e552da7Schristos const uv_stat_t* prev,
940e552da7Schristos const uv_stat_t* curr) {
950e552da7Schristos }
960e552da7Schristos
970e552da7Schristos
poll_cb(uv_fs_poll_t * handle,int status,const uv_stat_t * prev,const uv_stat_t * curr)980e552da7Schristos static void poll_cb(uv_fs_poll_t* handle,
990e552da7Schristos int status,
1000e552da7Schristos const uv_stat_t* prev,
1010e552da7Schristos const uv_stat_t* curr) {
1020e552da7Schristos uv_stat_t zero_statbuf;
1030e552da7Schristos
1040e552da7Schristos memset(&zero_statbuf, 0, sizeof(zero_statbuf));
1050e552da7Schristos
1060e552da7Schristos ASSERT(handle == &poll_handle);
1070e552da7Schristos ASSERT(1 == uv_is_active((uv_handle_t*) handle));
108*5f2f4271Schristos ASSERT_NOT_NULL(prev);
109*5f2f4271Schristos ASSERT_NOT_NULL(curr);
1100e552da7Schristos
1110e552da7Schristos switch (poll_cb_called++) {
1120e552da7Schristos case 0:
1130e552da7Schristos ASSERT(status == UV_ENOENT);
1140e552da7Schristos ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
1150e552da7Schristos ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
1160e552da7Schristos touch_file(FIXTURE);
1170e552da7Schristos break;
1180e552da7Schristos
1190e552da7Schristos case 1:
1200e552da7Schristos ASSERT(status == 0);
1210e552da7Schristos ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
1220e552da7Schristos ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
1230e552da7Schristos ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 20, 0));
1240e552da7Schristos break;
1250e552da7Schristos
1260e552da7Schristos case 2:
1270e552da7Schristos ASSERT(status == 0);
1280e552da7Schristos ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
1290e552da7Schristos ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
1300e552da7Schristos ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 200, 0));
1310e552da7Schristos break;
1320e552da7Schristos
1330e552da7Schristos case 3:
1340e552da7Schristos ASSERT(status == 0);
1350e552da7Schristos ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
1360e552da7Schristos ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
1370e552da7Schristos remove(FIXTURE);
1380e552da7Schristos break;
1390e552da7Schristos
1400e552da7Schristos case 4:
1410e552da7Schristos ASSERT(status == UV_ENOENT);
1420e552da7Schristos ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
1430e552da7Schristos ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
1440e552da7Schristos uv_close((uv_handle_t*)handle, close_cb);
1450e552da7Schristos break;
1460e552da7Schristos
1470e552da7Schristos default:
1480e552da7Schristos ASSERT(0);
1490e552da7Schristos }
1500e552da7Schristos }
1510e552da7Schristos
1520e552da7Schristos
TEST_IMPL(fs_poll)1530e552da7Schristos TEST_IMPL(fs_poll) {
1540e552da7Schristos loop = uv_default_loop();
1550e552da7Schristos
1560e552da7Schristos remove(FIXTURE);
1570e552da7Schristos
1580e552da7Schristos ASSERT(0 == uv_timer_init(loop, &timer_handle));
1590e552da7Schristos ASSERT(0 == uv_fs_poll_init(loop, &poll_handle));
1600e552da7Schristos ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb, FIXTURE, 100));
1610e552da7Schristos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
1620e552da7Schristos
1630e552da7Schristos ASSERT(poll_cb_called == 5);
1640e552da7Schristos ASSERT(timer_cb_called == 2);
1650e552da7Schristos ASSERT(close_cb_called == 1);
1660e552da7Schristos
1670e552da7Schristos MAKE_VALGRIND_HAPPY();
1680e552da7Schristos return 0;
1690e552da7Schristos }
1700e552da7Schristos
1710e552da7Schristos
TEST_IMPL(fs_poll_getpath)1720e552da7Schristos TEST_IMPL(fs_poll_getpath) {
1730e552da7Schristos char buf[1024];
1740e552da7Schristos size_t len;
1750e552da7Schristos loop = uv_default_loop();
1760e552da7Schristos
1770e552da7Schristos remove(FIXTURE);
1780e552da7Schristos
1790e552da7Schristos ASSERT(0 == uv_fs_poll_init(loop, &poll_handle));
1800e552da7Schristos len = sizeof buf;
1810e552da7Schristos ASSERT(UV_EINVAL == uv_fs_poll_getpath(&poll_handle, buf, &len));
1820e552da7Schristos ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb_fail, FIXTURE, 100));
1830e552da7Schristos len = sizeof buf;
1840e552da7Schristos ASSERT(0 == uv_fs_poll_getpath(&poll_handle, buf, &len));
1850e552da7Schristos ASSERT(buf[len - 1] != 0);
1860e552da7Schristos ASSERT(buf[len] == '\0');
1870e552da7Schristos ASSERT(0 == memcmp(buf, FIXTURE, len));
1880e552da7Schristos
1890e552da7Schristos uv_close((uv_handle_t*) &poll_handle, close_cb);
1900e552da7Schristos
1910e552da7Schristos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
1920e552da7Schristos
1930e552da7Schristos ASSERT(close_cb_called == 1);
1940e552da7Schristos
1950e552da7Schristos MAKE_VALGRIND_HAPPY();
1960e552da7Schristos return 0;
1970e552da7Schristos }
1980e552da7Schristos
1990e552da7Schristos
TEST_IMPL(fs_poll_close_request)2000e552da7Schristos TEST_IMPL(fs_poll_close_request) {
2010e552da7Schristos uv_loop_t loop;
2020e552da7Schristos uv_fs_poll_t poll_handle;
2030e552da7Schristos
2040e552da7Schristos remove(FIXTURE);
2050e552da7Schristos
2060e552da7Schristos ASSERT(0 == uv_loop_init(&loop));
2070e552da7Schristos
2080e552da7Schristos ASSERT(0 == uv_fs_poll_init(&loop, &poll_handle));
2090e552da7Schristos ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb_fail, FIXTURE, 100));
2100e552da7Schristos uv_close((uv_handle_t*) &poll_handle, close_cb);
2110e552da7Schristos while (close_cb_called == 0)
2120e552da7Schristos uv_run(&loop, UV_RUN_ONCE);
2130e552da7Schristos ASSERT(close_cb_called == 1);
2140e552da7Schristos
2150e552da7Schristos ASSERT(0 == uv_loop_close(&loop));
2160e552da7Schristos
2170e552da7Schristos MAKE_VALGRIND_HAPPY();
2180e552da7Schristos return 0;
2190e552da7Schristos }
2200e552da7Schristos
TEST_IMPL(fs_poll_close_request_multi_start_stop)2210e552da7Schristos TEST_IMPL(fs_poll_close_request_multi_start_stop) {
2220e552da7Schristos uv_loop_t loop;
2230e552da7Schristos uv_fs_poll_t poll_handle;
2240e552da7Schristos int i;
2250e552da7Schristos
2260e552da7Schristos remove(FIXTURE);
2270e552da7Schristos
2280e552da7Schristos ASSERT(0 == uv_loop_init(&loop));
2290e552da7Schristos
2300e552da7Schristos ASSERT(0 == uv_fs_poll_init(&loop, &poll_handle));
2310e552da7Schristos
2320e552da7Schristos for (i = 0; i < 10; ++i) {
2330e552da7Schristos ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb_fail, FIXTURE, 100));
2340e552da7Schristos ASSERT(0 == uv_fs_poll_stop(&poll_handle));
2350e552da7Schristos }
2360e552da7Schristos uv_close((uv_handle_t*) &poll_handle, close_cb);
2370e552da7Schristos while (close_cb_called == 0)
2380e552da7Schristos uv_run(&loop, UV_RUN_ONCE);
2390e552da7Schristos ASSERT(close_cb_called == 1);
2400e552da7Schristos
2410e552da7Schristos ASSERT(0 == uv_loop_close(&loop));
2420e552da7Schristos
2430e552da7Schristos MAKE_VALGRIND_HAPPY();
2440e552da7Schristos return 0;
2450e552da7Schristos }
2460e552da7Schristos
TEST_IMPL(fs_poll_close_request_multi_stop_start)2470e552da7Schristos TEST_IMPL(fs_poll_close_request_multi_stop_start) {
2480e552da7Schristos uv_loop_t loop;
2490e552da7Schristos uv_fs_poll_t poll_handle;
2500e552da7Schristos int i;
2510e552da7Schristos
2520e552da7Schristos remove(FIXTURE);
2530e552da7Schristos
2540e552da7Schristos ASSERT(0 == uv_loop_init(&loop));
2550e552da7Schristos
2560e552da7Schristos ASSERT(0 == uv_fs_poll_init(&loop, &poll_handle));
2570e552da7Schristos
2580e552da7Schristos for (i = 0; i < 10; ++i) {
2590e552da7Schristos ASSERT(0 == uv_fs_poll_stop(&poll_handle));
2600e552da7Schristos ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb_fail, FIXTURE, 100));
2610e552da7Schristos }
2620e552da7Schristos uv_close((uv_handle_t*) &poll_handle, close_cb);
2630e552da7Schristos while (close_cb_called == 0)
2640e552da7Schristos uv_run(&loop, UV_RUN_ONCE);
2650e552da7Schristos ASSERT(close_cb_called == 1);
2660e552da7Schristos
2670e552da7Schristos ASSERT(0 == uv_loop_close(&loop));
2680e552da7Schristos
2690e552da7Schristos MAKE_VALGRIND_HAPPY();
2700e552da7Schristos return 0;
2710e552da7Schristos }
2720e552da7Schristos
TEST_IMPL(fs_poll_close_request_stop_when_active)2730e552da7Schristos TEST_IMPL(fs_poll_close_request_stop_when_active) {
2740e552da7Schristos /* Regression test for https://github.com/libuv/libuv/issues/2287. */
2750e552da7Schristos uv_loop_t loop;
2760e552da7Schristos uv_fs_poll_t poll_handle;
2770e552da7Schristos
2780e552da7Schristos remove(FIXTURE);
2790e552da7Schristos
2800e552da7Schristos ASSERT(0 == uv_loop_init(&loop));
2810e552da7Schristos
2820e552da7Schristos /* Set up all handles. */
2830e552da7Schristos ASSERT(0 == uv_fs_poll_init(&loop, &poll_handle));
2840e552da7Schristos ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb_noop, FIXTURE, 100));
2850e552da7Schristos uv_run(&loop, UV_RUN_ONCE);
2860e552da7Schristos
2870e552da7Schristos /* Close the timer handle, and do not crash. */
2880e552da7Schristos ASSERT(0 == uv_fs_poll_stop(&poll_handle));
2890e552da7Schristos uv_run(&loop, UV_RUN_ONCE);
2900e552da7Schristos
2910e552da7Schristos /* Clean up after the test. */
2920e552da7Schristos uv_close((uv_handle_t*) &poll_handle, close_cb);
2930e552da7Schristos uv_run(&loop, UV_RUN_ONCE);
2940e552da7Schristos ASSERT(close_cb_called == 1);
2950e552da7Schristos
2960e552da7Schristos ASSERT(0 == uv_loop_close(&loop));
2970e552da7Schristos
2980e552da7Schristos MAKE_VALGRIND_HAPPY();
2990e552da7Schristos return 0;
3000e552da7Schristos }
301