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
3 // found in the LICENSE file.
5 #include "base/threading/thread.h"
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
18 typedef PlatformTest ThreadTest
;
22 void ToggleValue(bool* value
) {
23 ANNOTATE_BENIGN_RACE(value
, "Test-only data race on boolean "
24 "in base/thread_unittest");
28 class SleepInsideInitThread
: public Thread
{
30 SleepInsideInitThread() : Thread("none") {
33 this, "Benign test-only data race on vptr - http://crbug.com/98219");
35 ~SleepInsideInitThread() override
{ Stop(); }
37 void Init() override
{
38 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
41 bool InitCalled() { return init_called_
; }
47 // Thread::Init() was called.
48 THREAD_EVENT_INIT
= 0,
50 // The MessageLoop for the thread was deleted.
51 THREAD_EVENT_MESSAGE_LOOP_DESTROYED
,
53 // Thread::CleanUp() was called.
56 // Keep at end of list.
60 typedef std::vector
<ThreadEvent
> EventList
;
62 class CaptureToEventList
: public Thread
{
64 // This Thread pushes events into the vector |event_list| to show
65 // the order they occured in. |event_list| must remain valid for the
66 // lifetime of this thread.
67 explicit CaptureToEventList(EventList
* event_list
)
69 event_list_(event_list
) {
72 ~CaptureToEventList() override
{ Stop(); }
74 void Init() override
{ event_list_
->push_back(THREAD_EVENT_INIT
); }
76 void CleanUp() override
{ event_list_
->push_back(THREAD_EVENT_CLEANUP
); }
79 EventList
* event_list_
;
82 // Observer that writes a value into |event_list| when a message loop has been
84 class CapturingDestructionObserver
85 : public base::MessageLoop::DestructionObserver
{
87 // |event_list| must remain valid throughout the observer's lifetime.
88 explicit CapturingDestructionObserver(EventList
* event_list
)
89 : event_list_(event_list
) {
92 // DestructionObserver implementation:
93 void WillDestroyCurrentMessageLoop() override
{
94 event_list_
->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED
);
99 EventList
* event_list_
;
102 // Task that adds a destruction observer to the current message loop.
103 void RegisterDestructionObserver(
104 base::MessageLoop::DestructionObserver
* observer
) {
105 base::MessageLoop::current()->AddDestructionObserver(observer
);
110 TEST_F(ThreadTest
, Restart
) {
113 EXPECT_FALSE(a
.message_loop());
114 EXPECT_FALSE(a
.IsRunning());
115 EXPECT_TRUE(a
.Start());
116 EXPECT_TRUE(a
.message_loop());
117 EXPECT_TRUE(a
.IsRunning());
119 EXPECT_FALSE(a
.message_loop());
120 EXPECT_FALSE(a
.IsRunning());
121 EXPECT_TRUE(a
.Start());
122 EXPECT_TRUE(a
.message_loop());
123 EXPECT_TRUE(a
.IsRunning());
125 EXPECT_FALSE(a
.message_loop());
126 EXPECT_FALSE(a
.IsRunning());
128 EXPECT_FALSE(a
.message_loop());
129 EXPECT_FALSE(a
.IsRunning());
132 TEST_F(ThreadTest
, StartWithOptions_StackSize
) {
133 Thread
a("StartWithStackSize");
134 // Ensure that the thread can work with only 12 kb and still process a
136 Thread::Options options
;
137 #if defined(ADDRESS_SANITIZER) && defined(OS_MACOSX)
138 // ASan bloats the stack variables and overflows the 12 kb stack on OSX.
139 options
.stack_size
= 24*1024;
141 options
.stack_size
= 12*1024;
143 EXPECT_TRUE(a
.StartWithOptions(options
));
144 EXPECT_TRUE(a
.message_loop());
145 EXPECT_TRUE(a
.IsRunning());
147 bool was_invoked
= false;
148 a
.task_runner()->PostTask(FROM_HERE
, base::Bind(&ToggleValue
, &was_invoked
));
150 // wait for the task to run (we could use a kernel event here
151 // instead to avoid busy waiting, but this is sufficient for
152 // testing purposes).
153 for (int i
= 100; i
>= 0 && !was_invoked
; --i
) {
154 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
156 EXPECT_TRUE(was_invoked
);
159 TEST_F(ThreadTest
, TwoTasks
) {
160 bool was_invoked
= false;
162 Thread
a("TwoTasks");
163 EXPECT_TRUE(a
.Start());
164 EXPECT_TRUE(a
.message_loop());
166 // Test that all events are dispatched before the Thread object is
167 // destroyed. We do this by dispatching a sleep event before the
168 // event that will toggle our sentinel value.
169 a
.task_runner()->PostTask(
170 FROM_HERE
, base::Bind(static_cast<void (*)(base::TimeDelta
)>(
171 &base::PlatformThread::Sleep
),
172 base::TimeDelta::FromMilliseconds(20)));
173 a
.task_runner()->PostTask(FROM_HERE
,
174 base::Bind(&ToggleValue
, &was_invoked
));
176 EXPECT_TRUE(was_invoked
);
179 TEST_F(ThreadTest
, StopSoon
) {
180 Thread
a("StopSoon");
181 EXPECT_TRUE(a
.Start());
182 EXPECT_TRUE(a
.message_loop());
183 EXPECT_TRUE(a
.IsRunning());
187 EXPECT_FALSE(a
.message_loop());
188 EXPECT_FALSE(a
.IsRunning());
191 TEST_F(ThreadTest
, ThreadName
) {
192 Thread
a("ThreadName");
193 EXPECT_TRUE(a
.Start());
194 EXPECT_EQ("ThreadName", a
.thread_name());
197 // Make sure we can't use a thread between Start() and Init().
198 TEST_F(ThreadTest
, SleepInsideInit
) {
199 SleepInsideInitThread t
;
200 EXPECT_FALSE(t
.InitCalled());
202 EXPECT_TRUE(t
.InitCalled());
205 // Make sure that the destruction sequence is:
207 // (1) Thread::CleanUp()
208 // (2) MessageLoop::~MessageLoop()
209 // MessageLoop::DestructionObservers called.
210 TEST_F(ThreadTest
, CleanUp
) {
211 EventList captured_events
;
212 CapturingDestructionObserver
loop_destruction_observer(&captured_events
);
215 // Start a thread which writes its event into |captured_events|.
216 CaptureToEventList
t(&captured_events
);
217 EXPECT_TRUE(t
.Start());
218 EXPECT_TRUE(t
.message_loop());
219 EXPECT_TRUE(t
.IsRunning());
221 // Register an observer that writes into |captured_events| once the
222 // thread's message loop is destroyed.
223 t
.task_runner()->PostTask(
224 FROM_HERE
, base::Bind(&RegisterDestructionObserver
,
225 base::Unretained(&loop_destruction_observer
)));
227 // Upon leaving this scope, the thread is deleted.
230 // Check the order of events during shutdown.
231 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS
), captured_events
.size());
232 EXPECT_EQ(THREAD_EVENT_INIT
, captured_events
[0]);
233 EXPECT_EQ(THREAD_EVENT_CLEANUP
, captured_events
[1]);
234 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED
, captured_events
[2]);