This source file includes following definitions.
- touch_file
- close_cb
- timer_cb
- poll_cb
- TEST_IMPL
#include "uv.h"
#include "task.h"
#include <string.h>
#define FIXTURE "testfile"
static void timer_cb(uv_timer_t* handle, int status);
static void close_cb(uv_handle_t* handle);
static void poll_cb(uv_fs_poll_t* handle,
int status,
const uv_stat_t* prev,
const uv_stat_t* curr);
static uv_fs_poll_t poll_handle;
static uv_timer_t timer_handle;
static uv_loop_t* loop;
static int poll_cb_called;
static int timer_cb_called;
static int close_cb_called;
static void touch_file(const char* path) {
static int count;
FILE* fp;
int i;
ASSERT((fp = fopen(FIXTURE, "w+")));
i = ++count;
while (i--)
fputc('*', fp);
fclose(fp);
}
static void close_cb(uv_handle_t* handle) {
close_cb_called++;
}
static void timer_cb(uv_timer_t* handle, int status) {
touch_file(FIXTURE);
timer_cb_called++;
}
static void poll_cb(uv_fs_poll_t* handle,
int status,
const uv_stat_t* prev,
const uv_stat_t* curr) {
uv_stat_t zero_statbuf;
memset(&zero_statbuf, 0, sizeof(zero_statbuf));
ASSERT(handle == &poll_handle);
ASSERT(uv_is_active((uv_handle_t*)handle));
ASSERT(prev != NULL);
ASSERT(curr != NULL);
switch (poll_cb_called++) {
case 0:
ASSERT(status == UV_ENOENT);
ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
touch_file(FIXTURE);
break;
case 1:
ASSERT(status == 0);
ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 20, 0));
break;
case 2:
ASSERT(status == 0);
ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 200, 0));
break;
case 3:
ASSERT(status == 0);
ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
remove(FIXTURE);
break;
case 4:
ASSERT(status == UV_ENOENT);
ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
uv_close((uv_handle_t*)handle, close_cb);
break;
default:
ASSERT(0);
}
}
TEST_IMPL(fs_poll) {
loop = uv_default_loop();
remove(FIXTURE);
ASSERT(0 == uv_timer_init(loop, &timer_handle));
ASSERT(0 == uv_fs_poll_init(loop, &poll_handle));
ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb, FIXTURE, 100));
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
ASSERT(poll_cb_called == 5);
ASSERT(timer_cb_called == 2);
ASSERT(close_cb_called == 1);
MAKE_VALGRIND_HAPPY();
return 0;
}