From 3caac5c1f6a7a9c2ed61cdf3cae5e1f8be3319e0 Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Thu, 22 Apr 2021 09:10:15 -0600 Subject: [PATCH] tests: Add single process repo locking tests The semantics of multiple process locking are covered by test-concurrency.py, but the semantics of the repository locking from a single process aren't handled there. This checks how the repository locking is handled from a single thread with one OstreeRepo, a single thread with multiple OstreeRepos, and multiple threads sharing an OstreeRepo. --- tests/test-repo.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/tests/test-repo.c b/tests/test-repo.c index 75e264fee8..5f2037a59e 100644 --- a/tests/test-repo.c +++ b/tests/test-repo.c @@ -51,6 +51,29 @@ setup (Fixture *fixture, g_test_message ("Using temporary directory: %s", fixture->tmpdir.path); } +/* Common setup for locking tests. Create an archive repo in the tmpdir and + * set the locking timeout to 0 so lock failures don't block. + */ +static void +lock_setup (Fixture *fixture, + gconstpointer test_data) +{ + setup (fixture, test_data); + + g_autoptr(GError) error = NULL; + g_autoptr(OstreeRepo) repo = ostree_repo_create_at (fixture->tmpdir.fd, ".", + OSTREE_REPO_MODE_ARCHIVE, + NULL, + NULL, &error); + g_assert_no_error (error); + + /* Set the lock timeout to 0 so failures don't block the test */ + g_autoptr(GKeyFile) config = ostree_repo_copy_config (repo); + g_key_file_set_integer (config, "core", "lock-timeout-secs", 0); + ostree_repo_write_config (repo, config, &error); + g_assert_no_error (error); +} + static void teardown (Fixture *fixture, gconstpointer test_data) @@ -272,6 +295,126 @@ test_repo_autolock (Fixture *fixture, g_autoptr(OstreeRepoAutoLock) lock2 = ostree_repo_auto_lock_push (repo, OSTREE_REPO_LOCK_SHARED, NULL, &error); } +/* Locking from single thread with a single OstreeRepo */ +static void +test_repo_lock_single (Fixture *fixture, + gconstpointer test_data) +{ + g_autoptr(GError) error = NULL; + g_autoptr(OstreeRepo) repo = ostree_repo_open_at (fixture->tmpdir.fd, ".", + NULL, &error); + g_assert_no_error (error); + + /* Single thread on a single repo can freely recurse in any state */ + ostree_repo_lock_push (repo, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (repo, OSTREE_REPO_LOCK_EXCLUSIVE, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (repo, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (repo, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (repo, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (repo, NULL, &error); + g_assert_no_error (error); +} + +/* Locking with single thread and multiple OstreeRepos */ +static void +test_repo_lock_multi_repo (Fixture *fixture, + gconstpointer test_data) +{ + g_autoptr(GError) error = NULL; + + /* Open two OstreeRepo instances */ + g_autoptr(OstreeRepo) repo1 = ostree_repo_open_at (fixture->tmpdir.fd, ".", + NULL, &error); + g_assert_no_error (error); + g_autoptr(OstreeRepo) repo2 = ostree_repo_open_at (fixture->tmpdir.fd, ".", + NULL, &error); + g_assert_no_error (error); + + /* Single thread with multiple OstreeRepo's conflict */ + ostree_repo_lock_push (repo1, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (repo2, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (repo1, OSTREE_REPO_LOCK_EXCLUSIVE, NULL, &error); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK); + g_clear_error (&error); + ostree_repo_lock_pop (repo1, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (repo2, NULL, &error); + g_assert_no_error (error); + + /* Recursive lock should stay exclusive once acquired */ + ostree_repo_lock_push (repo1, OSTREE_REPO_LOCK_EXCLUSIVE, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (repo1, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (repo2, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK); + g_clear_error (&error); + ostree_repo_lock_push (repo2, OSTREE_REPO_LOCK_EXCLUSIVE, NULL, &error); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK); + g_clear_error (&error); + ostree_repo_lock_pop (repo1, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (repo1, NULL, &error); + g_assert_no_error (error); +} + +/* Locking from multiple threads with a single OstreeRepo */ +typedef struct { + OstreeRepo *repo; + gboolean ready; +} LockThreadData; + +static gpointer +lock_thread (gpointer thread_data) +{ + LockThreadData *data = thread_data; + g_autoptr(GError) error = NULL; + + /* Wait until the main thread says to go */ + while (!data->ready) + g_thread_yield (); + + /* Multiple threads on a single repo can freely recurse in any state */ + ostree_repo_lock_push (data->repo, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (data->repo, OSTREE_REPO_LOCK_EXCLUSIVE, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_push (data->repo, OSTREE_REPO_LOCK_SHARED, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (data->repo, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (data->repo, NULL, &error); + g_assert_no_error (error); + ostree_repo_lock_pop (data->repo, NULL, &error); + g_assert_no_error (error); + + return NULL; +} + +static void +test_repo_lock_multi_thread (Fixture *fixture, + gconstpointer test_data) +{ + g_autoptr(GError) error = NULL; + g_autoptr(OstreeRepo) repo = ostree_repo_open_at (fixture->tmpdir.fd, ".", + NULL, &error); + g_assert_no_error (error); + + LockThreadData thread_data = {repo, FALSE}; + GThread *thread1 = g_thread_new ("lock-thread-1", lock_thread, &thread_data); + GThread *thread2 = g_thread_new ("lock-thread-2", lock_thread, &thread_data); + thread_data.ready = TRUE; + g_thread_join (thread1); + g_thread_join (thread2); +} + int main (int argc, char **argv) @@ -291,6 +434,12 @@ main (int argc, test_write_regfile_api, teardown); g_test_add ("/repo/autolock", Fixture, NULL, setup, test_repo_autolock, teardown); + g_test_add ("/repo/lock/single", Fixture, NULL, lock_setup, + test_repo_lock_single, teardown); + g_test_add ("/repo/lock/multi-repo", Fixture, NULL, lock_setup, + test_repo_lock_multi_repo, teardown); + g_test_add ("/repo/lock/multi-thread", Fixture, NULL, lock_setup, + test_repo_lock_multi_thread, teardown); return g_test_run (); }