From 69355768e0f7d932f160767fa17c95f0823b17b7 Mon Sep 17 00:00:00 2001 From: mrambacher Date: Tue, 7 Feb 2023 09:21:46 -0500 Subject: [PATCH] unit_tests: Generate new db dir per test (#353) Add a method that allows the test name to be part of the DB name. This makes it easier to associate a given test run with a given test. Otherwise, some of the tests will overwrite the old data from a previous test. --- db/db_test_util.cc | 2 +- test_util/testharness.cc | 24 ++++++++++++++++++++++++ test_util/testharness.h | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/db/db_test_util.cc b/db/db_test_util.cc index 9125f65099..01c5326375 100644 --- a/db/db_test_util.cc +++ b/db/db_test_util.cc @@ -86,7 +86,7 @@ DBTestBase::DBTestBase(const std::string path, bool env_do_fsync) env_->SetBackgroundThreads(1, Env::LOW); env_->SetBackgroundThreads(1, Env::HIGH); env_->skip_fsync_ = !env_do_fsync; - dbname_ = test::PerThreadDBPath(env_, path); + dbname_ = test::PerThreadDBPath(env_, test::GetTestNameForDB(path)); alternative_wal_dir_ = dbname_ + "/wal"; alternative_db_log_dir_ = dbname_ + "/db_log_dir"; auto options = CurrentOptions(); diff --git a/test_util/testharness.cc b/test_util/testharness.cc index 3c7b835d2f..2b4bcbdd02 100644 --- a/test_util/testharness.cc +++ b/test_util/testharness.cc @@ -32,6 +32,30 @@ ::testing::AssertionResult AssertStatus(const char* s_expr, const Status& s) { } } +// If suggested is empty, the name will be - +// Replaces all of the "/" in the test case/name with "_", so that they will not +// appear as directories +std::string GetTestNameForDB(const std::string& suggested) { + const testing::TestInfo* const test_info = + testing::UnitTest::GetInstance()->current_test_info(); + std::string test_name = test_info->name(); + std::string test_case = test_info->test_case_name(); + auto pos = test_case.find("/"); + if (pos != test_case.npos && !suggested.empty()) { + test_case = suggested; + } else { + while (pos != test_case.npos) { + test_case[pos] = '_'; + pos = test_case.find("/", pos); + } + } + for (pos = test_name.find("/"); pos != test_name.npos; + pos = test_name.find("/", pos)) { + test_name[pos] = '_'; + } + return test_case + "-" + test_name; +} + std::string TmpDir(Env* env) { std::string dir; Status s = env->GetTestDirectory(&dir); diff --git a/test_util/testharness.h b/test_util/testharness.h index d8b6c9679c..0ac549435a 100644 --- a/test_util/testharness.h +++ b/test_util/testharness.h @@ -64,6 +64,9 @@ namespace ROCKSDB_NAMESPACE { namespace test { +// Return a name of the DB for this test, based on the test case/name +std::string GetTestNameForDB(const std::string& suggested = ""); + // Return the directory to use for temporary storage. std::string TmpDir(Env* env = Env::Default());