Commit 26cd0cc9 authored by Francois Doray's avatar Francois Doray Committed by Commit Bot

[Base] Test multiple priority transitions on the same thread.

The comment for the existing thread priority test suggested that
it verified that the priority of a thread could be toggled multiple
times, but that wasn't the case.

The new test verifies that every possible priority transition
works as expected on a single thread.

Bug: 872820
Change-Id: I04efe8c50579553962f0d99a5e8e87a54fe268a3
Reviewed-on: https://chromium-review.googlesource.com/1171187
Commit-Queue: François Doray <fdoray@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584524}
parent 66d3d678
...@@ -218,86 +218,97 @@ TEST(PlatformThreadTest, FunctionTimesTen) { ...@@ -218,86 +218,97 @@ TEST(PlatformThreadTest, FunctionTimesTen) {
namespace { namespace {
const ThreadPriority kThreadPriorityTestValues[] = { constexpr ThreadPriority kAllThreadPriorities[] = {
// The order should be higher to lower to cover as much cases as possible on ThreadPriority::REALTIME_AUDIO, ThreadPriority::DISPLAY,
// Linux trybots running without CAP_SYS_NICE permission. ThreadPriority::NORMAL, ThreadPriority::BACKGROUND};
#if !defined(OS_ANDROID)
// PlatformThread::GetCurrentThreadPriority() on Android does not support
// REALTIME_AUDIO case. See http://crbug.com/505474.
ThreadPriority::REALTIME_AUDIO,
#endif
ThreadPriority::DISPLAY,
// This redundant BACKGROUND priority is to test backgrounding from other
// priorities, and unbackgrounding.
ThreadPriority::BACKGROUND,
ThreadPriority::NORMAL,
ThreadPriority::BACKGROUND};
class ThreadPriorityTestThread : public FunctionTestThread { class ThreadPriorityTestThread : public FunctionTestThread {
public: public:
explicit ThreadPriorityTestThread(ThreadPriority priority) explicit ThreadPriorityTestThread() = default;
: priority_(priority) {}
~ThreadPriorityTestThread() override = default; ~ThreadPriorityTestThread() override = default;
private: private:
void RunTest() override { void RunTest() override {
// Confirm that the current thread's priority is as expected. #if !defined(OS_ANDROID)
EXPECT_EQ(ThreadPriority::NORMAL, // TODO(fdoray): PlatformThread::CanIncreaseCurrentThreadPriority()
PlatformThread::GetCurrentThreadPriority()); // incorrectly returns false on Android. There should be a cross-POSIX
// implementation of this method that checks RLIMIT_NICE to determine
// Alter and verify the current thread's priority. // whether it is possible to increase thread priority.
PlatformThread::SetCurrentThreadPriority(priority_); // https://crbug.com/872820
EXPECT_EQ(priority_, PlatformThread::GetCurrentThreadPriority()); if (PlatformThread::CanIncreaseCurrentThreadPriority()) {
#endif
// On platforms that support increasing thread priority, test transition
// between every possible pair of priorities.
for (auto first_priority : kAllThreadPriorities) {
for (auto second_priority : kAllThreadPriorities) {
PlatformThread::SetCurrentThreadPriority(first_priority);
EXPECT_EQ(first_priority, PlatformThread::GetCurrentThreadPriority());
PlatformThread::SetCurrentThreadPriority(second_priority);
EXPECT_EQ(second_priority,
PlatformThread::GetCurrentThreadPriority());
}
}
#if !defined(OS_ANDROID)
} else {
// On platforms that don't support increasing thread priority, the only
// valid transition is NORMAL -> BACKGROUND (it isn't possible to get a
// thread running with a higher priority than NORMAL).
EXPECT_EQ(ThreadPriority::NORMAL,
PlatformThread::GetCurrentThreadPriority());
// Verify that transition to DISPLAY or REALTIME_AUDIO is impossible.
PlatformThread::SetCurrentThreadPriority(ThreadPriority::DISPLAY);
EXPECT_EQ(ThreadPriority::NORMAL,
PlatformThread::GetCurrentThreadPriority());
PlatformThread::SetCurrentThreadPriority(ThreadPriority::REALTIME_AUDIO);
EXPECT_EQ(ThreadPriority::NORMAL,
PlatformThread::GetCurrentThreadPriority());
// Verify that transition to BACKGROUND is possible.
PlatformThread::SetCurrentThreadPriority(ThreadPriority::BACKGROUND);
EXPECT_EQ(ThreadPriority::BACKGROUND,
PlatformThread::GetCurrentThreadPriority());
// Verify that transition to NORMAL, DISPLAY or REALTIME_AUDIO is
// impossible.
PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL);
EXPECT_EQ(ThreadPriority::BACKGROUND,
PlatformThread::GetCurrentThreadPriority());
PlatformThread::SetCurrentThreadPriority(ThreadPriority::DISPLAY);
EXPECT_EQ(ThreadPriority::BACKGROUND,
PlatformThread::GetCurrentThreadPriority());
PlatformThread::SetCurrentThreadPriority(ThreadPriority::REALTIME_AUDIO);
EXPECT_EQ(ThreadPriority::BACKGROUND,
PlatformThread::GetCurrentThreadPriority());
}
#endif // !defined(OS_ANDROID)
} }
const ThreadPriority priority_;
DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread); DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread);
}; };
} // namespace } // namespace
// Test changing a created thread's priority (which has different semantics on // Test changing a created thread's priority (which has different semantics on
// some platforms). // some platforms).
#if defined(OS_FUCHSIA) #if defined(OS_FUCHSIA)
// TODO(crbug.com/851759): Thread priorities are not implemented in Fuchsia. // TODO(crbug.com/851759): Thread priorities are not implemented in Fuchsia.
#define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread #define MAYBE_SetCurrentThreadPriority DISABLED_SetCurrentThreadPriority
#else #else
#define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread #define MAYBE_SetCurrentThreadPriority SetCurrentThreadPriority
#endif #endif
TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { TEST(PlatformThreadTest, MAYBE_SetCurrentThreadPriority) {
const bool increase_priority_allowed = ThreadPriorityTestThread thread;
PlatformThread::CanIncreaseCurrentThreadPriority(); PlatformThreadHandle handle;
// Bump the priority in order to verify that new threads are started with normal
// priority. Skip this on Mac since this platform doesn't allow changing the
// priority of the main thread. Also skip this on platforms that don't allow
// increasing the priority of a thread.
#if !defined(OS_MACOSX)
if (increase_priority_allowed)
PlatformThread::SetCurrentThreadPriority(ThreadPriority::DISPLAY);
#endif
// Toggle each supported priority on the thread and confirm it affects it.
for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
if (!increase_priority_allowed &&
kThreadPriorityTestValues[i] >
PlatformThread::GetCurrentThreadPriority()) {
continue;
}
ThreadPriorityTestThread thread(kThreadPriorityTestValues[i]);
PlatformThreadHandle handle;
ASSERT_FALSE(thread.IsRunning()); ASSERT_FALSE(thread.IsRunning());
ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
thread.WaitForTerminationReady(); thread.WaitForTerminationReady();
ASSERT_TRUE(thread.IsRunning()); ASSERT_TRUE(thread.IsRunning());
thread.MarkForTermination(); thread.MarkForTermination();
PlatformThread::Join(handle); PlatformThread::Join(handle);
ASSERT_FALSE(thread.IsRunning()); ASSERT_FALSE(thread.IsRunning());
}
} }
// This tests internal PlatformThread APIs used under some POSIX platforms, // This tests internal PlatformThread APIs used under some POSIX platforms,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment