lazy_instance_unittest.cc 4.46 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9
#include "butil/at_exit.h"
#include "butil/atomic_sequence_num.h"
#include "butil/lazy_instance.h"
#include "butil/memory/aligned_memory.h"
#include "butil/threading/simple_thread.h"
gejun's avatar
gejun committed
10 11 12 13
#include <gtest/gtest.h>

namespace {

14 15
butil::StaticAtomicSequenceNumber constructed_seq_;
butil::StaticAtomicSequenceNumber destructed_seq_;
gejun's avatar
gejun committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

class ConstructAndDestructLogger {
 public:
  ConstructAndDestructLogger() {
    constructed_seq_.GetNext();
  }
  ~ConstructAndDestructLogger() {
    destructed_seq_.GetNext();
  }
};

class SlowConstructor {
 public:
  SlowConstructor() : some_int_(0) {
    // Sleep for 1 second to try to cause a race.
31
    butil::PlatformThread::Sleep(butil::TimeDelta::FromSeconds(1));
gejun's avatar
gejun committed
32 33 34 35 36 37 38 39 40 41 42 43
    ++constructed;
    some_int_ = 12;
  }
  int some_int() const { return some_int_; }

  static int constructed;
 private:
  int some_int_;
};

int SlowConstructor::constructed = 0;

44
class SlowDelegate : public butil::DelegateSimpleThread::Delegate {
gejun's avatar
gejun committed
45
 public:
46
  explicit SlowDelegate(butil::LazyInstance<SlowConstructor>* lazy)
gejun's avatar
gejun committed
47 48 49 50 51 52 53 54
      : lazy_(lazy) {}

  virtual void Run() OVERRIDE {
    EXPECT_EQ(12, lazy_->Get().some_int());
    EXPECT_EQ(12, lazy_->Pointer()->some_int());
  }

 private:
55
  butil::LazyInstance<SlowConstructor>* lazy_;
gejun's avatar
gejun committed
56 57 58 59
};

}  // namespace

60
static butil::LazyInstance<ConstructAndDestructLogger> lazy_logger =
gejun's avatar
gejun committed
61 62 63 64
    LAZY_INSTANCE_INITIALIZER;

TEST(LazyInstanceTest, Basic) {
  {
65
    butil::ShadowingAtExitManager shadow;
gejun's avatar
gejun committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    EXPECT_EQ(0, constructed_seq_.GetNext());
    EXPECT_EQ(0, destructed_seq_.GetNext());

    lazy_logger.Get();
    EXPECT_EQ(2, constructed_seq_.GetNext());
    EXPECT_EQ(1, destructed_seq_.GetNext());

    lazy_logger.Pointer();
    EXPECT_EQ(3, constructed_seq_.GetNext());
    EXPECT_EQ(2, destructed_seq_.GetNext());
  }
  EXPECT_EQ(4, constructed_seq_.GetNext());
  EXPECT_EQ(4, destructed_seq_.GetNext());
}

82
static butil::LazyInstance<SlowConstructor> lazy_slow =
gejun's avatar
gejun committed
83 84 85 86
    LAZY_INSTANCE_INITIALIZER;

TEST(LazyInstanceTest, ConstructorThreadSafety) {
  {
87
    butil::ShadowingAtExitManager shadow;
gejun's avatar
gejun committed
88 89 90 91

    SlowDelegate delegate(&lazy_slow);
    EXPECT_EQ(0, SlowConstructor::constructed);

92
    butil::DelegateSimpleThreadPool pool("lazy_instance_cons", 5);
gejun's avatar
gejun committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    pool.AddWork(&delegate, 20);
    EXPECT_EQ(0, SlowConstructor::constructed);

    pool.Start();
    pool.JoinAll();
    EXPECT_EQ(1, SlowConstructor::constructed);
  }
}

namespace {

// DeleteLogger is an object which sets a flag when it's destroyed.
// It accepts a bool* and sets the bool to true when the dtor runs.
class DeleteLogger {
 public:
  DeleteLogger() : deleted_(NULL) {}
  ~DeleteLogger() { *deleted_ = true; }

  void SetDeletedPtr(bool* deleted) {
    deleted_ = deleted;
  }

 private:
  bool* deleted_;
};

}  // anonymous namespace

TEST(LazyInstanceTest, LeakyLazyInstance) {
  // Check that using a plain LazyInstance causes the dtor to run
  // when the AtExitManager finishes.
  bool deleted1 = false;
  {
126 127
    butil::ShadowingAtExitManager shadow;
    static butil::LazyInstance<DeleteLogger> test = LAZY_INSTANCE_INITIALIZER;
gejun's avatar
gejun committed
128 129 130 131 132 133 134 135
    test.Get().SetDeletedPtr(&deleted1);
  }
  EXPECT_TRUE(deleted1);

  // Check that using a *leaky* LazyInstance makes the dtor not run
  // when the AtExitManager finishes.
  bool deleted2 = false;
  {
136 137
    butil::ShadowingAtExitManager shadow;
    static butil::LazyInstance<DeleteLogger>::Leaky
gejun's avatar
gejun committed
138 139 140 141 142 143 144 145 146 147 148 149 150
        test = LAZY_INSTANCE_INITIALIZER;
    test.Get().SetDeletedPtr(&deleted2);
  }
  EXPECT_FALSE(deleted2);
}

namespace {

template <size_t alignment>
class AlignedData {
 public:
  AlignedData() {}
  ~AlignedData() {}
151
  butil::AlignedMemory<alignment, alignment> data_;
gejun's avatar
gejun committed
152 153 154 155 156 157 158 159
};

}  // anonymous namespace

#define EXPECT_ALIGNED(ptr, align) \
    EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))

TEST(LazyInstanceTest, Alignment) {
160
  using butil::LazyInstance;
gejun's avatar
gejun committed
161 162 163 164 165 166 167 168 169 170 171 172

  // Create some static instances with increasing sizes and alignment
  // requirements. By ordering this way, the linker will need to do some work to
  // ensure proper alignment of the static data.
  static LazyInstance<AlignedData<4> > align4 = LAZY_INSTANCE_INITIALIZER;
  static LazyInstance<AlignedData<32> > align32 = LAZY_INSTANCE_INITIALIZER;
  static LazyInstance<AlignedData<4096> > align4096 = LAZY_INSTANCE_INITIALIZER;

  EXPECT_ALIGNED(align4.Pointer(), 4);
  EXPECT_ALIGNED(align32.Pointer(), 32);
  EXPECT_ALIGNED(align4096.Pointer(), 4096);
}