Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Created April 17, 2023 18:34
Show Gist options
  • Save trevnorris/bdaaf9e01cad30e1e509981fe0a8a8dd to your computer and use it in GitHub Desktop.
Save trevnorris/bdaaf9e01cad30e1e509981fe0a8a8dd to your computer and use it in GitHub Desktop.
diff --git a/test/test-list.h b/test/test-list.h
index 13c96d1d..81f2aa25 100644
--- a/test/test-list.h
+++ b/test/test-list.h
@@ -556,6 +556,7 @@ TEST_DECLARE (utf8_decode1_overrun)
TEST_DECLARE (uname)
TEST_DECLARE (metrics_info_check)
+TEST_DECLARE (metrics_fs_events)
TEST_DECLARE (metrics_idle_time)
TEST_DECLARE (metrics_idle_time_thread)
TEST_DECLARE (metrics_idle_time_zero)
@@ -1192,6 +1193,7 @@ TASK_LIST_START
TEST_HELPER (readable_on_eof, tcp4_echo_server)
TEST_ENTRY (metrics_info_check)
+ TEST_ENTRY (metrics_fs_events)
TEST_ENTRY (metrics_idle_time)
TEST_ENTRY (metrics_idle_time_thread)
TEST_ENTRY (metrics_idle_time_zero)
diff --git a/test/test-metrics.c b/test/test-metrics.c
index c6fa432a..fe7a8891 100644
--- a/test/test-metrics.c
+++ b/test/test-metrics.c
@@ -34,6 +34,7 @@ typedef struct {
static uint64_t last_events_count;
static char test_buf[] = "test-buffer\n";
static fs_reqs_t fs_reqs;
+static int fs_events_counter;
static void timer_spin_cb(uv_timer_t* handle) {
@@ -239,3 +240,89 @@ TEST_IMPL(metrics_info_check) {
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
+
+
+static void fs_prepare_cb(uv_prepare_t* handle) {
+ uv_metrics_t metrics;
+
+ ASSERT_EQ(0, uv_metrics_info(uv_default_loop(), &metrics));
+
+ if (fs_events_counter > 0)
+ return;
+
+ uv_prepare_stop(handle);
+ ASSERT_EQ(metrics.events, 3);
+ fs_events_counter = -42;
+}
+
+
+static void fs_stat_cb(uv_fs_t* req) {
+ uv_fs_req_cleanup(req);
+ free(req);
+ fs_events_counter--;
+}
+
+
+static void fs_write_cb(uv_fs_t* req) {
+ uv_fs_t* stat1_req = malloc(sizeof(*req));
+ uv_fs_t* stat2_req = malloc(sizeof(*req));
+ fs_events_counter--;
+
+ ASSERT_EQ(uv_fs_stat(uv_default_loop(),
+ stat1_req,
+ "test_file",
+ fs_stat_cb), 0);
+ ASSERT_EQ(uv_fs_stat(uv_default_loop(),
+ stat2_req,
+ "test_file",
+ fs_stat_cb), 0);
+ uv_fs_req_cleanup(req);
+}
+
+
+TEST_IMPL(metrics_fs_events) {
+ uv_fs_t open_req;
+ uv_fs_t unlink_req;
+ uv_fs_t write_req;
+ uv_prepare_t prepare;
+ uv_buf_t iov;
+ uv_metrics_t metrics;
+ int fd;
+
+ uv_fs_unlink(NULL, &unlink_req, "test_file", NULL);
+ uv_fs_req_cleanup(&unlink_req);
+
+ fs_events_counter = 3;
+ fd = uv_fs_open(NULL,
+ &open_req,
+ "test_file",
+ O_WRONLY | O_CREAT,
+ S_IRUSR | S_IWUSR,
+ NULL);
+ ASSERT_GT(fd, 0);
+ uv_fs_req_cleanup(&open_req);
+
+ iov = uv_buf_init(test_buf, sizeof(test_buf));
+ ASSERT_EQ(uv_fs_write(uv_default_loop(),
+ &write_req,
+ fd,
+ &iov,
+ 1,
+ 0,
+ fs_write_cb), 0);
+
+ ASSERT_EQ(0, uv_prepare_init(uv_default_loop(), &prepare));
+ ASSERT_EQ(0, uv_prepare_start(&prepare, fs_prepare_cb));
+
+ ASSERT_EQ(0, uv_run(uv_default_loop(), UV_RUN_DEFAULT));
+
+ ASSERT_EQ(0, uv_metrics_info(uv_default_loop(), &metrics));
+ ASSERT_EQ(metrics.events, 3);
+ ASSERT_EQ(fs_events_counter, -42);
+
+ uv_fs_unlink(NULL, &unlink_req, "test_file", NULL);
+ uv_fs_req_cleanup(&unlink_req);
+
+ MAKE_VALGRIND_HAPPY(uv_default_loop());
+ return 0;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment