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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 EXPECT_TRUE(a.message_loop()); | 157 EXPECT_TRUE(a.message_loop()); |
158 EXPECT_TRUE(a.IsRunning()); | 158 EXPECT_TRUE(a.IsRunning()); |
159 | 159 |
160 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 160 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
161 base::WaitableEvent::InitialState::NOT_SIGNALED); | 161 base::WaitableEvent::InitialState::NOT_SIGNALED); |
162 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, | 162 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, |
163 base::Unretained(&event))); | 163 base::Unretained(&event))); |
164 event.Wait(); | 164 event.Wait(); |
165 } | 165 } |
166 | 166 |
167 TEST_F(ThreadTest, TwoTasks) { | 167 TEST_F(ThreadTest, StartWithOptions_NonJoinable) { |
| 168 Thread a("StartNonJoinable"); |
| 169 Thread::Options options; |
| 170 options.joinable = false; |
| 171 EXPECT_TRUE(a.StartWithOptions(options)); |
| 172 EXPECT_TRUE(a.message_loop()); |
| 173 EXPECT_TRUE(a.IsRunning()); |
| 174 |
| 175 // Make the thread block until |block_event| is signaled. |
| 176 base::WaitableEvent block_event( |
| 177 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 178 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 179 a.task_runner()->PostTask( |
| 180 FROM_HERE, |
| 181 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&block_event))); |
| 182 |
| 183 // Stop() shouldn't block despite the thread still being alive. |
| 184 a.Stop(); |
| 185 EXPECT_TRUE(a.IsRunning()); |
| 186 |
| 187 // Unblock the task and give a bit of extra time to unwind QuitWhenIdle(). |
| 188 block_event.Signal(); |
| 189 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 190 |
| 191 // The thread should now have stopped on its own. |
| 192 EXPECT_FALSE(a.IsRunning()); |
| 193 } |
| 194 |
| 195 TEST_F(ThreadTest, TwoTasksOnJoinableThread) { |
168 bool was_invoked = false; | 196 bool was_invoked = false; |
169 { | 197 { |
170 Thread a("TwoTasks"); | 198 Thread a("TwoTasksOnJoinableThread"); |
171 EXPECT_TRUE(a.Start()); | 199 EXPECT_TRUE(a.Start()); |
172 EXPECT_TRUE(a.message_loop()); | 200 EXPECT_TRUE(a.message_loop()); |
173 | 201 |
174 // Test that all events are dispatched before the Thread object is | 202 // Test that all events are dispatched before the Thread object is |
175 // destroyed. We do this by dispatching a sleep event before the | 203 // destroyed. We do this by dispatching a sleep event before the |
176 // event that will toggle our sentinel value. | 204 // event that will toggle our sentinel value. |
177 a.task_runner()->PostTask( | 205 a.task_runner()->PostTask( |
178 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>( | 206 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>( |
179 &base::PlatformThread::Sleep), | 207 &base::PlatformThread::Sleep), |
180 base::TimeDelta::FromMilliseconds(20))); | 208 base::TimeDelta::FromMilliseconds(20))); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 EXPECT_FALSE(a.task_runner()); | 353 EXPECT_FALSE(a.task_runner()); |
326 } | 354 } |
327 | 355 |
328 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { | 356 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { |
329 Thread a("MultipleWaitUntilThreadStarted"); | 357 Thread a("MultipleWaitUntilThreadStarted"); |
330 EXPECT_TRUE(a.Start()); | 358 EXPECT_TRUE(a.Start()); |
331 // It's OK to call WaitUntilThreadStarted() multiple times. | 359 // It's OK to call WaitUntilThreadStarted() multiple times. |
332 EXPECT_TRUE(a.WaitUntilThreadStarted()); | 360 EXPECT_TRUE(a.WaitUntilThreadStarted()); |
333 EXPECT_TRUE(a.WaitUntilThreadStarted()); | 361 EXPECT_TRUE(a.WaitUntilThreadStarted()); |
334 } | 362 } |
OLD | NEW |