summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2009-12-21 07:00:13 -0700
committerEric Blake <ebb9@byu.net>2009-12-21 07:02:45 -0700
commitc7362ef0beb434d7a3063b5a7ac7b956988dc435 (patch)
tree5ac5e5ab921710d955583f9927563cbb80f44770 /tests
parent9a669cf64253a2b2149d7f7cc5e0664c1bc7dda9 (diff)
downloadgnulib-c7362ef0beb434d7a3063b5a7ac7b956988dc435.tar.gz
test-utimens: avoid spurious failure
Fixes a spurious failure on ext3, with one-second resolution, now that ctime effects are being tested for inequality. * tests/test-chown.h (nap): Factor... * tests/nap.h: ...into new file. * tests/test-lchown.h (nap): Avoid duplication. * tests/test-utimens-common.h (nap): Use shared implementation, necessary on file systems with 1-second resolution. * modules/chown-tests (Files): Include new file. * modules/fdutimensat-tests (Files): Likewise. * modules/futimens-tests (Files): Likewise. * modules/lchown-tests (Files): Likewise. * modules/openat-tests (Files): Likewise. * modules/utimens-tests (Files): Likewise. * modules/utimensat-tests (Files): Likewise. Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'tests')
-rw-r--r--tests/nap.h67
-rw-r--r--tests/test-chown.h45
-rw-r--r--tests/test-lchown.h46
-rw-r--r--tests/test-utimens-common.h27
4 files changed, 73 insertions, 112 deletions
diff --git a/tests/nap.h b/tests/nap.h
new file mode 100644
index 0000000000..e8b4f5a8e6
--- /dev/null
+++ b/tests/nap.h
@@ -0,0 +1,67 @@
+/* Assist in file system timestamp tests.
+ Copyright (C) 2009 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Eric Blake <ebb9@byu.net>, 2009. */
+
+#ifndef GLTEST_NAP_H
+# define GLTEST_NAP_H
+
+/* Sleep long enough to notice a timestamp difference on the file
+ system in the current directory. Assumes that BASE is defined,
+ and requires that the test module depends on usleep. */
+static void
+nap (void)
+{
+ static long delay;
+ if (!delay)
+ {
+ /* Initialize only once, by sleeping for 20 milliseconds (needed
+ since xfs has a quantization of about 10 milliseconds, even
+ though it has a granularity of 1 nanosecond, and since NTFS
+ has a default quantization of 15.25 milliseconds, even though
+ it has a granularity of 100 nanoseconds). If the seconds
+ differ, repeat the test one more time (in case we crossed a
+ quantization boundary on a file system with 1 second
+ resolution). If we can't observe a difference in only the
+ nanoseconds, then fall back to 1 second if the time is odd,
+ and 2 seconds (needed for FAT) if time is even. */
+ struct stat st1;
+ struct stat st2;
+ ASSERT (close (creat (BASE "tmp", 0600)) == 0);
+ ASSERT (stat (BASE "tmp", &st1) == 0);
+ ASSERT (unlink (BASE "tmp") == 0);
+ delay = 20000;
+ usleep (delay);
+ ASSERT (close (creat (BASE "tmp", 0600)) == 0);
+ ASSERT (stat (BASE "tmp", &st2) == 0);
+ ASSERT (unlink (BASE "tmp") == 0);
+ if (st1.st_mtime != st2.st_mtime)
+ {
+ /* Seconds differ, give it one more shot. */
+ st1 = st2;
+ usleep (delay);
+ ASSERT (close (creat (BASE "tmp", 0600)) == 0);
+ ASSERT (stat (BASE "tmp", &st2) == 0);
+ ASSERT (unlink (BASE "tmp") == 0);
+ }
+ if (! (st1.st_mtime == st2.st_mtime
+ && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
+ delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
+ }
+ usleep (delay);
+}
+
+#endif /* GLTEST_NAP_H */
diff --git a/tests/test-chown.h b/tests/test-chown.h
index c4e652af89..62e612e7aa 100644
--- a/tests/test-chown.h
+++ b/tests/test-chown.h
@@ -16,50 +16,7 @@
/* Written by Eric Blake <ebb9@byu.net>, 2009. */
-#define TEST_CHOWN_NAP
-/* Sleep long enough to notice a timestamp difference on the file
- system in the current directory. */
-static void
-nap (void)
-{
- static long delay;
- if (!delay)
- {
- /* Initialize only once, by sleeping for 20 milliseconds (needed
- since xfs has a quantization of about 10 milliseconds, even
- though it has a granularity of 1 nanosecond, and since NTFS
- has a default quantization of 15.25 milliseconds, even though
- it has a granularity of 100 nanoseconds). If the seconds
- differ, repeat the test one more time (in case we crossed a
- quantization boundary on a file system with 1 second
- resolution). If we can't observe a difference in only the
- nanoseconds, then fall back to 1 second if the time is odd,
- and 2 seconds (needed for FAT) if time is even. */
- struct stat st1;
- struct stat st2;
- ASSERT (close (creat (BASE "tmp", 0600)) == 0);
- ASSERT (stat (BASE "tmp", &st1) == 0);
- ASSERT (unlink (BASE "tmp") == 0);
- delay = 20000;
- usleep (delay);
- ASSERT (close (creat (BASE "tmp", 0600)) == 0);
- ASSERT (stat (BASE "tmp", &st2) == 0);
- ASSERT (unlink (BASE "tmp") == 0);
- if (st1.st_mtime != st2.st_mtime)
- {
- /* Seconds differ, give it one more shot. */
- st1 = st2;
- usleep (delay);
- ASSERT (close (creat (BASE "tmp", 0600)) == 0);
- ASSERT (stat (BASE "tmp", &st2) == 0);
- ASSERT (unlink (BASE "tmp") == 0);
- }
- if (! (st1.st_mtime == st2.st_mtime
- && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
- delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
- }
- usleep (delay);
-}
+#include "nap.h"
#if !HAVE_GETEGID
# define getegid() ((gid_t) -1)
diff --git a/tests/test-lchown.h b/tests/test-lchown.h
index 93a657cf5d..26f80f7911 100644
--- a/tests/test-lchown.h
+++ b/tests/test-lchown.h
@@ -16,51 +16,7 @@
/* Written by Eric Blake <ebb9@byu.net>, 2009. */
-#ifndef TEST_CHOWN_NAP
-/* Sleep long enough to notice a timestamp difference on the file
- system in the current directory. */
-static void
-nap (void)
-{
- static long delay;
- if (!delay)
- {
- /* Initialize only once, by sleeping for 20 milliseconds (needed
- since xfs has a quantization of about 10 milliseconds, even
- though it has a granularity of 1 nanosecond, and since NTFS
- has a default quantization of 15.25 milliseconds, even though
- it has a granularity of 100 nanoseconds). If the seconds
- differ, repeat the test one more time (in case we crossed a
- quantization boundary on a file system with 1 second
- resolution). If we can't observe a difference in only the
- nanoseconds, then fall back to 1 second if the time is odd,
- and 2 seconds (needed for FAT) if time is even. */
- struct stat st1;
- struct stat st2;
- ASSERT (close (creat (BASE "tmp", 0600)) == 0);
- ASSERT (stat (BASE "tmp", &st1) == 0);
- ASSERT (unlink (BASE "tmp") == 0);
- delay = 20000;
- usleep (delay);
- ASSERT (close (creat (BASE "tmp", 0600)) == 0);
- ASSERT (stat (BASE "tmp", &st2) == 0);
- ASSERT (unlink (BASE "tmp") == 0);
- if (st1.st_mtime != st2.st_mtime)
- {
- /* Seconds differ, give it one more shot. */
- st1 = st2;
- usleep (delay);
- ASSERT (close (creat (BASE "tmp", 0600)) == 0);
- ASSERT (stat (BASE "tmp", &st2) == 0);
- ASSERT (unlink (BASE "tmp") == 0);
- }
- if (! (st1.st_mtime == st2.st_mtime
- && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
- delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
- }
- usleep (delay);
-}
-#endif /* !TEST_CHOWN_NAP */
+#include "nap.h"
#if !HAVE_GETEGID
# define getegid() ((gid_t) -1)
diff --git a/tests/test-utimens-common.h b/tests/test-utimens-common.h
index 707971abe4..30fd886748 100644
--- a/tests/test-utimens-common.h
+++ b/tests/test-utimens-common.h
@@ -24,10 +24,14 @@
# include <string.h>
# include <unistd.h>
+/* Gnulib modules. */
# include "stat-time.h"
# include "timespec.h"
# include "utimecmp.h"
+/* Gnulib test header. */
+# include "nap.h"
+
enum {
BILLION = 1000 * 1000 * 1000,
@@ -44,29 +48,6 @@ enum {
: 0)
};
-/* Sleep long enough to cross a timestamp quantization boundary on
- most known systems with subsecond timestamp resolution. For
- example, ext4 has a quantization of 10 milliseconds, but a
- resolution of 1 nanosecond. Likewise, NTFS has a quantization as
- slow as 15.25 milliseconds, but a resolution of 100 nanoseconds.
- This is necessary on systems where creat or utimens with NULL
- rounds down to the quantization boundary, but where gettime and
- hence utimensat can inject timestamps between quantization
- boundaries. By ensuring we cross a boundary, we are less likely to
- confuse utimecmp for two times that would round to the same
- quantization boundary but are distinct based on resolution. */
-static void
-nap (void)
-{
- /* Systems that lack usleep also lack subsecond timestamps, and have
- a quantization boundary equal to the resolution. Our usage of
- utimecmp allows equality, so no need to waste 980 milliseconds
- if the replacement usleep rounds to 1 second. */
-# if HAVE_USLEEP
- usleep (20 * 1000); /* 20 milliseconds. */
-# endif
-}
-
# if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
/* Skip ctime tests on native Windows, since it is either a copy of
mtime or birth time (depending on the file system), rather than a