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

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

Issue 2135413003: Add |joinable| to Thread::Options (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix compile Created 4 years, 5 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
« base/threading/thread.cc ('K') | « base/threading/thread.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/waitable_event.h" 13 #include "base/synchronization/waitable_event.h"
15 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 14 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
15 #include "base/threading/platform_thread.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h" 18 #include "testing/platform_test.h"
19 19
20 // Death tests misbehave on Android.
21 // TODO(gab): Remove this when https://codereview.chromium.org/2162053006
22 // lands.
23 #if DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
24 #define EXPECT_DCHECK_DEATH(statement, regex) EXPECT_DEATH(statement, regex)
25 #else
26 #define EXPECT_DCHECK_DEATH(statement, regex)
27 #endif
28
20 using base::Thread; 29 using base::Thread;
21 30
22 typedef PlatformTest ThreadTest; 31 typedef PlatformTest ThreadTest;
23 32
24 namespace { 33 namespace {
25 34
26 void ToggleValue(bool* value) { 35 void ToggleValue(bool* value) {
27 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean " 36 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean "
28 "in base/thread_unittest"); 37 "in base/thread_unittest");
29 *value = !*value; 38 *value = !*value;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 EXPECT_TRUE(a.message_loop()); 166 EXPECT_TRUE(a.message_loop());
158 EXPECT_TRUE(a.IsRunning()); 167 EXPECT_TRUE(a.IsRunning());
159 168
160 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, 169 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
161 base::WaitableEvent::InitialState::NOT_SIGNALED); 170 base::WaitableEvent::InitialState::NOT_SIGNALED);
162 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, 171 a.task_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal,
163 base::Unretained(&event))); 172 base::Unretained(&event)));
164 event.Wait(); 173 event.Wait();
165 } 174 }
166 175
167 TEST_F(ThreadTest, TwoTasks) { 176 TEST_F(ThreadTest, StartWithOptions_NonJoinable) {
177 Thread a("StartNonJoinable");
178 Thread::Options options;
179 options.joinable = false;
180 EXPECT_TRUE(a.StartWithOptions(options));
181 EXPECT_TRUE(a.message_loop());
182 EXPECT_TRUE(a.IsRunning());
183
184 // Make the thread block until |block_event| is signaled.
185 base::WaitableEvent block_event(
186 base::WaitableEvent::ResetPolicy::AUTOMATIC,
187 base::WaitableEvent::InitialState::NOT_SIGNALED);
188 a.task_runner()->PostTask(
189 FROM_HERE,
190 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&block_event)));
191
192 // Stop() shouldn't block despite the thread still being alive.
193 a.Stop();
194 EXPECT_TRUE(a.IsRunning());
195
196 // Unblock the task and give a bit of extra time to unwind QuitWhenIdle().
197 block_event.Signal();
198 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20));
199
200 // The thread should now have stopped on its own.
201 EXPECT_FALSE(a.IsRunning());
202 }
203
204 TEST_F(ThreadTest, TwoTasksOnJoinableThread) {
168 bool was_invoked = false; 205 bool was_invoked = false;
169 { 206 {
170 Thread a("TwoTasks"); 207 Thread a("TwoTasksOnJoinableThread");
171 EXPECT_TRUE(a.Start()); 208 EXPECT_TRUE(a.Start());
172 EXPECT_TRUE(a.message_loop()); 209 EXPECT_TRUE(a.message_loop());
173 210
174 // Test that all events are dispatched before the Thread object is 211 // Test that all events are dispatched before the Thread object is
175 // destroyed. We do this by dispatching a sleep event before the 212 // destroyed. We do this by dispatching a sleep event before the
176 // event that will toggle our sentinel value. 213 // event that will toggle our sentinel value.
177 a.task_runner()->PostTask( 214 a.task_runner()->PostTask(
178 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>( 215 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>(
179 &base::PlatformThread::Sleep), 216 &base::PlatformThread::Sleep),
180 base::TimeDelta::FromMilliseconds(20))); 217 base::TimeDelta::FromMilliseconds(20)));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 EXPECT_FALSE(a.IsRunning()); 261 EXPECT_FALSE(a.IsRunning());
225 262
226 EXPECT_TRUE(a.Start()); 263 EXPECT_TRUE(a.Start());
227 EXPECT_TRUE(a.message_loop()); 264 EXPECT_TRUE(a.message_loop());
228 EXPECT_TRUE(a.IsRunning()); 265 EXPECT_TRUE(a.IsRunning());
229 a.Stop(); 266 a.Stop();
230 EXPECT_FALSE(a.message_loop()); 267 EXPECT_FALSE(a.message_loop());
231 EXPECT_FALSE(a.IsRunning()); 268 EXPECT_FALSE(a.IsRunning());
232 } 269 }
233 270
271 TEST_F(ThreadTest, StartTwiceNonJoinableNotAllowed) {
272 Thread a("StartTwiceNonJoinable");
273
274 Thread::Options options;
275 options.joinable = false;
276 EXPECT_TRUE(a.StartWithOptions(options));
277 EXPECT_TRUE(a.message_loop());
278 EXPECT_TRUE(a.IsRunning());
279
280 // Signaled when last task on |a| is processed.
281 base::WaitableEvent last_task_event(
282 base::WaitableEvent::ResetPolicy::AUTOMATIC,
283 base::WaitableEvent::InitialState::NOT_SIGNALED);
284 a.task_runner()->PostTask(
285 FROM_HERE,
286 base::Bind(&base::WaitableEvent::Signal,
287 base::Unretained(&last_task_event)));
288
289 // Stop() is non-blocking, but Yield() to |a|, wait for last task to be
290 // processed and a little more for QuitWhenIdle() to unwind before considering
291 // the thread "stopped".
292 a.Stop();
293 base::PlatformThread::YieldCurrentThread();
294 last_task_event.Wait();
295 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20));
296
297 // This test assumes that the above was sufficient to let the thread fully
298 // stop.
299 ASSERT_FALSE(a.IsRunning());
300
301 // Restarting it should not be allowed.
302 EXPECT_DCHECK_DEATH(a.Start(), "");
303 }
304
234 TEST_F(ThreadTest, ThreadName) { 305 TEST_F(ThreadTest, ThreadName) {
235 Thread a("ThreadName"); 306 Thread a("ThreadName");
236 EXPECT_TRUE(a.Start()); 307 EXPECT_TRUE(a.Start());
237 EXPECT_EQ("ThreadName", a.thread_name()); 308 EXPECT_EQ("ThreadName", a.thread_name());
238 } 309 }
239 310
240 TEST_F(ThreadTest, ThreadId) { 311 TEST_F(ThreadTest, ThreadId) {
241 Thread a("ThreadId0"); 312 Thread a("ThreadId0");
242 Thread b("ThreadId1"); 313 Thread b("ThreadId1");
243 a.Start(); 314 a.Start();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 EXPECT_FALSE(a.task_runner()); 396 EXPECT_FALSE(a.task_runner());
326 } 397 }
327 398
328 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { 399 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) {
329 Thread a("MultipleWaitUntilThreadStarted"); 400 Thread a("MultipleWaitUntilThreadStarted");
330 EXPECT_TRUE(a.Start()); 401 EXPECT_TRUE(a.Start());
331 // It's OK to call WaitUntilThreadStarted() multiple times. 402 // It's OK to call WaitUntilThreadStarted() multiple times.
332 EXPECT_TRUE(a.WaitUntilThreadStarted()); 403 EXPECT_TRUE(a.WaitUntilThreadStarted());
333 EXPECT_TRUE(a.WaitUntilThreadStarted()); 404 EXPECT_TRUE(a.WaitUntilThreadStarted());
334 } 405 }
OLDNEW
« base/threading/thread.cc ('K') | « base/threading/thread.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698