changeset 12469:8b48f88c47f1

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>
author Eric Blake <ebb9@byu.net>
date Mon, 21 Dec 2009 07:00:13 -0700
parents ff7c15d38975
children bce25ef49b1e
files ChangeLog modules/chown-tests modules/fdutimensat-tests modules/futimens-tests modules/lchown-tests modules/openat-tests modules/utimens-tests modules/utimensat-tests tests/nap.h tests/test-chown.h tests/test-lchown.h tests/test-utimens-common.h
diffstat 12 files changed, 96 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2009-12-21  Eric Blake  <ebb9@byu.net>
+
+	test-utimens: avoid spurious failure
+	* 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.
+
 2009-12-19  Eric Blake  <ebb9@byu.net>
 
 	futimens, utimensat: work around Linux bug
--- a/modules/chown-tests
+++ b/modules/chown-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-chown.h
 tests/test-chown.c
 
--- a/modules/fdutimensat-tests
+++ b/modules/fdutimensat-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-futimens.h
 tests/test-lutimens.h
 tests/test-utimens.h
--- a/modules/futimens-tests
+++ b/modules/futimens-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-futimens.h
 tests/test-utimens-common.h
 tests/test-futimens.c
--- a/modules/lchown-tests
+++ b/modules/lchown-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-lchown.h
 tests/test-lchown.c
 
--- a/modules/openat-tests
+++ b/modules/openat-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-chown.h
 tests/test-lchown.h
 tests/test-lstat.h
--- a/modules/utimens-tests
+++ b/modules/utimens-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-futimens.h
 tests/test-lutimens.h
 tests/test-utimens.h
--- a/modules/utimensat-tests
+++ b/modules/utimensat-tests
@@ -1,4 +1,5 @@
 Files:
+tests/nap.h
 tests/test-lutimens.h
 tests/test-utimens.h
 tests/test-utimens-common.h
new file mode 100644
--- /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 */
--- 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)
--- 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)
--- 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 @@
                           : 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