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

Side by Side Diff: base/test/test_suite.cc

Issue 10735063: Create an AtExitManager around each test, to take care of stray singletons (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 74
75 } // namespace 75 } // namespace
76 76
77 namespace base { 77 namespace base {
78 78
79 class TestWatchAtExitManager : public testing::EmptyTestEventListener { 79 class TestWatchAtExitManager : public testing::EmptyTestEventListener {
80 public: 80 public:
81 TestWatchAtExitManager() { } 81 TestWatchAtExitManager() { }
82 ~TestWatchAtExitManager() { } 82 ~TestWatchAtExitManager() { }
83 83
84 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { 84 virtual void OnTestCaseStart(const testing::TestCase& /*test_case*/) {
85 initial_top_manager_ = AtExitManager::current(); 85 initial_top_manager_ = AtExitManager::current();
86 at_exit_stack_size_ = initial_top_manager_->CallbackStackSize(); 86 at_exit_stack_size_ = initial_top_manager_->CallbackStackSize();
87 per_test_case_manager_.reset(new base::AtExitManager);
88 }
89
90 virtual void OnTestCaseEnd(const testing::TestCase& test_case) {
91 AtExitManager* new_top_manager = AtExitManager::current();
92 if (per_test_case_manager_.get() != new_top_manager) {
93 ADD_FAILURE()
94 << "The current AtExitManager has changed across the test case "
95 << test_case.name() << " most likely because one was created in "
96 << "setting up the test case without being destroyed.";
97 }
98 per_test_case_manager_.reset();
99
100 new_top_manager = AtExitManager::current();
101 size_t new_stack_size = new_top_manager->CallbackStackSize();
102
103 if (initial_top_manager_ != new_top_manager) {
104 ADD_FAILURE()
105 << "Clearing the per test case AtExitManager did not restore the "
106 << "original AtExitManager to the top of the stack, indicating that "
107 << "the testing AtExitManager stack was corrupted while running the "
108 << "test case " << test_case.name() << ".";
109 } else if (new_stack_size != at_exit_stack_size_) {
110 ADD_FAILURE()
111 << "Items were added to the original AtExitManager callback list "
112 << "while running the test case " << test_case.name() << ".";
113 }
114 }
115
116 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
117 per_test_manager_.reset(new base::AtExitManager);
87 } 118 }
88 119
89 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { 120 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
90 AtExitManager* new_top_manager = AtExitManager::current(); 121 AtExitManager* new_top_manager = AtExitManager::current();
91 size_t new_stack_size = new_top_manager->CallbackStackSize(); 122 if (per_test_manager_.get() != new_top_manager) {
123 ADD_FAILURE()
124 << "The current AtExitManager has changed across the test "
125 << test_info.test_case_name() << "." << test_info.name()
126 << " most likely because one was created without being destroyed.";
127 }
128 per_test_manager_.reset();
92 129
93 if (initial_top_manager_ != new_top_manager) { 130 new_top_manager = AtExitManager::current();
94 ADD_FAILURE() << "The current AtExitManager has changed across the " 131 if (per_test_case_manager_.get() != new_top_manager) {
95 "test " << test_info.test_case_name() << "." << test_info.name() << 132 ADD_FAILURE()
96 " most likely because one was created without being destroyed."; 133 << "Clearing the per test AtExitManager did not restore the test "
97 } else if (new_stack_size != at_exit_stack_size_) { 134 << "case AtExitManager to the top of the stack, indicating that the "
98 // TODO(scottbyer): clean up all the errors that result from this and 135 << "testing AtExitManager stack was corrupted while running the test "
99 // turn this into a test failure with 136 << test_info.test_case_name() << "." << test_info.name() << ".";
100 // ADD_FAILURE(). http://crbug.com/133403
101 LOG(WARNING) <<
102 "AtExitManager: items were added to the callback list by " <<
103 test_info.test_case_name() << "." << test_info.name() <<
104 ". Global state should be cleaned up before a test exits.";
105 } 137 }
106 } 138 }
107 139
108 private: 140 private:
109 AtExitManager* initial_top_manager_; 141 AtExitManager* initial_top_manager_;
142 scoped_ptr<base::AtExitManager> per_test_case_manager_;
143 scoped_ptr<base::AtExitManager> per_test_manager_;
110 size_t at_exit_stack_size_; 144 size_t at_exit_stack_size_;
111 145
112 DISALLOW_COPY_AND_ASSIGN(TestWatchAtExitManager); 146 DISALLOW_COPY_AND_ASSIGN(TestWatchAtExitManager);
113 }; 147 };
114 148
115 } // namespace base 149 } // namespace base
116 150
117 const char TestSuite::kStrictFailureHandling[] = "strict_failure_handling"; 151 const char TestSuite::kStrictFailureHandling[] = "strict_failure_handling";
118 152
119 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) { 153 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 !CommandLine::ForCurrentProcess()->HasSwitch( 382 !CommandLine::ForCurrentProcess()->HasSwitch(
349 switches::kSingleProcessChromeFlag)) { 383 switches::kSingleProcessChromeFlag)) {
350 WatchAtExitManager(); 384 WatchAtExitManager();
351 } 385 }
352 386
353 TestTimeouts::Initialize(); 387 TestTimeouts::Initialize();
354 } 388 }
355 389
356 void TestSuite::Shutdown() { 390 void TestSuite::Shutdown() {
357 } 391 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698