From 22ebdc5c35f4fb284e38aa80727cf408ec12ea8c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 12 Jan 2020 12:26:43 +0100 Subject: [PATCH] test: fix fs birth time test failure The test checks that the creation time equals the birth time on systems that support the notion of birth time (macOS, Linux.) The test was flaky because there was a write taking place between the creation of the file and the fstat() call, sometimes changing the ctime by fractions of milliseconds... First fstat(), only then write(). Fixes: https://github.com/libuv/libuv/issues/2235 PR-URL: https://github.com/libuv/libuv/pull/2621 Reviewed-By: Anna Henningsen Reviewed-By: Jameson Nash Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau --- test/test-fs.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/test/test-fs.c b/test/test-fs.c index 4b5051df..75cefe32 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -1323,6 +1323,25 @@ TEST_IMPL(fs_fstat) { file = req.result; uv_fs_req_cleanup(&req); +#ifndef _WIN32 + ASSERT(0 == fstat(file, &t)); + ASSERT(0 == uv_fs_fstat(NULL, &req, file, NULL)); + ASSERT(req.result == 0); + s = req.ptr; +# if defined(__APPLE__) + ASSERT(s->st_birthtim.tv_sec == t.st_birthtimespec.tv_sec); + ASSERT(s->st_birthtim.tv_nsec == t.st_birthtimespec.tv_nsec); +# elif defined(__linux__) + /* If statx() is supported, the birth time should be equal to the change time + * because we just created the file. On older kernels, it's set to zero. + */ + ASSERT(s->st_birthtim.tv_sec == 0 || + s->st_birthtim.tv_sec == t.st_ctim.tv_sec); + ASSERT(s->st_birthtim.tv_nsec == 0 || + s->st_birthtim.tv_nsec == t.st_ctim.tv_nsec); +# endif +#endif + iov = uv_buf_init(test_buf, sizeof(test_buf)); r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL); ASSERT(r == sizeof(test_buf)); @@ -1357,10 +1376,6 @@ TEST_IMPL(fs_fstat) { ASSERT(s->st_mtim.tv_nsec == t.st_mtimespec.tv_nsec); ASSERT(s->st_ctim.tv_sec == t.st_ctimespec.tv_sec); ASSERT(s->st_ctim.tv_nsec == t.st_ctimespec.tv_nsec); - ASSERT(s->st_birthtim.tv_sec == t.st_birthtimespec.tv_sec); - ASSERT(s->st_birthtim.tv_nsec == t.st_birthtimespec.tv_nsec); - ASSERT(s->st_flags == t.st_flags); - ASSERT(s->st_gen == t.st_gen); #elif defined(_AIX) ASSERT(s->st_atim.tv_sec == t.st_atime); ASSERT(s->st_atim.tv_nsec == 0); @@ -1395,8 +1410,6 @@ TEST_IMPL(fs_fstat) { defined(__NetBSD__) ASSERT(s->st_birthtim.tv_sec == t.st_birthtim.tv_sec); ASSERT(s->st_birthtim.tv_nsec == t.st_birthtim.tv_nsec); - ASSERT(s->st_flags == t.st_flags); - ASSERT(s->st_gen == t.st_gen); # endif #else ASSERT(s->st_atim.tv_sec == t.st_atime); @@ -1408,14 +1421,10 @@ TEST_IMPL(fs_fstat) { #endif #endif -#if defined(__linux__) - /* If statx() is supported, the birth time should be equal to the change time - * because we just created the file. On older kernels, it's set to zero. - */ - ASSERT(s->st_birthtim.tv_sec == 0 || - s->st_birthtim.tv_sec == t.st_ctim.tv_sec); - ASSERT(s->st_birthtim.tv_nsec == 0 || - s->st_birthtim.tv_nsec == t.st_ctim.tv_nsec); +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) + ASSERT(s->st_flags == t.st_flags); + ASSERT(s->st_gen == t.st_gen); +#else ASSERT(s->st_flags == 0); ASSERT(s->st_gen == 0); #endif