#include "base/threading/hang_watcher.h"
#include <atomic>
#include <memory>
#include <optional>
#include "base/barrier_closure.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/power_monitor_test.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_checker.h"
#include "base/threading/threading_features.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
ElementsAre;
IsEmpty;
namespace base {
namespace {
const std::vector<base::test::FeatureRefAndParams> kFeatureAndParams{ … };
constexpr TimeDelta kVeryLongDelta{ … };
constexpr TimeDelta kSmallCPUQuantum{ … };
constexpr uint64_t kArbitraryDeadline = …;
constexpr uint64_t kAllOnes = …;
constexpr uint64_t kAllZeros = …;
constexpr uint64_t kOnesThenZeroes = …;
constexpr uint64_t kZeroesThenOnes = …;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
class HangWatcherEnabledInZygoteChildTest
: public testing::TestWithParam<std::tuple<bool, bool>> { … };
TEST_P(HangWatcherEnabledInZygoteChildTest, IsEnabled) { … }
INSTANTIATE_TEST_SUITE_P(…);
#endif
class BlockingThread : public DelegateSimpleThread::Delegate { … };
class HangWatcherTest : public testing::Test { … };
class HangWatcherBlockingThreadTest : public HangWatcherTest { … };
}
TEST_F(HangWatcherTest, InvalidatingExpectationsPreventsCapture) { … }
TEST_F(HangWatcherTest, MultipleInvalidateExpectationsDoNotCancelOut) { … }
TEST_F(HangWatcherTest, NewInnerWatchHangsInScopeAfterInvalidationDetectsHang) { … }
TEST_F(HangWatcherTest,
NewSeparateWatchHangsInScopeAfterInvalidationDetectsHang) { … }
TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) { … }
TEST_F(HangWatcherTest, NewScopeAfterDisabling) { … }
TEST_F(HangWatcherTest, NestedScopes) { … }
TEST_F(HangWatcherBlockingThreadTest, HistogramsLoggedOnHang) { … }
TEST_F(HangWatcherBlockingThreadTest, HistogramsLoggedWithoutHangs) { … }
TEST_F(HangWatcherBlockingThreadTest, HistogramsLoggedWithShutdownFlag) { … }
TEST_F(HangWatcherBlockingThreadTest, Hang) { … }
TEST_F(HangWatcherBlockingThreadTest, HangAlreadyRecorded) { … }
TEST_F(HangWatcherBlockingThreadTest, NoHang) { … }
namespace {
class HangWatcherSnapshotTest : public testing::Test { … };
}
TEST_F(HangWatcherSnapshotTest, NonActionableReport) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_HungThreadIDs …
#else
#define MAYBE_HungThreadIDs …
#endif
TEST_F(HangWatcherSnapshotTest, MAYBE_HungThreadIDs) { … }
TEST_F(HangWatcherSnapshotTest, TimeSinceLastSystemPowerResumeCrashKey) { … }
namespace {
const base::TimeDelta kMonitoringPeriod = …;
class HangWatcherPeriodicMonitoringTest : public testing::Test { … };
}
TEST_F(HangWatcherPeriodicMonitoringTest,
NoPeriodicMonitoringWithoutRegisteredThreads) { … }
TEST_F(HangWatcherPeriodicMonitoringTest, PeriodicCallsTakePlace) { … }
TEST_F(HangWatcherPeriodicMonitoringTest, NoMonitorOnOverSleep) { … }
namespace {
class WatchHangsInScopeBlockingTest : public testing::Test { … };
}
TEST_F(WatchHangsInScopeBlockingTest, ScopeDoesNotBlocksWithoutCapture) { … }
TEST_F(WatchHangsInScopeBlockingTest, ScopeBlocksDuringCapture) { … }
#if BUILDFLAG(IS_MAC) && defined(ARCH_CPU_ARM64)
#define MAYBE_NewScopeDoesNotBlockDuringCapture …
#else
#define MAYBE_NewScopeDoesNotBlockDuringCapture …
#endif
TEST_F(WatchHangsInScopeBlockingTest, MAYBE_NewScopeDoesNotBlockDuringCapture) { … }
namespace internal {
namespace {
constexpr std::array<HangWatchDeadline::Flag, 3> kAllFlags{ … };
}
class HangWatchDeadlineTest : public testing::Test { … };
TEST_F(HangWatchDeadlineTest, BitsPreservedThroughExtract) { … }
TEST_F(HangWatchDeadlineTest, SetAndClearPersistentFlag) { … }
TEST_F(HangWatchDeadlineTest, SetDeadline) { … }
TEST_F(HangWatchDeadlineTest, SetShouldBlockOnHangDeadlineChanged) { … }
TEST_F(HangWatchDeadlineTest, ClearIgnoreHangsDeadlineChanged) { … }
TEST_F(HangWatchDeadlineTest,
SetIgnoreCurrentHangWatchScopeEnableDeadlineChangedd) { … }
TEST_F(HangWatchDeadlineTest, SetDeadlineWipesFlags) { … }
}
}