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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 #if defined(ADDRESS_SANITIZER) | 150 #if defined(ADDRESS_SANITIZER) |
151 // ASan bloats the stack variables and overflows the 12 kb stack. | 151 // ASan bloats the stack variables and overflows the 12 kb stack. |
152 options.stack_size = 24*1024; | 152 options.stack_size = 24*1024; |
153 #else | 153 #else |
154 options.stack_size = 12*1024; | 154 options.stack_size = 12*1024; |
155 #endif | 155 #endif |
156 EXPECT_TRUE(a.StartWithOptions(options)); | 156 EXPECT_TRUE(a.StartWithOptions(options)); |
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 bool was_invoked = false; | 160 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
161 a.task_runner()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked)); | 161 base::WaitableEvent::InitialState::NOT_SIGNALED); |
162 | 162 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, |
163 // wait for the task to run (we could use a kernel event here | 163 base::Unretained(&event))); |
164 // instead to avoid busy waiting, but this is sufficient for | 164 event.Wait(); |
165 // testing purposes). | |
166 for (int i = 100; i >= 0 && !was_invoked; --i) { | |
167 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | |
168 } | |
169 EXPECT_TRUE(was_invoked); | |
170 } | 165 } |
171 | 166 |
172 TEST_F(ThreadTest, TwoTasks) { | 167 TEST_F(ThreadTest, TwoTasks) { |
173 bool was_invoked = false; | 168 bool was_invoked = false; |
174 { | 169 { |
175 Thread a("TwoTasks"); | 170 Thread a("TwoTasks"); |
176 EXPECT_TRUE(a.Start()); | 171 EXPECT_TRUE(a.Start()); |
177 EXPECT_TRUE(a.message_loop()); | 172 EXPECT_TRUE(a.message_loop()); |
178 | 173 |
179 // Test that all events are dispatched before the Thread object is | 174 // Test that all events are dispatched before the Thread object is |
180 // destroyed. We do this by dispatching a sleep event before the | 175 // destroyed. We do this by dispatching a sleep event before the |
181 // event that will toggle our sentinel value. | 176 // event that will toggle our sentinel value. |
182 a.task_runner()->PostTask( | 177 a.task_runner()->PostTask( |
183 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>( | 178 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>( |
184 &base::PlatformThread::Sleep), | 179 &base::PlatformThread::Sleep), |
185 base::TimeDelta::FromMilliseconds(20))); | 180 base::TimeDelta::FromMilliseconds(20))); |
186 a.task_runner()->PostTask(FROM_HERE, | 181 a.task_runner()->PostTask(FROM_HERE, |
187 base::Bind(&ToggleValue, &was_invoked)); | 182 base::Bind(&ToggleValue, &was_invoked)); |
188 } | 183 } |
189 EXPECT_TRUE(was_invoked); | 184 EXPECT_TRUE(was_invoked); |
190 } | 185 } |
191 | 186 |
192 TEST_F(ThreadTest, StopSoon) { | 187 TEST_F(ThreadTest, StopSoon) { |
193 Thread a("StopSoon"); | 188 Thread a("StopSoon"); |
194 EXPECT_TRUE(a.Start()); | 189 EXPECT_TRUE(a.Start()); |
195 EXPECT_TRUE(a.message_loop()); | 190 EXPECT_TRUE(a.message_loop()); |
196 EXPECT_TRUE(a.IsRunning()); | 191 EXPECT_TRUE(a.IsRunning()); |
197 a.StopSoon(); | 192 a.StopSoon(); |
198 a.StopSoon(); | |
199 a.Stop(); | 193 a.Stop(); |
200 EXPECT_FALSE(a.message_loop()); | 194 EXPECT_FALSE(a.message_loop()); |
201 EXPECT_FALSE(a.IsRunning()); | 195 EXPECT_FALSE(a.IsRunning()); |
202 } | 196 } |
| 197 |
| 198 TEST_F(ThreadTest, StopTwiceNop) { |
| 199 Thread a("StopTwiceNop"); |
| 200 EXPECT_TRUE(a.Start()); |
| 201 EXPECT_TRUE(a.message_loop()); |
| 202 EXPECT_TRUE(a.IsRunning()); |
| 203 a.StopSoon(); |
| 204 // Calling StopSoon() a second time should be a nop. |
| 205 a.StopSoon(); |
| 206 a.Stop(); |
| 207 // Same with Stop(). |
| 208 a.Stop(); |
| 209 EXPECT_FALSE(a.message_loop()); |
| 210 EXPECT_FALSE(a.IsRunning()); |
| 211 // Calling them when not running should also nop. |
| 212 a.StopSoon(); |
| 213 a.Stop(); |
| 214 } |
| 215 |
| 216 TEST_F(ThreadTest, StartTwice) { |
| 217 Thread a("StartTwice"); |
| 218 |
| 219 EXPECT_TRUE(a.Start()); |
| 220 EXPECT_TRUE(a.message_loop()); |
| 221 EXPECT_TRUE(a.IsRunning()); |
| 222 a.Stop(); |
| 223 EXPECT_FALSE(a.message_loop()); |
| 224 EXPECT_FALSE(a.IsRunning()); |
| 225 |
| 226 EXPECT_TRUE(a.Start()); |
| 227 EXPECT_TRUE(a.message_loop()); |
| 228 EXPECT_TRUE(a.IsRunning()); |
| 229 a.Stop(); |
| 230 EXPECT_FALSE(a.message_loop()); |
| 231 EXPECT_FALSE(a.IsRunning()); |
| 232 } |
203 | 233 |
204 TEST_F(ThreadTest, ThreadName) { | 234 TEST_F(ThreadTest, ThreadName) { |
205 Thread a("ThreadName"); | 235 Thread a("ThreadName"); |
206 EXPECT_TRUE(a.Start()); | 236 EXPECT_TRUE(a.Start()); |
207 EXPECT_EQ("ThreadName", a.thread_name()); | 237 EXPECT_EQ("ThreadName", a.thread_name()); |
208 } | 238 } |
209 | 239 |
210 TEST_F(ThreadTest, ThreadId) { | 240 TEST_F(ThreadTest, ThreadId) { |
211 Thread a("ThreadId0"); | 241 Thread a("ThreadId0"); |
212 Thread b("ThreadId1"); | 242 Thread b("ThreadId1"); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 EXPECT_FALSE(a.task_runner()); | 325 EXPECT_FALSE(a.task_runner()); |
296 } | 326 } |
297 | 327 |
298 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { | 328 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { |
299 Thread a("MultipleWaitUntilThreadStarted"); | 329 Thread a("MultipleWaitUntilThreadStarted"); |
300 EXPECT_TRUE(a.Start()); | 330 EXPECT_TRUE(a.Start()); |
301 // It's OK to call WaitUntilThreadStarted() multiple times. | 331 // It's OK to call WaitUntilThreadStarted() multiple times. |
302 EXPECT_TRUE(a.WaitUntilThreadStarted()); | 332 EXPECT_TRUE(a.WaitUntilThreadStarted()); |
303 EXPECT_TRUE(a.WaitUntilThreadStarted()); | 333 EXPECT_TRUE(a.WaitUntilThreadStarted()); |
304 } | 334 } |
OLD | NEW |