Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: base/threading/platform_thread_unittest.cc

Issue 1051863003: Turn ThreadPriority enum into an enum class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@setthreadpri
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/synchronization/waitable_event.h" 7 #include "base/synchronization/waitable_event.h"
8 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 } 168 }
169 169
170 namespace { 170 namespace {
171 171
172 const ThreadPriority kThreadPriorityTestValues[] = { 172 const ThreadPriority kThreadPriorityTestValues[] = {
173 // Disable non-normal priority toggling on POSIX as it appears to be broken 173 // Disable non-normal priority toggling on POSIX as it appears to be broken
174 // (http://crbug.com/468793). This is prefered to disabling the tests altogether 174 // (http://crbug.com/468793). This is prefered to disabling the tests altogether
175 // on POSIX as it at least provides coverage for running this code under 175 // on POSIX as it at least provides coverage for running this code under
176 // "normal" priority. 176 // "normal" priority.
177 #if !defined(OS_POSIX) 177 #if !defined(OS_POSIX)
178 kThreadPriority_RealtimeAudio, 178 ThreadPriority::REALTIMEAUDIO,
179 kThreadPriority_Display, 179 ThreadPriority::DISPLAY,
180 kThreadPriority_Background, 180 ThreadPriority::BACKGROUND,
181 #endif // !defined(OS_POSIX) 181 #endif // !defined(OS_POSIX)
182 // Keep normal last to test unbackgrounding. 182 // Keep normal last to test unbackgrounding.
183 kThreadPriority_Normal 183 ThreadPriority::NORMAL
184 }; 184 };
185 185
186 } // namespace 186 } // namespace
187 187
188 // Test changing another thread's priority. 188 // Test changing another thread's priority.
189 // NOTE: This test is partially disabled on POSIX, see note above and 189 // NOTE: This test is partially disabled on POSIX, see note above and
190 // http://crbug.com/468793. 190 // http://crbug.com/468793.
191 TEST(PlatformThreadTest, ThreadPriorityOtherThread) { 191 TEST(PlatformThreadTest, ThreadPriorityOtherThread) {
192 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); 192 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
193 193
194 // Confirm that the current thread's priority is as expected. 194 // Confirm that the current thread's priority is as expected.
195 EXPECT_EQ(kThreadPriority_Normal, 195 EXPECT_EQ(ThreadPriority::NORMAL,
196 PlatformThread::GetThreadPriority(current_handle)); 196 PlatformThread::GetThreadPriority(current_handle));
197 197
198 // Create a test thread. 198 // Create a test thread.
199 FunctionTestThread thread; 199 FunctionTestThread thread;
200 PlatformThreadHandle handle; 200 PlatformThreadHandle handle;
201 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 201 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
202 thread.WaitForThreadStart(); 202 thread.WaitForThreadStart();
203 EXPECT_NE(thread.thread_id(), kInvalidThreadId); 203 EXPECT_NE(thread.thread_id(), kInvalidThreadId);
204 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId()); 204 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
205 205
206 // New threads should get normal priority by default. 206 // New threads should get normal priority by default.
207 EXPECT_EQ(kThreadPriority_Normal, PlatformThread::GetThreadPriority(handle)); 207 EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle));
208 208
209 // Toggle each supported priority on the test thread and confirm it only 209 // Toggle each supported priority on the test thread and confirm it only
210 // affects it (and not the current thread). 210 // affects it (and not the current thread).
211 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { 211 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
212 SCOPED_TRACE(i); 212 SCOPED_TRACE(i);
213 213
214 // Alter and verify the test thread's priority. 214 // Alter and verify the test thread's priority.
215 PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]); 215 PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]);
216 EXPECT_EQ(kThreadPriorityTestValues[i], 216 EXPECT_EQ(kThreadPriorityTestValues[i],
217 PlatformThread::GetThreadPriority(handle)); 217 PlatformThread::GetThreadPriority(handle));
218 218
219 // Make sure the current thread was otherwise unaffected. 219 // Make sure the current thread was otherwise unaffected.
220 EXPECT_EQ(kThreadPriority_Normal, 220 EXPECT_EQ(ThreadPriority::NORMAL,
221 PlatformThread::GetThreadPriority(current_handle)); 221 PlatformThread::GetThreadPriority(current_handle));
222 } 222 }
223 223
224 thread.MarkForTermination(); 224 thread.MarkForTermination();
225 PlatformThread::Join(handle); 225 PlatformThread::Join(handle);
226 } 226 }
227 227
228 // Test changing the current thread's priority (which has different semantics on 228 // Test changing the current thread's priority (which has different semantics on
229 // some platforms). 229 // some platforms).
230 // NOTE: This test is partially disabled on POSIX, see note above and 230 // NOTE: This test is partially disabled on POSIX, see note above and
231 // http://crbug.com/468793. 231 // http://crbug.com/468793.
232 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) { 232 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) {
233 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); 233 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
234 234
235 // Confirm that the current thread's priority is as expected. 235 // Confirm that the current thread's priority is as expected.
236 EXPECT_EQ(kThreadPriority_Normal, 236 EXPECT_EQ(ThreadPriority::NORMAL,
237 PlatformThread::GetThreadPriority(current_handle)); 237 PlatformThread::GetThreadPriority(current_handle));
238 238
239 // Create a test thread for verification purposes only. 239 // Create a test thread for verification purposes only.
240 FunctionTestThread thread; 240 FunctionTestThread thread;
241 PlatformThreadHandle handle; 241 PlatformThreadHandle handle;
242 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 242 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
243 thread.WaitForThreadStart(); 243 thread.WaitForThreadStart();
244 EXPECT_NE(thread.thread_id(), kInvalidThreadId); 244 EXPECT_NE(thread.thread_id(), kInvalidThreadId);
245 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId()); 245 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
246 246
247 // Confirm that the new thread's priority is as expected. 247 // Confirm that the new thread's priority is as expected.
248 EXPECT_EQ(kThreadPriority_Normal, PlatformThread::GetThreadPriority(handle)); 248 EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle));
249 249
250 // Toggle each supported priority on the current thread and confirm it only 250 // Toggle each supported priority on the current thread and confirm it only
251 // affects it (and not the test thread). 251 // affects it (and not the test thread).
252 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { 252 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
253 SCOPED_TRACE(i); 253 SCOPED_TRACE(i);
254 254
255 // Alter and verify the current thread's priority. 255 // Alter and verify the current thread's priority.
256 PlatformThread::SetThreadPriority(current_handle, 256 PlatformThread::SetThreadPriority(current_handle,
257 kThreadPriorityTestValues[i]); 257 kThreadPriorityTestValues[i]);
258 EXPECT_EQ(kThreadPriorityTestValues[i], 258 EXPECT_EQ(kThreadPriorityTestValues[i],
259 PlatformThread::GetThreadPriority(current_handle)); 259 PlatformThread::GetThreadPriority(current_handle));
260 260
261 // Make sure the test thread was otherwise unaffected. 261 // Make sure the test thread was otherwise unaffected.
262 EXPECT_EQ(kThreadPriority_Normal, 262 EXPECT_EQ(ThreadPriority::NORMAL,
263 PlatformThread::GetThreadPriority(handle)); 263 PlatformThread::GetThreadPriority(handle));
264 } 264 }
265 265
266 // Restore current thread priority for follow-up tests. 266 // Restore current thread priority for follow-up tests.
267 PlatformThread::SetThreadPriority(current_handle, kThreadPriority_Normal); 267 PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL);
268 268
269 thread.MarkForTermination(); 269 thread.MarkForTermination();
270 PlatformThread::Join(handle); 270 PlatformThread::Join(handle);
271 } 271 }
272 272
273 } // namespace base 273 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698