diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 09:11:39 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 09:11:39 +0000 |
commit | 4ea927b886af2654b5a193278c0bfb6c25b56fc7 (patch) | |
tree | 81c0a1916e2b683b687ed6538386fcdbc9261c11 /base/test/test_suite.h | |
parent | d10852ddcea9285a2808a1fa0caeddf4c5e221e0 (diff) | |
download | chromium_src-4ea927b886af2654b5a193278c0bfb6c25b56fc7.zip chromium_src-4ea927b886af2654b5a193278c0bfb6c25b56fc7.tar.gz chromium_src-4ea927b886af2654b5a193278c0bfb6c25b56fc7.tar.bz2 |
Isolate tests by running AtExit callbacks between them.
For now, this is only for base_unittests. The plan is to enable
it for all unit tests. This should finally fix mysterious
problems cause by Singletons surviving after one test etc.
This change also adapts LazyInstance so that it can be reused
after being destroyed. It is used very frequently, for example
each time a MessageLoop is used. It is also worth noting that
we had some problems in the past related to the MessageLoop
being destroyed and re-instantiated in the same test executable.
This patch should also fix that.
TEST=none
BUG=12710
Review URL: http://codereview.chromium.org/372057
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32507 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test/test_suite.h')
-rw-r--r-- | base/test/test_suite.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/base/test/test_suite.h b/base/test/test_suite.h index 42ad027..44e8511 100644 --- a/base/test/test_suite.h +++ b/base/test/test_suite.h @@ -14,8 +14,10 @@ #include "base/debug_on_start.h" #include "base/i18n/icu_util.h" #include "base/multiprocess_test.h" +#include "base/nss_init.h" #include "base/process_util.h" #include "base/scoped_nsautorelease_pool.h" +#include "base/scoped_ptr.h" #include "base/time.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/multiprocess_func_list.h" @@ -27,6 +29,25 @@ // Match function used by the GetTestCount method. typedef bool (*TestMatch)(const testing::TestInfo&); +// By setting up a shadow AtExitManager, this test event listener ensures that +// no state is carried between tests (like singletons, lazy instances, etc). +// Of course it won't help if the code under test corrupts memory. +class TestIsolationEnforcer : public testing::EmptyTestEventListener { + public: + virtual void OnTestStart(const testing::TestInfo& test_info) { + ASSERT_FALSE(exit_manager_.get()); + exit_manager_.reset(new base::ShadowingAtExitManager()); + } + + virtual void OnTestEnd(const testing::TestInfo& test_info) { + ASSERT_TRUE(exit_manager_.get()); + exit_manager_.reset(); + } + + private: + scoped_ptr<base::ShadowingAtExitManager> exit_manager_; +}; + class TestSuite { public: TestSuite(int argc, char** argv) { @@ -77,6 +98,13 @@ class TestSuite { return count; } + // TODO(phajdan.jr): Enforce isolation for all tests once it's stable. + void EnforceTestIsolation() { + testing::TestEventListeners& listeners = + testing::UnitTest::GetInstance()->listeners(); + listeners.Append(new TestIsolationEnforcer); + } + // Don't add additional code to this method. Instead add it to // Initialize(). See bug 6436. int Run() { @@ -172,6 +200,14 @@ class TestSuite { #endif // defined(OS_WIN) icu_util::Initialize(); + +#if defined(OS_LINUX) + // Trying to repeatedly initialize and cleanup NSS and NSPR may result in + // a deadlock. Such repeated initialization will happen when using test + // isolation. Prevent problems by initializing NSS here, so that the cleanup + // will be done only on process exit. + base::EnsureNSSInit(); +#endif // defined(OS_LINUX) } virtual void Shutdown() { |