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

Side by Side Diff: base/lazy_instance_unittest.cc

Issue 8491043: Allow linker initialization of lazy instance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix CrOS Created 9 years, 1 month 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
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008 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/at_exit.h" 5 #include "base/at_exit.h"
6 #include "base/atomic_sequence_num.h" 6 #include "base/atomic_sequence_num.h"
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/threading/simple_thread.h" 8 #include "base/threading/simple_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 EXPECT_EQ(12, lazy_->Get().some_int()); 49 EXPECT_EQ(12, lazy_->Get().some_int());
50 EXPECT_EQ(12, lazy_->Pointer()->some_int()); 50 EXPECT_EQ(12, lazy_->Pointer()->some_int());
51 } 51 }
52 52
53 private: 53 private:
54 base::LazyInstance<SlowConstructor>* lazy_; 54 base::LazyInstance<SlowConstructor>* lazy_;
55 }; 55 };
56 56
57 } // namespace 57 } // namespace
58 58
59 static base::LazyInstance<ConstructAndDestructLogger> lazy_logger( 59 static base::LazyInstance<ConstructAndDestructLogger> lazy_logger =
60 base::LINKER_INITIALIZED); 60 LINKER_ZERO_INITIALIZED;
61 61
62 TEST(LazyInstanceTest, Basic) { 62 TEST(LazyInstanceTest, Basic) {
63 { 63 {
64 base::ShadowingAtExitManager shadow; 64 base::ShadowingAtExitManager shadow;
65 65
66 EXPECT_EQ(0, constructed_seq_.GetNext()); 66 EXPECT_EQ(0, constructed_seq_.GetNext());
67 EXPECT_EQ(0, destructed_seq_.GetNext()); 67 EXPECT_EQ(0, destructed_seq_.GetNext());
68 68
69 lazy_logger.Get(); 69 lazy_logger.Get();
70 EXPECT_EQ(2, constructed_seq_.GetNext()); 70 EXPECT_EQ(2, constructed_seq_.GetNext());
71 EXPECT_EQ(1, destructed_seq_.GetNext()); 71 EXPECT_EQ(1, destructed_seq_.GetNext());
72 72
73 lazy_logger.Pointer(); 73 lazy_logger.Pointer();
74 EXPECT_EQ(3, constructed_seq_.GetNext()); 74 EXPECT_EQ(3, constructed_seq_.GetNext());
75 EXPECT_EQ(2, destructed_seq_.GetNext()); 75 EXPECT_EQ(2, destructed_seq_.GetNext());
76 } 76 }
77 EXPECT_EQ(4, constructed_seq_.GetNext()); 77 EXPECT_EQ(4, constructed_seq_.GetNext());
78 EXPECT_EQ(4, destructed_seq_.GetNext()); 78 EXPECT_EQ(4, destructed_seq_.GetNext());
79 } 79 }
80 80
81 static base::LazyInstance<SlowConstructor> lazy_slow(base::LINKER_INITIALIZED); 81 static base::LazyInstance<SlowConstructor> lazy_slow = LINKER_ZERO_INITIALIZED;
82 82
83 TEST(LazyInstanceTest, ConstructorThreadSafety) { 83 TEST(LazyInstanceTest, ConstructorThreadSafety) {
84 { 84 {
85 base::ShadowingAtExitManager shadow; 85 base::ShadowingAtExitManager shadow;
86 86
87 SlowDelegate delegate(&lazy_slow); 87 SlowDelegate delegate(&lazy_slow);
88 EXPECT_EQ(0, SlowConstructor::constructed); 88 EXPECT_EQ(0, SlowConstructor::constructed);
89 89
90 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); 90 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5);
91 pool.AddWork(&delegate, 20); 91 pool.AddWork(&delegate, 20);
(...skipping 23 matching lines...) Expand all
115 }; 115 };
116 116
117 } // anonymous namespace 117 } // anonymous namespace
118 118
119 TEST(LazyInstanceTest, LeakyLazyInstance) { 119 TEST(LazyInstanceTest, LeakyLazyInstance) {
120 // Check that using a plain LazyInstance causes the dtor to run 120 // Check that using a plain LazyInstance causes the dtor to run
121 // when the AtExitManager finishes. 121 // when the AtExitManager finishes.
122 bool deleted1 = false; 122 bool deleted1 = false;
123 { 123 {
124 base::ShadowingAtExitManager shadow; 124 base::ShadowingAtExitManager shadow;
125 static base::LazyInstance<DeleteLogger> test(base::LINKER_INITIALIZED); 125 static base::LazyInstance<DeleteLogger> test = LINKER_ZERO_INITIALIZED;
126 test.Get().SetDeletedPtr(&deleted1); 126 test.Get().SetDeletedPtr(&deleted1);
127 } 127 }
128 EXPECT_TRUE(deleted1); 128 EXPECT_TRUE(deleted1);
129 129
130 // Check that using a *leaky* LazyInstance makes the dtor not run 130 // Check that using a *leaky* LazyInstance makes the dtor not run
131 // when the AtExitManager finishes. 131 // when the AtExitManager finishes.
132 bool deleted2 = false; 132 bool deleted2 = false;
133 { 133 {
134 base::ShadowingAtExitManager shadow; 134 base::ShadowingAtExitManager shadow;
135 static base::LazyInstance<DeleteLogger, 135 static base::LazyInstance<DeleteLogger,
136 base::LeakyLazyInstanceTraits<DeleteLogger> > 136 base::LeakyLazyInstanceTraits<DeleteLogger> >
137 test(base::LINKER_INITIALIZED); 137 test = LINKER_ZERO_INITIALIZED;
138 test.Get().SetDeletedPtr(&deleted2); 138 test.Get().SetDeletedPtr(&deleted2);
139 } 139 }
140 EXPECT_FALSE(deleted2); 140 EXPECT_FALSE(deleted2);
141 } 141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698