From c70834914b2adfe6379eaac2b98973764e0bbbf6 Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Tue, 19 Nov 2024 05:39:04 -0500 Subject: [PATCH] feat: locking test data dir when not specified and then deleting lock and dir at end of test --- src/test/util/setup_common.cpp | 37 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 46a6daa88cf88..40d257fe35ca3 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -139,8 +139,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts) const std::string test_name{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""}; if (!m_node.args->IsArgSet("-testdatadir")) { const auto now{TicksSinceEpoch(SystemClock::now())}; - m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / util::ToString(now); - TryCreateDirectories(m_path_root); + m_path_lock = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / util::ToString(now); } else { // Custom data directory m_has_custom_datadir = true; @@ -149,21 +148,24 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts) root_dir = fs::absolute(root_dir); m_path_lock = root_dir / TEST_DIR_PATH_ELEMENT / fs::PathFromString(test_name); - m_path_root = m_path_lock / "datadir"; + } - // Try to obtain the lock; if unsuccessful don't disturb the existing test. - TryCreateDirectories(m_path_lock); - if (util::LockDirectory(m_path_lock, ".lock", /*probe_only=*/false) != util::LockResult::Success) { - ExitFailure("Cannot obtain a lock on test data lock directory " + fs::PathToString(m_path_lock) + '\n' + "The test executable is probably already running."); - } + m_path_root = m_path_lock / "datadir"; + // Try to obtain the lock; if unsuccessful don't disturb the existing test. + TryCreateDirectories(m_path_lock); + if (util::LockDirectory(m_path_lock, ".lock", /*probe_only=*/false) != util::LockResult::Success) { + ExitFailure("Cannot obtain a lock on test data lock directory " + fs::PathToString(m_path_lock) + '\n' + "The test executable is probably already running."); + } - // Always start with a fresh data directory; this doesn't delete the .lock file located one level above. - fs::remove_all(m_path_root); - if (!TryCreateDirectories(m_path_root)) ExitFailure("Cannot create test data directory"); + // Always start with a fresh data directory; this doesn't delete the .lock file located one level above. + fs::remove_all(m_path_root); + if (!TryCreateDirectories(m_path_root)) ExitFailure("Cannot create test data directory"); + if (m_has_custom_datadir) { // Print the test directory name if custom. std::cout << "Test directory (will not be deleted): " << m_path_root << std::endl; } + m_args.ForceSetArg("-datadir", fs::PathToString(m_path_root)); gArgs.ForceSetArg("-datadir", fs::PathToString(m_path_root)); @@ -191,12 +193,13 @@ BasicTestingSetup::~BasicTestingSetup() m_node.kernel.reset(); SetMockTime(0s); // Reset mocktime for following tests LogInstance().DisconnectTestLogger(); - if (m_has_custom_datadir) { - // Only remove the lock file, preserve the data directory. - UnlockDirectory(m_path_lock, ".lock"); - fs::remove(m_path_lock / ".lock"); - } else { - fs::remove_all(m_path_root); + + UnlockDirectory(m_path_lock, ".lock"); + fs::remove(m_path_lock / ".lock"); + + // Only delete dir if not a custom datadir + if (!m_has_custom_datadir) { + fs::remove_all(m_path_lock); } gArgs.ClearArgs(); }