#include "base/threading/scoped_thread_priority.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
#define ASSERT_RUNS_ONCE() …
static ThreadType kAllThreadTypes[] = …;
static_assert …;
static_assert …;
class ScopedThreadPriorityTest : public testing::Test { … };
#if BUILDFLAG(IS_WIN)
void FunctionThatBoostsPriorityOnFirstInvoke(
ThreadPriorityForTest expected_priority) {
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(expected_priority,
PlatformThread::GetCurrentThreadPriorityForTest());
}
void FunctionThatBoostsPriorityOnEveryInvoke() {
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY_REPEATEDLY();
EXPECT_EQ(base::ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
#endif
}
TEST_F(ScopedThreadPriorityTest, BasicTest) { … }
TEST_F(ScopedThreadPriorityTest, WithoutPriorityBoost) { … }
#if BUILDFLAG(IS_WIN)
TEST_F(ScopedThreadPriorityTest, WithPriorityBoost) {
ASSERT_RUNS_ONCE();
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kBackground,
PlatformThread::GetCurrentThreadPriorityForTest());
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
#endif
#if BUILDFLAG(IS_WIN)
TEST_F(ScopedThreadPriorityTest, NestedScope) {
ASSERT_RUNS_ONCE();
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
{
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kNormal,
PlatformThread::GetCurrentThreadPriorityForTest());
}
EXPECT_EQ(ThreadPriorityForTest::kBackground,
PlatformThread::GetCurrentThreadPriorityForTest());
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
#endif
#if BUILDFLAG(IS_WIN)
TEST_F(ScopedThreadPriorityTest, FunctionThatBoostsPriorityOnFirstInvoke) {
ASSERT_RUNS_ONCE();
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
FunctionThatBoostsPriorityOnFirstInvoke(base::ThreadPriorityForTest::kNormal);
FunctionThatBoostsPriorityOnFirstInvoke(
base::ThreadPriorityForTest::kBackground);
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
TEST_F(ScopedThreadPriorityTest, FunctionThatBoostsPriorityOnEveryInvoke) {
PlatformThread::SetCurrentThreadType(ThreadType::kBackground);
FunctionThatBoostsPriorityOnEveryInvoke();
FunctionThatBoostsPriorityOnEveryInvoke();
PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
}
#endif
}