diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-25 17:42:23 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-25 17:42:23 +0000 |
commit | c05960ea782363a8d9c68967309839cfcb51d424 (patch) | |
tree | 60abdf12770a554f9c0e152904f2ce52c48eba71 /base | |
parent | 32a93e0c1ac080239558a2ff6a499857d144571f (diff) | |
download | chromium_src-c05960ea782363a8d9c68967309839cfcb51d424.zip chromium_src-c05960ea782363a8d9c68967309839cfcb51d424.tar.gz chromium_src-c05960ea782363a8d9c68967309839cfcb51d424.tar.bz2 |
Destroy all Singletons and LazyInstances between each test.
This modifies base::TestSuite to add a test listener that runs the AtExitManager
between each test.
BUG=110594, 156433, 238654
TEST=*unit*tests
R=apatrick@chromium.org, gspencer@chromium.org, phajdan.jr@chromium.org, pneubeck@chromium.org, rsleevi@chromium.org
Review URL: https://codereview.chromium.org/8947021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@202305 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/test/test_suite.cc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/base/test/test_suite.cc b/base/test/test_suite.cc index 07b9964..2d526927 100644 --- a/base/test/test_suite.cc +++ b/base/test/test_suite.cc @@ -76,6 +76,30 @@ class TestClientInitializer : public testing::EmptyTestEventListener { DISALLOW_COPY_AND_ASSIGN(TestClientInitializer); }; +// This class forces the destruction of all Singletons and LazyInstances +// between tests. Deleting singletons between each test prevents state from +// being shared amongst tests, which can lead to subtle bugs in tests. +class SingletonDestructor : public testing::EmptyTestEventListener { + public: + SingletonDestructor() {} + virtual ~SingletonDestructor() {} + + // testing::EmptyTestEventListener: + virtual void OnTestStart( + const testing::TestInfo& test_info) OVERRIDE { + at_exit_manager_.reset(new base::ShadowingAtExitManager); + } + + virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { + at_exit_manager_.reset(); + } + + private: + scoped_ptr<base::ShadowingAtExitManager> at_exit_manager_; + + DISALLOW_COPY_AND_ASSIGN(SingletonDestructor); +}; + } // namespace TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) { @@ -252,6 +276,12 @@ void TestSuite::Initialize() { CatchMaybeTests(); ResetCommandLine(); + // Add a listener to destroy all Singletons and LazyInstances between each + // test. See SingletonDestructor for more information. + testing::TestEventListeners& listeners = + testing::UnitTest::GetInstance()->listeners(); + listeners.Append(new SingletonDestructor); + TestTimeouts::Initialize(); } |