chromium/base/lazy_instance_unittest.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <memory>
#include <utility>
#include <vector>

#include "base/at_exit.h"
#include "base/atomic_sequence_num.h"
#include "base/atomicops.h"
#include "base/barrier_closure.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/memory/aligned_memory.h"
#include "base/memory/raw_ptr.h"
#include "base/system/sys_info.h"
#include "base/threading/platform_thread.h"
#include "base/threading/simple_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

base::AtomicSequenceNumber constructed_seq_;
base::AtomicSequenceNumber destructed_seq_;

class ConstructAndDestructLogger {};

class SlowConstructor {};

// static
int SlowConstructor::constructed =;

class SlowDelegate : public base::DelegateSimpleThread::Delegate {};

}  // namespace

base::LazyInstance<ConstructAndDestructLogger>::DestructorAtExit lazy_logger =;

TEST(LazyInstanceTest, Basic) {}

base::LazyInstance<SlowConstructor>::DestructorAtExit lazy_slow =;

TEST(LazyInstanceTest, ConstructorThreadSafety) {}

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 {};

}  // anonymous namespace

TEST(LazyInstanceTest, LeakyLazyInstance) {}

namespace {

template <size_t alignment>
class AlignedData {};

}  // namespace

TEST(LazyInstanceTest, Alignment) {}

namespace {

// A class whose constructor busy-loops until it is told to complete
// construction.
class BlockingConstructor {};

// A SimpleThread running at |thread_type| which invokes |before_get| (optional)
// and then invokes Get() on the LazyInstance it's assigned.
class BlockingConstructorThread : public base::SimpleThread {};

// static
base::subtle::Atomic32 BlockingConstructor::constructor_called_ =;
// static
base::subtle::Atomic32 BlockingConstructor::complete_construction_ =;

base::LazyInstance<BlockingConstructor>::DestructorAtExit lazy_blocking =;

}  // namespace

// Tests that if the thread assigned to construct the LazyInstance runs at
// background priority : the foreground threads will yield to it enough for it
// to eventually complete construction.
// This is a regression test for https://crbug.com/797129.
TEST(LazyInstanceTest, PriorityInversionAtInitializationResolves) {}