chromium/base/no_destructor_unittest.cc

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

#include "base/no_destructor.h"

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

#include "base/atomicops.h"
#include "base/barrier_closure.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/system/sys_info.h"
#include "base/threading/platform_thread.h"
#include "base/threading/simple_thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

static_assert;
static_assert;

struct CheckOnDestroy {};

TEST(NoDestructorTest, SkipsDestructors) {}

struct UncopyableUnmovable {};

struct CopyOnly {};

struct MoveOnly {};

struct ForwardingTestStruct {};

TEST(NoDestructorTest, UncopyableUnmovable) {}

TEST(NoDestructorTest, ForwardsArguments) {}

TEST(NoDestructorTest, Accessors) {}

// Passing initializer list to a NoDestructor like in this test
// is ambiguous in GCC.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84849
#if !defined(COMPILER_GCC) && !defined(__clang__)
TEST(NoDestructorTest, InitializerList) {
  static NoDestructor<std::vector<std::string>> vector({"a", "b", "c"});
}
#endif
}  // namespace

namespace {

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

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

// A SimpleThread running at |thread_type| which invokes |before_get| (optional)
// and then invokes thread-safe scoped-static-initializationconstruction on its
// NoDestructor instance.
class BlockingConstructorThread : public SimpleThread {};

}  // namespace

// Tests that if the thread assigned to construct the local-static
// initialization of the NoDestructor runs at background priority : the
// foreground threads will yield to it enough for it to eventually complete
// construction. While local-static thread-safe initialization isn't specific to
// NoDestructor, it is tested here as NoDestructor is set to replace
// LazyInstance and this is an important regression test for it
// (https://crbug.com/797129).
TEST(NoDestructorTest, PriorityInversionAtStaticInitializationResolves) {}

}  // namespace base