[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/at_exit.h" |
| 6 | #include "base/atomic_sequence_num.h" |
| 7 | #include "base/lazy_instance.h" |
| 8 | #include "base/simple_thread.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | class ShadowingAtExitManager : public base::AtExitManager { |
| 14 | public: |
| 15 | ShadowingAtExitManager() : AtExitManager(true) { } |
| 16 | }; |
| 17 | |
| 18 | base::AtomicSequenceNumber constructed_seq_(base::LINKER_INITIALIZED); |
| 19 | base::AtomicSequenceNumber destructed_seq_(base::LINKER_INITIALIZED); |
| 20 | |
| 21 | class ConstructAndDestructLogger { |
| 22 | public: |
| 23 | ConstructAndDestructLogger() { |
| 24 | constructed_seq_.GetNext(); |
| 25 | } |
| 26 | ~ConstructAndDestructLogger() { |
| 27 | destructed_seq_.GetNext(); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | class SlowConstructor { |
| 32 | public: |
| 33 | SlowConstructor() : some_int_(0) { |
| 34 | PlatformThread::Sleep(1000); // Sleep for 1 second to try to cause a race. |
| 35 | ++constructed; |
| 36 | some_int_ = 12; |
| 37 | } |
| 38 | int some_int() const { return some_int_; } |
| 39 | |
| 40 | static int constructed; |
| 41 | private: |
| 42 | int some_int_; |
| 43 | }; |
| 44 | |
| 45 | int SlowConstructor::constructed = 0; |
| 46 | |
| 47 | class SlowDelegate : public base::DelegateSimpleThread::Delegate { |
| 48 | public: |
| 49 | SlowDelegate(base::LazyInstance<SlowConstructor>* lazy) : lazy_(lazy) { } |
| 50 | virtual void Run() { |
| 51 | EXPECT_EQ(12, lazy_->Get().some_int()); |
| 52 | EXPECT_EQ(12, lazy_->Pointer()->some_int()); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | base::LazyInstance<SlowConstructor>* lazy_; |
| 57 | }; |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | static base::LazyInstance<ConstructAndDestructLogger> lazy_logger( |
| 62 | base::LINKER_INITIALIZED); |
| 63 | |
| 64 | TEST(LazyInstanceTest, Basic) { |
| 65 | { |
| 66 | ShadowingAtExitManager shadow; |
| 67 | |
| 68 | EXPECT_EQ(0, constructed_seq_.GetNext()); |
| 69 | EXPECT_EQ(0, destructed_seq_.GetNext()); |
| 70 | |
| 71 | lazy_logger.Get(); |
| 72 | EXPECT_EQ(2, constructed_seq_.GetNext()); |
| 73 | EXPECT_EQ(1, destructed_seq_.GetNext()); |
| 74 | |
| 75 | lazy_logger.Pointer(); |
| 76 | EXPECT_EQ(3, constructed_seq_.GetNext()); |
| 77 | EXPECT_EQ(2, destructed_seq_.GetNext()); |
| 78 | } |
| 79 | EXPECT_EQ(4, constructed_seq_.GetNext()); |
| 80 | EXPECT_EQ(4, destructed_seq_.GetNext()); |
| 81 | } |
| 82 | |
| 83 | static base::LazyInstance<SlowConstructor> lazy_slow(base::LINKER_INITIALIZED); |
| 84 | |
| 85 | TEST(LazyInstanceTest, ConstructorThreadSafety) { |
| 86 | { |
| 87 | ShadowingAtExitManager shadow; |
| 88 | |
| 89 | SlowDelegate delegate(&lazy_slow); |
| 90 | EXPECT_EQ(0, SlowConstructor::constructed); |
| 91 | |
| 92 | base::DelegateSimpleThreadPool pool("lazy_instance_cons", 5); |
| 93 | pool.AddWork(&delegate, 20); |
| 94 | EXPECT_EQ(0, SlowConstructor::constructed); |
| 95 | |
| 96 | pool.Start(); |
| 97 | pool.JoinAll(); |
| 98 | EXPECT_EQ(1, SlowConstructor::constructed); |
| 99 | } |
| 100 | } |