OLD | NEW |
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 Loading... |
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 LAZY_INSTANCE_INITIALIZER; |
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 = |
| 82 LAZY_INSTANCE_INITIALIZER; |
82 | 83 |
83 TEST(LazyInstanceTest, ConstructorThreadSafety) { | 84 TEST(LazyInstanceTest, ConstructorThreadSafety) { |
84 { | 85 { |
85 base::ShadowingAtExitManager shadow; | 86 base::ShadowingAtExitManager shadow; |
86 | 87 |
87 SlowDelegate delegate(&lazy_slow); | 88 SlowDelegate delegate(&lazy_slow); |
88 EXPECT_EQ(0, SlowConstructor::constructed); | 89 EXPECT_EQ(0, SlowConstructor::constructed); |
89 | 90 |
90 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); | 91 base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); |
91 pool.AddWork(&delegate, 20); | 92 pool.AddWork(&delegate, 20); |
(...skipping 23 matching lines...) Expand all Loading... |
115 }; | 116 }; |
116 | 117 |
117 } // anonymous namespace | 118 } // anonymous namespace |
118 | 119 |
119 TEST(LazyInstanceTest, LeakyLazyInstance) { | 120 TEST(LazyInstanceTest, LeakyLazyInstance) { |
120 // Check that using a plain LazyInstance causes the dtor to run | 121 // Check that using a plain LazyInstance causes the dtor to run |
121 // when the AtExitManager finishes. | 122 // when the AtExitManager finishes. |
122 bool deleted1 = false; | 123 bool deleted1 = false; |
123 { | 124 { |
124 base::ShadowingAtExitManager shadow; | 125 base::ShadowingAtExitManager shadow; |
125 static base::LazyInstance<DeleteLogger> test(base::LINKER_INITIALIZED); | 126 static base::LazyInstance<DeleteLogger> test = LAZY_INSTANCE_INITIALIZER; |
126 test.Get().SetDeletedPtr(&deleted1); | 127 test.Get().SetDeletedPtr(&deleted1); |
127 } | 128 } |
128 EXPECT_TRUE(deleted1); | 129 EXPECT_TRUE(deleted1); |
129 | 130 |
130 // Check that using a *leaky* LazyInstance makes the dtor not run | 131 // Check that using a *leaky* LazyInstance makes the dtor not run |
131 // when the AtExitManager finishes. | 132 // when the AtExitManager finishes. |
132 bool deleted2 = false; | 133 bool deleted2 = false; |
133 { | 134 { |
134 base::ShadowingAtExitManager shadow; | 135 base::ShadowingAtExitManager shadow; |
135 static base::LazyInstance<DeleteLogger, | 136 static base::LazyInstance<DeleteLogger, |
136 base::LeakyLazyInstanceTraits<DeleteLogger> > | 137 base::LeakyLazyInstanceTraits<DeleteLogger> > |
137 test(base::LINKER_INITIALIZED); | 138 test = LAZY_INSTANCE_INITIALIZER; |
138 test.Get().SetDeletedPtr(&deleted2); | 139 test.Get().SetDeletedPtr(&deleted2); |
139 } | 140 } |
140 EXPECT_FALSE(deleted2); | 141 EXPECT_FALSE(deleted2); |
141 } | 142 } |
OLD | NEW |