From: Eric Blake Date: Tue, 17 Nov 2009 16:58:18 +0000 (-0700) Subject: usleep: use it to simplify tests X-Git-Tag: v0.1~5171 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=6038ee4b827caaf05fa37dbb2304fedb9d0cd6c7;p=gnulib.git usleep: use it to simplify tests Blindly using usleep makes it easier to write tests. * modules/stat-time-tests (Depends-on): Add usleep. (configure.ac): Drop usleep check. * modules/chown-tests (Depends-on, configure.ac): Likewise. * modules/lchown-tests (Depends-on, configure.ac): Likewise. * modules/fdutimensat-tests (Depends-on, configure.ac): Likewise. * modules/futimens-tests (Depends-on, configure.ac): Likewise. * modules/openat-tests (Depends-on, configure.ac): Likewise. * modules/utimens-tests (Depends-on, configure.ac): Likewise. * modules/utimensat-tests (Depends-on, configure.ac): Likewise. * modules/pipe-filter-gi-tests (Depends-on, configure.ac): Likewise. * tests/test-chown.h (nap): Rely on nicer usleep semantics. * tests/test-lchown.h (nap): Likewise. * tests/test-pipe-filter-gi2-main.c (small_nap): Likewise. * tests/test-stat-time.c (nap): Likewise. * tests/test-utimens-common.h (nap): Update comments. Signed-off-by: Eric Blake --- diff --git a/ChangeLog b/ChangeLog index cf14b4ba1..62b5423ac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,23 @@ 2009-11-17 Eric Blake + usleep: use it to simplify tests + * modules/stat-time-tests (Depends-on): Add usleep. + (configure.ac): Drop usleep check. + * modules/chown-tests (Depends-on, configure.ac): Likewise. + * modules/lchown-tests (Depends-on, configure.ac): Likewise. + * modules/fdutimensat-tests (Depends-on, configure.ac): Likewise. + * modules/futimens-tests (Depends-on, configure.ac): Likewise. + * modules/openat-tests (Depends-on, configure.ac): Likewise. + * modules/utimens-tests (Depends-on, configure.ac): Likewise. + * modules/utimensat-tests (Depends-on, configure.ac): Likewise. + * modules/pipe-filter-gi-tests (Depends-on, configure.ac): + Likewise. + * tests/test-chown.h (nap): Rely on nicer usleep semantics. + * tests/test-lchown.h (nap): Likewise. + * tests/test-pipe-filter-gi2-main.c (small_nap): Likewise. + * tests/test-stat-time.c (nap): Likewise. + * tests/test-utimens-common.h (nap): Update comments. + usleep: new module * modules/usleep: New file. * m4/usleep.m4 (gl_FUNC_USLEEP): Likewise. diff --git a/modules/chown-tests b/modules/chown-tests index 74e4cfbb8..a5d5c3af7 100644 --- a/modules/chown-tests +++ b/modules/chown-tests @@ -6,13 +6,13 @@ Depends-on: lstat mgetgroups progname -sleep +usleep stat-time stdbool symlink configure.ac: -AC_CHECK_FUNCS_ONCE([getegid usleep]) +AC_CHECK_FUNCS_ONCE([getegid]) Makefile.am: TESTS += test-chown diff --git a/modules/fdutimensat-tests b/modules/fdutimensat-tests index abd813d8b..75ff37424 100644 --- a/modules/fdutimensat-tests +++ b/modules/fdutimensat-tests @@ -8,10 +8,10 @@ tests/test-fdutimensat.c Depends-on: progname timespec +usleep utimecmp configure.ac: -AC_CHECK_FUNCS_ONCE([usleep]) Makefile.am: TESTS += test-fdutimensat diff --git a/modules/futimens-tests b/modules/futimens-tests index 3a92524f1..2f57c3bbb 100644 --- a/modules/futimens-tests +++ b/modules/futimens-tests @@ -5,10 +5,10 @@ tests/test-futimens.c Depends-on: timespec +usleep utimecmp configure.ac: -AC_CHECK_FUNCS_ONCE([usleep]) Makefile.am: TESTS += test-futimens diff --git a/modules/lchown-tests b/modules/lchown-tests index c33103a6b..8b71e7525 100644 --- a/modules/lchown-tests +++ b/modules/lchown-tests @@ -5,13 +5,13 @@ tests/test-lchown.c Depends-on: mgetgroups progname -sleep +usleep stat-time stdbool symlink configure.ac: -AC_CHECK_FUNCS_ONCE([getegid usleep]) +AC_CHECK_FUNCS_ONCE([getegid]) Makefile.am: TESTS += test-lchown diff --git a/modules/openat-tests b/modules/openat-tests index 62cef8820..e604ca497 100644 --- a/modules/openat-tests +++ b/modules/openat-tests @@ -15,13 +15,13 @@ tests/test-unlinkat.c Depends-on: mgetgroups pathmax -sleep +usleep stat-time symlink unlinkdir configure.ac: -AC_CHECK_FUNCS_ONCE([getegid usleep]) +AC_CHECK_FUNCS_ONCE([getegid]) Makefile.am: TESTS += test-fchownat test-fstatat test-mkdirat test-openat test-unlinkat diff --git a/modules/pipe-filter-gi-tests b/modules/pipe-filter-gi-tests index a02efa8be..f0b2969f5 100644 --- a/modules/pipe-filter-gi-tests +++ b/modules/pipe-filter-gi-tests @@ -11,11 +11,10 @@ binary-io c-ctype read-file full-write -sleep +usleep progname configure.ac: -AC_CHECK_FUNCS_ONCE([usleep]) Makefile.am: TESTS += test-pipe-filter-gi1.sh test-pipe-filter-gi2.sh diff --git a/modules/stat-time-tests b/modules/stat-time-tests index 05b2b6237..0c0b66a79 100644 --- a/modules/stat-time-tests +++ b/modules/stat-time-tests @@ -3,10 +3,9 @@ tests/test-stat-time.c Depends-on: time -sleep +usleep configure.ac: -AC_CHECK_FUNCS_ONCE([usleep]) Makefile.am: TESTS += test-stat-time diff --git a/modules/utimens-tests b/modules/utimens-tests index 25e7476a5..6a2d16168 100644 --- a/modules/utimens-tests +++ b/modules/utimens-tests @@ -8,10 +8,10 @@ tests/test-utimens.c Depends-on: symlink timespec +usleep utimecmp configure.ac: -AC_CHECK_FUNCS_ONCE([usleep]) Makefile.am: TESTS += test-utimens diff --git a/modules/utimensat-tests b/modules/utimensat-tests index 1f161ea35..ded7f74e6 100644 --- a/modules/utimensat-tests +++ b/modules/utimensat-tests @@ -7,10 +7,10 @@ tests/test-utimensat.c Depends-on: progname timespec +usleep utimecmp configure.ac: -AC_CHECK_FUNCS_ONCE([usleep]) Makefile.am: TESTS += test-utimensat diff --git a/tests/test-chown.h b/tests/test-chown.h index 12082e4ef..ab98682e2 100644 --- a/tests/test-chown.h +++ b/tests/test-chown.h @@ -22,11 +22,6 @@ static void nap (void) { -#if !HAVE_USLEEP - /* Assume the worst case file system of FAT, which has a granularity - of 2 seconds. */ - sleep (2); -#else /* HAVE_USLEEP */ static long delay; if (!delay) { @@ -38,8 +33,8 @@ nap (void) 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 2 seconds. However, note that - usleep (2000000) is allowed to fail with EINVAL. */ + 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); @@ -61,13 +56,9 @@ nap (void) } if (! (st1.st_mtime == st2.st_mtime && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2))) - delay = 2000000; + delay = (st1.st_mtime & 1) ? 1000000 : 2000000; } - if (delay == 2000000) - sleep (2); - else - usleep (delay); -#endif /* HAVE_USLEEP */ + usleep (delay); } #if !HAVE_GETEGID diff --git a/tests/test-lchown.h b/tests/test-lchown.h index a1e8b6809..f3ddb7a98 100644 --- a/tests/test-lchown.h +++ b/tests/test-lchown.h @@ -22,11 +22,6 @@ static void nap (void) { -# if !HAVE_USLEEP - /* Assume the worst case file system of FAT, which has a granularity - of 2 seconds. */ - sleep (2); -# else /* HAVE_USLEEP */ static long delay; if (!delay) { @@ -38,8 +33,8 @@ nap (void) 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 2 seconds. However, note that - usleep (2000000) is allowed to fail with EINVAL. */ + 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); @@ -61,13 +56,9 @@ nap (void) } if (! (st1.st_mtime == st2.st_mtime && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2))) - delay = 2000000; + delay = (st1.st_mtime & 1) ? 1000000 : 2000000; } - if (delay == 2000000) - sleep (2); - else - usleep (delay); -# endif /* HAVE_USLEEP */ + usleep (delay); } #endif /* !TEST_CHOWN_NAP */ diff --git a/tests/test-pipe-filter-gi2-main.c b/tests/test-pipe-filter-gi2-main.c index 7be285e08..b9a0b11a9 100644 --- a/tests/test-pipe-filter-gi2-main.c +++ b/tests/test-pipe-filter-gi2-main.c @@ -45,11 +45,7 @@ static void small_nap (void) { -#if HAVE_USLEEP usleep (100000); -#else - sleep (1); -#endif } static char static_buf[5]; diff --git a/tests/test-stat-time.c b/tests/test-stat-time.c index b88af3dc5..85ada8773 100644 --- a/tests/test-stat-time.c +++ b/tests/test-stat-time.c @@ -96,11 +96,6 @@ do_stat (const char *filename, struct stat *p) static void nap (void) { -#if !HAVE_USLEEP - /* Assume the worst case file system of FAT, which has a granularity - of 2 seconds. */ - sleep (2); -#else /* HAVE_USLEEP */ static long delay; if (!delay) { @@ -112,8 +107,8 @@ nap (void) 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 2 seconds. However, note that - usleep (2000000) is allowed to fail with EINVAL. */ + 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 (stat ("t-stt-stamp1", &st1) == 0); @@ -133,13 +128,9 @@ nap (void) } if (! (st1.st_mtime == st2.st_mtime && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2))) - delay = 2000000; + delay = (st1.st_mtime & 1) ? 1000000 : 2000000; } - if (delay == 2000000) - sleep (2); - else - usleep (delay); -#endif /* HAVE_USLEEP */ + usleep (delay); } static void diff --git a/tests/test-utimens-common.h b/tests/test-utimens-common.h index 1945f23e3..6c404cc5d 100644 --- a/tests/test-utimens-common.h +++ b/tests/test-utimens-common.h @@ -58,8 +58,10 @@ enum { static void nap (void) { - /* Systems that lack usleep also lack subsecond timestamps. Our - usage of utimecmp allows equality, so we don't need to sleep. */ + /* 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