You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by ab...@apache.org on 2021/11/16 20:46:03 UTC

[kudu] 03/03: [tests] Add iteration to test dir path

This is an automated email from the ASF dual-hosted git repository.

abukor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit c423172ca60383a63b3e5ca00f767ccc11001180
Author: Attila Bukor <ab...@apache.org>
AuthorDate: Tue Nov 16 18:07:41 2021 +0100

    [tests] Add iteration to test dir path
    
    GTest supports running tests repeatedly by setting a "--gtest_repeat"
    flag, which can be incredibly useful to debug flaky tests when using
    together with "--gtest_break_on_failure" in gdb.
    
    Unfortunately, most of our tests expect an empty data directory, but the
    test directory was the same across repeated runs without being emptied.
    
    Adding the iteration number to the end of the directory solves this
    problem and --gtest_repeat can be used safely.
    
    Change-Id: I175756e6f758246f2234eae3a0ad99a544f80026
    Reviewed-on: http://gerrit.cloudera.org:8080/18030
    Reviewed-by: Andrew Wong <aw...@cloudera.com>
    Reviewed-by: Alexey Serbin <as...@cloudera.com>
    Tested-by: Attila Bukor <ab...@apache.org>
---
 src/kudu/util/test_main.cc |  5 +++--
 src/kudu/util/test_util.cc | 19 +++++++++++++++----
 src/kudu/util/test_util.h  |  4 ++++
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/kudu/util/test_main.cc b/src/kudu/util/test_main.cc
index c75e5ae..ccdc418 100644
--- a/src/kudu/util/test_main.cc
+++ b/src/kudu/util/test_main.cc
@@ -103,7 +103,8 @@ int main(int argc, char **argv) {
   // cover our bases and call it here too.
   kudu::KuduTest::OverrideKrb5Environment();
 
-  int ret = RUN_ALL_TESTS();
+  testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners();
+  listeners.Append(new kudu::KuduTestEventListener());
 
-  return ret;
+  return RUN_ALL_TESTS();
 }
diff --git a/src/kudu/util/test_util.cc b/src/kudu/util/test_util.cc
index dd6eb1b..a01fbb0 100644
--- a/src/kudu/util/test_util.cc
+++ b/src/kudu/util/test_util.cc
@@ -71,6 +71,10 @@ using std::string;
 using std::vector;
 using strings::Substitute;
 
+namespace {
+int testIteration = 0;
+} // namespace
+
 namespace kudu {
 
 const char* kInvalidPath = "/dev/invalid-path-for-kudu-tests";
@@ -86,6 +90,11 @@ static const uint64_t kTestBeganAtMicros = Env::Default()->NowMicros();
 // This can be checked using the 'IsGTest()' function from test_util_prod.cc.
 bool g_is_gtest = true;
 
+void KuduTestEventListener::OnTestIterationStart(const testing::UnitTest& /*unit_test*/,
+                                                 int iteration) {
+  testIteration = iteration;
+}
+
 ///////////////////////////////////////////////////
 // KuduTest
 ///////////////////////////////////////////////////
@@ -130,7 +139,7 @@ KuduTest::KuduTest()
   // If the TEST_TMPDIR variable has been set, then glog will automatically use that
   // as its default log directory. We would prefer that the default log directory
   // instead be the test-case-specific subdirectory.
-  FLAGS_log_dir = GetTestDataDirectory();
+  FLAGS_log_dir = test_dir_;
 }
 
 KuduTest::~KuduTest() {
@@ -223,8 +232,9 @@ string GetTestDataDirectory() {
   // The directory name includes some strings for specific reasons:
   // - program name: identifies the directory to the test invoker
   // - timestamp and pid: disambiguates with prior runs of the same test
+  // - iteration: identifies the iteration when using --gtest_repeat
   //
-  // e.g. "env-test.TestEnv.TestReadFully.1409169025392361-23600"
+  // e.g. "env-test.TestEnv.TestReadFully.1409169025392361-23600-0"
   //
   // If the test is sharded, the shard index is also included so that the test
   // invoker can more easily identify all directories belonging to each shard.
@@ -233,13 +243,14 @@ string GetTestDataDirectory() {
   if (shard_index && shard_index[0] != '\0') {
     shard_index_infix = Substitute("$0.", shard_index);
   }
-  dir += Substitute("/$0.$1$2.$3.$4-$5",
+  dir += Substitute("/$0.$1$2.$3.$4-$5-$6",
     StringReplace(google::ProgramInvocationShortName(), "/", "_", true),
     shard_index_infix,
     StringReplace(test_info->test_case_name(), "/", "_", true),
     StringReplace(test_info->name(), "/", "_", true),
     kTestBeganAtMicros,
-    getpid());
+    getpid(),
+    testIteration);
   Status s = Env::Default()->CreateDir(dir);
   CHECK(s.IsAlreadyPresent() || s.ok())
     << "Could not create directory " << dir << ": " << s.ToString();
diff --git a/src/kudu/util/test_util.h b/src/kudu/util/test_util.h
index d9150d7..47bdd0e 100644
--- a/src/kudu/util/test_util.h
+++ b/src/kudu/util/test_util.h
@@ -55,6 +55,10 @@ class Status;
 
 extern const char* kInvalidPath;
 
+class KuduTestEventListener : public ::testing::EmptyTestEventListener {
+  void OnTestIterationStart(const testing::UnitTest& unit_test, int iteration) override;
+};
+
 class KuduTest : public ::testing::Test {
  public:
   KuduTest();