diff --git a/test/test_common/test_base.cc b/test/test_common/test_base.cc index 0a10e332ac4a4..b40b56fdb6633 100644 --- a/test/test_common/test_base.cc +++ b/test/test_common/test_base.cc @@ -6,13 +6,12 @@ namespace Envoy { -void TestBase::checkSingletonQuiescensce() { +void TestListener::OnTestEnd(const ::testing::TestInfo& test_info) { // Check that all singletons have been destroyed. std::string active_singletons = Envoy::Test::Globals::describeActiveSingletons(); RELEASE_ASSERT(active_singletons.empty(), - absl::StrCat("FAIL: Active singletons exist:\n", active_singletons)); + absl::StrCat("FAIL [", test_info.test_suite_name(), ".", test_info.name(), + "]: Active singletons exist:\n", active_singletons)); } -TestBase::~TestBase() { checkSingletonQuiescensce(); } - } // namespace Envoy diff --git a/test/test_common/test_base.h b/test/test_common/test_base.h index defb51b17ad5f..7723081b2317e 100644 --- a/test/test_common/test_base.h +++ b/test/test_common/test_base.h @@ -4,7 +4,7 @@ namespace Envoy { -// Provides a common test-base class for all tests in Envoy to use. This offers +// Provides a test listener to be called after each test method. This offers // a place to put hooks we'd like to run on every test. There's currently a // check that all test-scoped singletons have been destroyed. A test-scoped // singleton might remain at the end of a test if it's transitively referenced @@ -17,18 +17,16 @@ namespace Envoy { // than there were at the start of it. This is likely to fail in a few // places when introduced, but we could add known test overrides for this. // -// Note: nothing compute-intensive should be put in this test-class, as it will +// Note: nothing compute-intensive should be put in this class, as it will // be a tax paid by every test method in the codebase. -class TestBase : public ::testing::Test { -public: - static void checkSingletonQuiescensce(); - ~TestBase() override; +class TestListener : public ::testing::EmptyTestEventListener { + void OnTestEnd(const ::testing::TestInfo& test_info) override; }; -// Templatized version of TestBase. See above notes. -template class TestBaseWithParam : public ::testing::TestWithParam { -public: - ~TestBaseWithParam() { TestBase::checkSingletonQuiescensce(); } -}; +// TODO(jmarantz): Before Alyssa found this TestListener hook for me, we had +// merged a PR that added the same functionality via common TestBase classes. +// These should be removed. +using TestBase = ::testing::Test; +template using TestBaseWithParam = ::testing::TestWithParam; } // namespace Envoy diff --git a/test/test_runner.h b/test/test_runner.h index 8070c127c126f..b19c7e0e8d999 100644 --- a/test/test_runner.h +++ b/test/test_runner.h @@ -18,6 +18,13 @@ class TestRunner { ::testing::InitGoogleMock(&argc, argv); Event::Libevent::Global::initialize(); + // Add a test-listener so we can call a hook where we can do a quiescence + // check after each method. See + // https://github.com/google/googletest/blob/master/googletest/docs/advanced.md + // for details. + ::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners(); + listeners.Append(new TestListener); + // Use the recommended, but not default, "threadsafe" style for the Death Tests. // See: https://github.com/google/googletest/commit/84ec2e0365d791e4ebc7ec249f09078fb5ab6caa ::testing::FLAGS_gtest_death_test_style = "threadsafe"; @@ -45,12 +52,7 @@ class TestRunner { file_logger = std::make_unique( TestEnvironment::getOptions().logPath(), access_log_manager, Logger::Registry::getSink()); } - int exit_status = RUN_ALL_TESTS(); - - // Check that all singletons have been destroyed. - TestBase::checkSingletonQuiescensce(); - - return exit_status; + return RUN_ALL_TESTS(); } }; } // namespace Envoy