chromium/base/threading/thread_collision_warner_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 "base/threading/thread_collision_warner.h"

#include <memory>

#include "base/compiler_specific.h"
#include "base/dcheck_is_on.h"
#include "base/memory/raw_ptr.h"
#include "base/synchronization/lock.h"
#include "base/threading/platform_thread.h"
#include "base/threading/simple_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

#if !DCHECK_IS_ON()

// Would cause a memory leak otherwise.
#undef DFAKE_MUTEX
#define DFAKE_MUTEX

// In non-DCHECK builds, we expect the AsserterBase::warn() to not happen
// because the ThreadCollisionWarner's implementation is going to be
// #ifdefined out.
#define EXPECT_NDCHECK_FALSE_DCHECK_TRUE

#else

// In DCHECK builds, we expect the AsserterBase::warn() to happen.
#define EXPECT_NDCHECK_FALSE_DCHECK_TRUE

#endif


namespace {

// This is the asserter used with ThreadCollisionWarner instead of the default
// DCheckAsserter. The method fail_state is used to know if a collision took
// place.
class AssertReporter : public base::AsserterBase {};

}  // namespace

TEST(ThreadCollisionTest, BookCriticalSection) {}

TEST(ThreadCollisionTest, ScopedRecursiveBookCriticalSection) {}

TEST(ThreadCollisionTest, ScopedBookCriticalSection) {}

TEST(ThreadCollisionTest, MTBookCriticalSectionTest) {}

// This unittest accesses a queue in a non-thread-safe manner in an attempt to
// exercise the ThreadCollisionWarner code. When it's run under TSan, the test's
// assumptions pass, but the ThreadSanitizer detects unsafe access and raises a
// warning, causing this unittest to fail. Just ignore this test case when TSan
// is enabled.
#ifndef THREAD_SANITIZER
TEST(ThreadCollisionTest, MTScopedBookCriticalSectionTest) {}
#endif  // THREAD_SANITIZER

TEST(ThreadCollisionTest, MTSynchedScopedBookCriticalSectionTest) {}

TEST(ThreadCollisionTest, MTSynchedScopedRecursiveBookCriticalSectionTest) {}