From 4ea927b886af2654b5a193278c0bfb6c25b56fc7 Mon Sep 17 00:00:00 2001 From: "phajdan.jr@chromium.org" Date: Thu, 19 Nov 2009 09:11:39 +0000 Subject: 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 --- base/lazy_instance.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'base/lazy_instance.cc') diff --git a/base/lazy_instance.cc b/base/lazy_instance.cc index 388622a..9923253 100644 --- a/base/lazy_instance.cc +++ b/base/lazy_instance.cc @@ -27,7 +27,14 @@ void LazyInstanceHelper::EnsureInstance(void* instance, // Instance is created, go from CREATING to CREATED. base::subtle::Release_Store(&state_, STATE_CREATED); - // Register the destructor callback with AtExitManager. + + // Allow reusing the LazyInstance (reset it to the initial state). This + // makes possible calling all AtExit callbacks between tests. Assumes that + // no other threads execute when AtExit callbacks are processed. + base::AtExitManager::RegisterCallback(&LazyInstanceHelper::ResetState, + this); + + // Make sure that the lazily instantiated object will get destroyed at exit. base::AtExitManager::RegisterCallback(dtor, instance); } else { // It's either in the process of being created, or already created. Spin. @@ -36,4 +43,9 @@ void LazyInstanceHelper::EnsureInstance(void* instance, } } +// static +void LazyInstanceHelper::ResetState(void* helper) { + reinterpret_cast(helper)->state_ = STATE_EMPTY; +} + } // namespace base -- cgit v1.1