Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Unified Diff: base/test/test_suite.cc

Issue 8947021: Destroy all Singletons and LazyInstances between each test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Resurrect() Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/singleton.h ('k') | chromeos/network/onc/onc_certificate_importer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/test_suite.cc
diff --git a/base/test/test_suite.cc b/base/test/test_suite.cc
index 635423feb03b7a3e6ff7d580a2c9b0d92ec2df82..8f05419cfaeedd8acce29be4e8ee3a1011bd9551 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) {
@@ -251,6 +275,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();
}
« no previous file with comments | « base/memory/singleton.h ('k') | chromeos/network/onc/onc_certificate_importer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698