From bda91be7512d14430cc93d22b8a3a5b6d151158b Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 14 Feb 2017 19:49:46 +0000 Subject: [PATCH 07/11] tests: Wrap file-deletion functions to handle EINTR The GLib functions we're using don't, and it seems to be possible to be interrupted during cleanup for our tests. Signed-off-by: Simon McVittie --- test/dbus-daemon.c | 5 +++-- test/loopback.c | 4 ++-- test/test-utils-glib.c | 41 +++++++++++++++++++++++++++++++++++++++++ test/test-utils-glib.h | 3 +++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/test/dbus-daemon.c b/test/dbus-daemon.c index 35538c68..d0b44d72 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -1085,10 +1085,11 @@ teardown (Fixture *f, /* the socket may exist */ path = g_strdup_printf ("%s/bus", f->tmp_runtime_dir); - g_assert (g_remove (path) == 0 || errno == ENOENT); + + test_remove_if_exists (path); g_free (path); /* there shouldn't be anything else in there */ - g_assert_cmpint (g_rmdir (f->tmp_runtime_dir), ==, 0); + test_rmdir_must_exist (f->tmp_runtime_dir); /* we're relying on being single-threaded for this to be safe */ if (f->saved_runtime_dir != NULL) diff --git a/test/loopback.c b/test/loopback.c index bf0542aa..5232a09a 100644 --- a/test/loopback.c +++ b/test/loopback.c @@ -332,10 +332,10 @@ teardown_runtime (Fixture *f, /* the socket may exist */ path = g_strdup_printf ("%s/bus", f->tmp_runtime_dir); - g_assert (g_remove (path) == 0 || errno == ENOENT); + test_remove_if_exists (path); g_free (path); /* there shouldn't be anything else in there */ - g_assert_cmpint (g_rmdir (f->tmp_runtime_dir), ==, 0); + test_rmdir_must_exist (f->tmp_runtime_dir); /* we're relying on being single-threaded for this to be safe */ if (f->saved_runtime_dir != NULL) diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c index 5991b122..711cfb07 100644 --- a/test/test-utils-glib.c +++ b/test/test-utils-glib.c @@ -479,3 +479,44 @@ test_progress (char symbol) if (g_test_verbose () && isatty (1)) g_print ("%c", symbol); } + +/* + * Delete @path, with a retry loop if the system call is interrupted by + * an async signal. If @path does not exist, ignore; otherwise, it is + * required to be a non-directory. + */ +void +test_remove_if_exists (const gchar *path) +{ + while (g_remove (path) != 0) + { + int saved_errno = errno; + + if (saved_errno == ENOENT) + return; + + if (saved_errno == EINTR) + continue; + + g_error ("Unable to remove file \"%s\": %s", path, g_strerror (errno)); + } +} + +/* + * Delete empty directory @path, with a retry loop if the system call is + * interrupted by an async signal. @path is required to exist. + */ +void +test_rmdir_must_exist (const gchar *path) +{ + while (g_remove (path) != 0) + { + int saved_errno = errno; + + if (saved_errno == EINTR) + continue; + + g_error ("Unable to remove directory \"%s\": %s", path, + g_strerror (errno)); + } +} diff --git a/test/test-utils-glib.h b/test/test-utils-glib.h index 4016f73f..e62ef3d6 100644 --- a/test/test-utils-glib.h +++ b/test/test-utils-glib.h @@ -92,4 +92,7 @@ static inline void my_test_skip (const gchar *s) } #endif +void test_remove_if_exists (const gchar *path); +void test_rmdir_must_exist (const gchar *path); + #endif -- 2.11.0