diff options
author | bauerb <bauerb@chromium.org> | 2015-08-17 13:13:44 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-17 20:14:26 +0000 |
commit | 477a7fb8f985c9f59b7b8f2530779c4e88a0cb12 (patch) | |
tree | 93a34041f8d65b1ba8d534a5177097e66bb8ebfb /base | |
parent | 3f06287763c3938d080a843a0fbc209432f28a6b (diff) | |
download | chromium_src-477a7fb8f985c9f59b7b8f2530779c4e88a0cb12.zip chromium_src-477a7fb8f985c9f59b7b8f2530779c4e88a0cb12.tar.gz chromium_src-477a7fb8f985c9f59b7b8f2530779c4e88a0cb12.tar.bz2 |
Add a ShadowingAtExitManager in the base TestSuite and reset it after every test.
This ensures that any singletons or (non-leaky) lazy instances creating during a test are destroyed afterwards, which reduces the risk of modifying global state between tests.
BUG=nope
Review URL: https://codereview.chromium.org/1285853002
Cr-Commit-Position: refs/heads/master@{#343742}
Diffstat (limited to 'base')
-rw-r--r-- | base/test/test_suite.cc | 29 | ||||
-rw-r--r-- | base/test/test_suite.h | 2 |
2 files changed, 30 insertions, 1 deletions
diff --git a/base/test/test_suite.cc b/base/test/test_suite.cc index fee7b68..a37270c 100644 --- a/base/test/test_suite.cc +++ b/base/test/test_suite.cc @@ -87,6 +87,25 @@ class TestClientInitializer : public testing::EmptyTestEventListener { DISALLOW_COPY_AND_ASSIGN(TestClientInitializer); }; +class AtExitManagerResetter : public testing::EmptyTestEventListener { +public: + AtExitManagerResetter() {} + + void OnTestStart(const testing::TestInfo& test_info) override { + at_exit_manager_.reset(new ShadowingAtExitManager); + } + + void OnTestEnd(const testing::TestInfo& test_info) override { + at_exit_manager_.reset(); + } + + private: + // Ensures that singletons created during a test run are destroyed afterwards. + scoped_ptr<base::ShadowingAtExitManager> at_exit_manager_; + + DISALLOW_COPY_AND_ASSIGN(AtExitManagerResetter); +}; + } // namespace int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) { @@ -154,7 +173,7 @@ void TestSuite::PreInitialize(bool create_at_exit_manager) { // testing/android/native_test_wrapper.cc before main() is called. #if !defined(OS_ANDROID) if (create_at_exit_manager) - at_exit_manager_.reset(new AtExitManager); + CreateAtExitManager(); #endif // Don't add additional code to this function. Instead add it to @@ -179,6 +198,14 @@ void TestSuite::ResetCommandLine() { listeners.Append(new TestClientInitializer); } +void TestSuite::CreateAtExitManager() { + at_exit_manager_.reset(new base::AtExitManager); + + testing::TestEventListeners& listeners = + testing::UnitTest::GetInstance()->listeners(); + listeners.Append(new AtExitManagerResetter); +} + void TestSuite::AddTestLauncherResultPrinter() { // Only add the custom printer if requested. if (!CommandLine::ForCurrentProcess()->HasSwitch( diff --git a/base/test/test_suite.h b/base/test/test_suite.h index cf0dd3a..9cb0fd2 100644 --- a/base/test/test_suite.h +++ b/base/test/test_suite.h @@ -44,6 +44,8 @@ class TestSuite { void ResetCommandLine(); + void CreateAtExitManager(); + void AddTestLauncherResultPrinter(); int Run(); |