diff --git a/tests/check_lib.c b/tests/check_lib.c index 154798a..8598980 100644 --- a/tests/check_lib.c +++ b/tests/check_lib.c @@ -59,6 +59,8 @@ dispatch_subcmds ( status = subcmd_prng(argc, argv); } else if (0 == strcmp("watcher", cmd)) { status = check_watcher(argc, argv); + } else { + log_printf("bad subcmd '%s'", cmd); } return status; } @@ -87,6 +89,7 @@ subcmd_hash ( break; default: + log_printf("bad option '-%c'", optopt); return -1; } @@ -110,6 +113,7 @@ subcmd_prng ( "%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64, &seed[0], &seed[1], &seed[2], &seed[3]); if (cnt != 4) { + log_printf("bad seed '%s'", optarg); return -1; } break; @@ -119,6 +123,7 @@ subcmd_prng ( break; default: + log_printf("bad option '-%c'", optopt); return -1; } diff --git a/tests/check_lib.h b/tests/check_lib.h index e95ccc5..4d73dbe 100644 --- a/tests/check_lib.h +++ b/tests/check_lib.h @@ -23,11 +23,15 @@ #include "xstd.h" -#define ASSERT_EXPR(cond, action_if_false) \ - if (!(cond)) { \ - log_printf("assertion (%s) failed", #cond); \ - action_if_false \ - } +#define ASSERT_EXPR_INT(expr, r, cond, action_if_false) \ + do { \ + int r = (expr); \ + if (cond) { \ + break; \ + } \ + log_printf("assertion failed: (%d == %s)", r, #expr); \ + action_if_false \ + } while (0) int check_watcher ( diff --git a/tests/check_watcher.c b/tests/check_watcher.c index 444f4c2..9523aef 100644 --- a/tests/check_watcher.c +++ b/tests/check_watcher.c @@ -49,11 +49,11 @@ do_check_watcher ( #define FILE1_NAME "foo.tmp" #define FILE2_NAME "bar.tmp" -#define ASSERT_EQ(val, expr) ASSERT_EXPR((val) == (expr), goto end;) -#define ASSERT_NE(val, expr) ASSERT_EXPR((val) != (expr), goto end;) +#define ASSERT_EQ(val, expr) ASSERT_EXPR_INT(expr, r_, (val) == r_, goto end;) +#define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;) - unsigned long msecs = 100; - int tries = 5; + unsigned long msecs = 50; + int tries = 10; if (flags & WATCHER_FALLBACK) { msecs = 2500; tries = 2; @@ -63,12 +63,15 @@ do_check_watcher ( int status = -1; int fd = -1; + struct watcher *w = NULL; fd = openat(dirfd, FILE1_NAME, O_WRONLY | O_CREAT, 0600); ASSERT_NE(-1, fd); - struct watcher *w = watcher_create(dirfd, FILE1_NAME, flags); - ASSERT_NE(NULL, w); + w = watcher_create(dirfd, FILE1_NAME, flags); + if (w == NULL) { + goto end; + } // Check for spurious zero returns. ASSERT_EQ(-ETIMEDOUT, wait_for_watcher(w, &ts, tries)); @@ -79,7 +82,9 @@ do_check_watcher ( #if defined(__FreeBSD__) // For kevent() EVFILT_VNODE, ftruncate() only triggers NOTE_ATTRIB, // which we don't want to watch. - check_truncate = flags & WATCHER_FALLBACK; + if (!(flags & WATCHER_FALLBACK)) { + check_truncate = false; + } #endif if (check_truncate) { ASSERT_EQ(0, ftruncate(fd, 0)); @@ -90,10 +95,19 @@ do_check_watcher ( ASSERT_NE(-1, fd2); close(fd2); + bool close_fd = false; +#if defined(__linux__) // FAN_DELETE_SELF won't fire if the watched file // is still opened somewhere. - close(fd); - fd = -1; + if (!(flags & WATCHER_FALLBACK)) { + close_fd = true; + } +#endif + if (close_fd) { + close(fd); + fd = -1; + } + ASSERT_EQ(0, renameat(dirfd, FILE2_NAME, dirfd, FILE1_NAME)); ASSERT_EQ(0, wait_for_watcher(w, &ts, tries)); @@ -165,18 +179,22 @@ check_watcher ( break; default: + log_printf("bad option '-%c'", optopt); return -1; } argc -= optind; if (argc < 1) { + log_puts("path not specified"); return -1; } argv += optind; int dirfd = open(argv[0], O_RDONLY | O_DIRECTORY); if (dirfd < 0) { + log_printf("failed to open '%s'", argv[0]); return -1; } + int status = do_check_watcher(dirfd, flags); close(dirfd); return status;