Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/threading/thread.h" | 5 #include "base/threading/thread.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 EXPECT_TRUE(a.message_loop()); | 147 EXPECT_TRUE(a.message_loop()); |
| 148 EXPECT_TRUE(a.IsRunning()); | 148 EXPECT_TRUE(a.IsRunning()); |
| 149 | 149 |
| 150 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 150 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 151 base::WaitableEvent::InitialState::NOT_SIGNALED); | 151 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 152 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, | 152 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, |
| 153 base::Unretained(&event))); | 153 base::Unretained(&event))); |
| 154 event.Wait(); | 154 event.Wait(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Intentional test-only race for otherwise untestable code, won't fix. | |
| 158 // https://crbug.com/634383 | |
| 159 #if !defined(THREAD_SANITIZER) | |
| 157 TEST_F(ThreadTest, StartWithOptions_NonJoinable) { | 160 TEST_F(ThreadTest, StartWithOptions_NonJoinable) { |
| 158 Thread* a = new Thread("StartNonJoinable"); | 161 Thread* a = new Thread("StartNonJoinable"); |
| 159 // Non-joinable threads have to be leaked for now (see | 162 // Non-joinable threads have to be leaked for now (see |
| 160 // Thread::Options::joinable for details). | 163 // Thread::Options::joinable for details). |
| 161 ANNOTATE_LEAKING_OBJECT_PTR(a); | 164 ANNOTATE_LEAKING_OBJECT_PTR(a); |
| 162 | 165 |
| 163 Thread::Options options; | 166 Thread::Options options; |
| 164 options.joinable = false; | 167 options.joinable = false; |
| 165 EXPECT_TRUE(a->StartWithOptions(options)); | 168 EXPECT_TRUE(a->StartWithOptions(options)); |
| 166 EXPECT_TRUE(a->message_loop()); | 169 EXPECT_TRUE(a->message_loop()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 186 a->StopSoon(); | 189 a->StopSoon(); |
| 187 EXPECT_TRUE(a->IsRunning()); | 190 EXPECT_TRUE(a->IsRunning()); |
| 188 | 191 |
| 189 // Unblock the task and give a bit of extra time to unwind QuitWhenIdle(). | 192 // Unblock the task and give a bit of extra time to unwind QuitWhenIdle(). |
| 190 block_event.Signal(); | 193 block_event.Signal(); |
| 191 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | 194 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 192 | 195 |
| 193 // The thread should now have stopped on its own. | 196 // The thread should now have stopped on its own. |
| 194 EXPECT_FALSE(a->IsRunning()); | 197 EXPECT_FALSE(a->IsRunning()); |
| 195 } | 198 } |
| 199 #endif | |
| 196 | 200 |
| 197 TEST_F(ThreadTest, TwoTasksOnJoinableThread) { | 201 TEST_F(ThreadTest, TwoTasksOnJoinableThread) { |
| 198 bool was_invoked = false; | 202 bool was_invoked = false; |
| 199 { | 203 { |
| 200 Thread a("TwoTasksOnJoinableThread"); | 204 Thread a("TwoTasksOnJoinableThread"); |
| 201 EXPECT_TRUE(a.Start()); | 205 EXPECT_TRUE(a.Start()); |
| 202 EXPECT_TRUE(a.message_loop()); | 206 EXPECT_TRUE(a.message_loop()); |
| 203 | 207 |
| 204 // Test that all events are dispatched before the Thread object is | 208 // Test that all events are dispatched before the Thread object is |
| 205 // destroyed. We do this by dispatching a sleep event before the | 209 // destroyed. We do this by dispatching a sleep event before the |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 | 285 |
| 282 EXPECT_TRUE(a.Start()); | 286 EXPECT_TRUE(a.Start()); |
| 283 EXPECT_TRUE(a.message_loop()); | 287 EXPECT_TRUE(a.message_loop()); |
| 284 EXPECT_TRUE(a.IsRunning()); | 288 EXPECT_TRUE(a.IsRunning()); |
| 285 | 289 |
| 286 a.Stop(); | 290 a.Stop(); |
| 287 EXPECT_FALSE(a.message_loop()); | 291 EXPECT_FALSE(a.message_loop()); |
| 288 EXPECT_FALSE(a.IsRunning()); | 292 EXPECT_FALSE(a.IsRunning()); |
| 289 } | 293 } |
| 290 | 294 |
| 295 // Intentional test-only race for otherwise untestable code, won't fix. | |
| 296 // https://crbug.com/634383 | |
| 297 #if !defined(THREAD_SANITIZER) | |
| 291 TEST_F(ThreadTest, StartTwiceNonJoinableNotAllowed) { | 298 TEST_F(ThreadTest, StartTwiceNonJoinableNotAllowed) { |
| 299 LOG(ERROR) << __FUNCTION__; | |
|
gab
2016/08/08 21:09:51
Leftover debug code?
| |
| 292 Thread* a = new Thread("StartTwiceNonJoinable"); | 300 Thread* a = new Thread("StartTwiceNonJoinable"); |
| 293 // Non-joinable threads have to be leaked for now (see | 301 // Non-joinable threads have to be leaked for now (see |
| 294 // Thread::Options::joinable for details). | 302 // Thread::Options::joinable for details). |
| 295 ANNOTATE_LEAKING_OBJECT_PTR(a); | 303 ANNOTATE_LEAKING_OBJECT_PTR(a); |
| 296 | 304 |
| 297 Thread::Options options; | 305 Thread::Options options; |
| 298 options.joinable = false; | 306 options.joinable = false; |
| 299 EXPECT_TRUE(a->StartWithOptions(options)); | 307 EXPECT_TRUE(a->StartWithOptions(options)); |
| 300 EXPECT_TRUE(a->message_loop()); | 308 EXPECT_TRUE(a->message_loop()); |
| 301 EXPECT_TRUE(a->IsRunning()); | 309 EXPECT_TRUE(a->IsRunning()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 316 last_task_event.Wait(); | 324 last_task_event.Wait(); |
| 317 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | 325 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 318 | 326 |
| 319 // This test assumes that the above was sufficient to let the thread fully | 327 // This test assumes that the above was sufficient to let the thread fully |
| 320 // stop. | 328 // stop. |
| 321 ASSERT_FALSE(a->IsRunning()); | 329 ASSERT_FALSE(a->IsRunning()); |
| 322 | 330 |
| 323 // Restarting it should not be allowed. | 331 // Restarting it should not be allowed. |
| 324 EXPECT_DCHECK_DEATH(a->Start()); | 332 EXPECT_DCHECK_DEATH(a->Start()); |
| 325 } | 333 } |
| 334 #endif | |
| 326 | 335 |
| 327 TEST_F(ThreadTest, ThreadName) { | 336 TEST_F(ThreadTest, ThreadName) { |
| 328 Thread a("ThreadName"); | 337 Thread a("ThreadName"); |
| 329 EXPECT_TRUE(a.Start()); | 338 EXPECT_TRUE(a.Start()); |
| 330 EXPECT_EQ("ThreadName", a.thread_name()); | 339 EXPECT_EQ("ThreadName", a.thread_name()); |
| 331 } | 340 } |
| 332 | 341 |
| 333 TEST_F(ThreadTest, ThreadId) { | 342 TEST_F(ThreadTest, ThreadId) { |
| 334 Thread a("ThreadId0"); | 343 Thread a("ThreadId0"); |
| 335 Thread b("ThreadId1"); | 344 Thread b("ThreadId1"); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 | 473 |
| 465 a.Stop(); | 474 a.Stop(); |
| 466 EXPECT_FALSE(a.message_loop()); | 475 EXPECT_FALSE(a.message_loop()); |
| 467 EXPECT_FALSE(a.IsRunning()); | 476 EXPECT_FALSE(a.IsRunning()); |
| 468 | 477 |
| 469 // Confirm that running any remaining tasks posted from Stop() goes smoothly | 478 // Confirm that running any remaining tasks posted from Stop() goes smoothly |
| 470 // (e.g. https://codereview.chromium.org/2135413003/#ps300001 crashed if | 479 // (e.g. https://codereview.chromium.org/2135413003/#ps300001 crashed if |
| 471 // StopSoon() posted Thread::ThreadQuitHelper() while |run_loop_| was null). | 480 // StopSoon() posted Thread::ThreadQuitHelper() while |run_loop_| was null). |
| 472 base::RunLoop().RunUntilIdle(); | 481 base::RunLoop().RunUntilIdle(); |
| 473 } | 482 } |
| OLD | NEW |