mirror of
https://git.sr.ht/~cismonx/bookmarkfs
synced 2025-06-07 19:58:50 +00:00
test: refactor tests
- Output more error message. - Improve conditional tests for watcher.
This commit is contained in:
parent
b8d03f008c
commit
97ddb47cc8
3 changed files with 41 additions and 14 deletions
|
@ -59,6 +59,8 @@ dispatch_subcmds (
|
||||||
status = subcmd_prng(argc, argv);
|
status = subcmd_prng(argc, argv);
|
||||||
} else if (0 == strcmp("watcher", cmd)) {
|
} else if (0 == strcmp("watcher", cmd)) {
|
||||||
status = check_watcher(argc, argv);
|
status = check_watcher(argc, argv);
|
||||||
|
} else {
|
||||||
|
log_printf("bad subcmd '%s'", cmd);
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
@ -87,6 +89,7 @@ subcmd_hash (
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
log_printf("bad option '-%c'", optopt);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +113,7 @@ subcmd_prng (
|
||||||
"%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64,
|
"%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64,
|
||||||
&seed[0], &seed[1], &seed[2], &seed[3]);
|
&seed[0], &seed[1], &seed[2], &seed[3]);
|
||||||
if (cnt != 4) {
|
if (cnt != 4) {
|
||||||
|
log_printf("bad seed '%s'", optarg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -119,6 +123,7 @@ subcmd_prng (
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
log_printf("bad option '-%c'", optopt);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,11 +23,15 @@
|
||||||
|
|
||||||
#include "xstd.h"
|
#include "xstd.h"
|
||||||
|
|
||||||
#define ASSERT_EXPR(cond, action_if_false) \
|
#define ASSERT_EXPR_INT(expr, r, cond, action_if_false) \
|
||||||
if (!(cond)) { \
|
do { \
|
||||||
log_printf("assertion (%s) failed", #cond); \
|
int r = (expr); \
|
||||||
action_if_false \
|
if (cond) { \
|
||||||
}
|
break; \
|
||||||
|
} \
|
||||||
|
log_printf("assertion failed: (%d == %s)", r, #expr); \
|
||||||
|
action_if_false \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
int
|
int
|
||||||
check_watcher (
|
check_watcher (
|
||||||
|
|
|
@ -49,11 +49,11 @@ do_check_watcher (
|
||||||
#define FILE1_NAME "foo.tmp"
|
#define FILE1_NAME "foo.tmp"
|
||||||
#define FILE2_NAME "bar.tmp"
|
#define FILE2_NAME "bar.tmp"
|
||||||
|
|
||||||
#define ASSERT_EQ(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((val) != (expr), goto end;)
|
#define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;)
|
||||||
|
|
||||||
unsigned long msecs = 100;
|
unsigned long msecs = 50;
|
||||||
int tries = 5;
|
int tries = 10;
|
||||||
if (flags & WATCHER_FALLBACK) {
|
if (flags & WATCHER_FALLBACK) {
|
||||||
msecs = 2500;
|
msecs = 2500;
|
||||||
tries = 2;
|
tries = 2;
|
||||||
|
@ -63,12 +63,15 @@ do_check_watcher (
|
||||||
|
|
||||||
int status = -1;
|
int status = -1;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
|
struct watcher *w = NULL;
|
||||||
|
|
||||||
fd = openat(dirfd, FILE1_NAME, O_WRONLY | O_CREAT, 0600);
|
fd = openat(dirfd, FILE1_NAME, O_WRONLY | O_CREAT, 0600);
|
||||||
ASSERT_NE(-1, fd);
|
ASSERT_NE(-1, fd);
|
||||||
|
|
||||||
struct watcher *w = watcher_create(dirfd, FILE1_NAME, flags);
|
w = watcher_create(dirfd, FILE1_NAME, flags);
|
||||||
ASSERT_NE(NULL, w);
|
if (w == NULL) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
// Check for spurious zero returns.
|
// Check for spurious zero returns.
|
||||||
ASSERT_EQ(-ETIMEDOUT, wait_for_watcher(w, &ts, tries));
|
ASSERT_EQ(-ETIMEDOUT, wait_for_watcher(w, &ts, tries));
|
||||||
|
|
||||||
|
@ -79,7 +82,9 @@ do_check_watcher (
|
||||||
#if defined(__FreeBSD__)
|
#if defined(__FreeBSD__)
|
||||||
// For kevent() EVFILT_VNODE, ftruncate() only triggers NOTE_ATTRIB,
|
// For kevent() EVFILT_VNODE, ftruncate() only triggers NOTE_ATTRIB,
|
||||||
// which we don't want to watch.
|
// which we don't want to watch.
|
||||||
check_truncate = flags & WATCHER_FALLBACK;
|
if (!(flags & WATCHER_FALLBACK)) {
|
||||||
|
check_truncate = false;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if (check_truncate) {
|
if (check_truncate) {
|
||||||
ASSERT_EQ(0, ftruncate(fd, 0));
|
ASSERT_EQ(0, ftruncate(fd, 0));
|
||||||
|
@ -90,10 +95,19 @@ do_check_watcher (
|
||||||
ASSERT_NE(-1, fd2);
|
ASSERT_NE(-1, fd2);
|
||||||
close(fd2);
|
close(fd2);
|
||||||
|
|
||||||
|
bool close_fd = false;
|
||||||
|
#if defined(__linux__)
|
||||||
// FAN_DELETE_SELF won't fire if the watched file
|
// FAN_DELETE_SELF won't fire if the watched file
|
||||||
// is still opened somewhere.
|
// is still opened somewhere.
|
||||||
close(fd);
|
if (!(flags & WATCHER_FALLBACK)) {
|
||||||
fd = -1;
|
close_fd = true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (close_fd) {
|
||||||
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
ASSERT_EQ(0, renameat(dirfd, FILE2_NAME, dirfd, FILE1_NAME));
|
ASSERT_EQ(0, renameat(dirfd, FILE2_NAME, dirfd, FILE1_NAME));
|
||||||
ASSERT_EQ(0, wait_for_watcher(w, &ts, tries));
|
ASSERT_EQ(0, wait_for_watcher(w, &ts, tries));
|
||||||
|
|
||||||
|
@ -165,18 +179,22 @@ check_watcher (
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
log_printf("bad option '-%c'", optopt);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
if (argc < 1) {
|
if (argc < 1) {
|
||||||
|
log_puts("path not specified");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
int dirfd = open(argv[0], O_RDONLY | O_DIRECTORY);
|
int dirfd = open(argv[0], O_RDONLY | O_DIRECTORY);
|
||||||
if (dirfd < 0) {
|
if (dirfd < 0) {
|
||||||
|
log_printf("failed to open '%s'", argv[0]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int status = do_check_watcher(dirfd, flags);
|
int status = do_check_watcher(dirfd, flags);
|
||||||
close(dirfd);
|
close(dirfd);
|
||||||
return status;
|
return status;
|
||||||
|
|
Loading…
Add table
Reference in a new issue