Chromium Code Reviews

Side by Side 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: NSS Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/test/test_suite.h" 5 #include "base/test/test_suite.h"
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/base_paths.h" 8 #include "base/base_paths.h"
9 #include "base/base_switches.h" 9 #include "base/base_switches.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 58 matching lines...)
69 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { 69 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
70 *CommandLine::ForCurrentProcess() = old_command_line_; 70 *CommandLine::ForCurrentProcess() = old_command_line_;
71 } 71 }
72 72
73 private: 73 private:
74 CommandLine old_command_line_; 74 CommandLine old_command_line_;
75 75
76 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer); 76 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
77 }; 77 };
78 78
79 // This class forces the destruction of all Singletons and LazyInstances
80 // between tests. Deleting singletons between each test prevents state from
81 // being shared amongst tests, which can lead to subtle bugs in tests.
82 class SingletonDestructor : public testing::EmptyTestEventListener {
83 public:
84 SingletonDestructor() {}
85 virtual ~SingletonDestructor() {}
86
87 // testing::EmptyTestEventListener:
88 virtual void OnTestStart(
89 const testing::TestInfo& test_info) OVERRIDE {
90 at_exit_manager_.reset(new base::ShadowingAtExitManager);
91 }
92
93 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
94 at_exit_manager_.reset();
95 }
96
97 private:
98 scoped_ptr<base::ShadowingAtExitManager> at_exit_manager_;
99
100 DISALLOW_COPY_AND_ASSIGN(SingletonDestructor);
101 };
102
79 } // namespace 103 } // namespace
80 104
81 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) { 105 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
82 PreInitialize(argc, argv, true); 106 PreInitialize(argc, argv, true);
83 } 107 }
84 108
85 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager) 109 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
86 : initialized_command_line_(false) { 110 : initialized_command_line_(false) {
87 PreInitialize(argc, argv, create_at_exit_manager); 111 PreInitialize(argc, argv, create_at_exit_manager);
88 } 112 }
(...skipping 156 matching lines...)
245 SuppressErrorDialogs(); 269 SuppressErrorDialogs();
246 base::debug::SetSuppressDebugUI(true); 270 base::debug::SetSuppressDebugUI(true);
247 logging::SetLogAssertHandler(UnitTestAssertHandler); 271 logging::SetLogAssertHandler(UnitTestAssertHandler);
248 } 272 }
249 273
250 icu_util::Initialize(); 274 icu_util::Initialize();
251 275
252 CatchMaybeTests(); 276 CatchMaybeTests();
253 ResetCommandLine(); 277 ResetCommandLine();
254 278
279 // Add a listener to destroy all Singletons and LazyInstances between each
280 // test. See SingletonDestructor for more information.
281 testing::TestEventListeners& listeners =
282 testing::UnitTest::GetInstance()->listeners();
283 listeners.Append(new SingletonDestructor);
284
255 TestTimeouts::Initialize(); 285 TestTimeouts::Initialize();
256 } 286 }
257 287
258 void TestSuite::Shutdown() { 288 void TestSuite::Shutdown() {
259 } 289 }
OLDNEW
« no previous file with comments | « no previous file | chromeos/network/onc/onc_certificate_importer_unittest.cc » ('j') | crypto/nss_util.cc » ('J')

Powered by Google App Engine