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/message_loop/message_loop.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
17 typedef PlatformTest ThreadTest
;
21 void ToggleValue(bool* value
) {
22 ANNOTATE_BENIGN_RACE(value
, "Test-only data race on boolean "
23 "in base/thread_unittest");
27 class SleepInsideInitThread
: public Thread
{
29 SleepInsideInitThread() : Thread("none") {
32 this, "Benign test-only data race on vptr - http://crbug.com/98219");
34 ~SleepInsideInitThread() override
{ Stop(); }
36 void Init() override
{
37 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
40 bool InitCalled() { return init_called_
; }
46 // Thread::Init() was called.
47 THREAD_EVENT_INIT
= 0,
49 // The MessageLoop for the thread was deleted.
50 THREAD_EVENT_MESSAGE_LOOP_DESTROYED
,
52 // Thread::CleanUp() was called.
55 // Keep at end of list.
59 typedef std::vector
<ThreadEvent
> EventList
;
61 class CaptureToEventList
: public Thread
{
63 // This Thread pushes events into the vector |event_list| to show
64 // the order they occured in. |event_list| must remain valid for the
65 // lifetime of this thread.
66 explicit CaptureToEventList(EventList
* event_list
)
68 event_list_(event_list
) {
71 ~CaptureToEventList() override
{ Stop(); }
73 void Init() override
{ event_list_
->push_back(THREAD_EVENT_INIT
); }
75 void CleanUp() override
{ event_list_
->push_back(THREAD_EVENT_CLEANUP
); }
78 EventList
* event_list_
;
81 // Observer that writes a value into |event_list| when a message loop has been
83 class CapturingDestructionObserver
84 : public base::MessageLoop::DestructionObserver
{
86 // |event_list| must remain valid throughout the observer's lifetime.
87 explicit CapturingDestructionObserver(EventList
* event_list
)
88 : event_list_(event_list
) {
91 // DestructionObserver implementation:
92 void WillDestroyCurrentMessageLoop() override
{
93 event_list_
->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED
);
98 EventList
* event_list_
;
101 // Task that adds a destruction observer to the current message loop.
102 void RegisterDestructionObserver(
103 base::MessageLoop::DestructionObserver
* observer
) {
104 base::MessageLoop::current()->AddDestructionObserver(observer
);
109 TEST_F(ThreadTest
, Restart
) {
112 EXPECT_FALSE(a
.message_loop());
113 EXPECT_FALSE(a
.IsRunning());
114 EXPECT_TRUE(a
.Start());
115 EXPECT_TRUE(a
.message_loop());
116 EXPECT_TRUE(a
.IsRunning());
118 EXPECT_FALSE(a
.message_loop());
119 EXPECT_FALSE(a
.IsRunning());
120 EXPECT_TRUE(a
.Start());
121 EXPECT_TRUE(a
.message_loop());
122 EXPECT_TRUE(a
.IsRunning());
124 EXPECT_FALSE(a
.message_loop());
125 EXPECT_FALSE(a
.IsRunning());
127 EXPECT_FALSE(a
.message_loop());
128 EXPECT_FALSE(a
.IsRunning());
131 TEST_F(ThreadTest
, StartWithOptions_StackSize
) {
132 Thread
a("StartWithStackSize");
133 // Ensure that the thread can work with only 12 kb and still process a
135 Thread::Options options
;
136 #if defined(ADDRESS_SANITIZER) && defined(OS_MACOSX)
137 // ASan bloats the stack variables and overflows the 12 kb stack on OSX.
138 options
.stack_size
= 24*1024;
140 options
.stack_size
= 12*1024;
142 EXPECT_TRUE(a
.StartWithOptions(options
));
143 EXPECT_TRUE(a
.message_loop());
144 EXPECT_TRUE(a
.IsRunning());
146 bool was_invoked
= false;
147 a
.message_loop()->PostTask(FROM_HERE
, base::Bind(&ToggleValue
, &was_invoked
));
149 // wait for the task to run (we could use a kernel event here
150 // instead to avoid busy waiting, but this is sufficient for
151 // testing purposes).
152 for (int i
= 100; i
>= 0 && !was_invoked
; --i
) {
153 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
155 EXPECT_TRUE(was_invoked
);
158 TEST_F(ThreadTest
, TwoTasks
) {
159 bool was_invoked
= false;
161 Thread
a("TwoTasks");
162 EXPECT_TRUE(a
.Start());
163 EXPECT_TRUE(a
.message_loop());
165 // Test that all events are dispatched before the Thread object is
166 // destroyed. We do this by dispatching a sleep event before the
167 // event that will toggle our sentinel value.
168 a
.message_loop()->PostTask(
171 static_cast<void (*)(base::TimeDelta
)>(
172 &base::PlatformThread::Sleep
),
173 base::TimeDelta::FromMilliseconds(20)));
174 a
.message_loop()->PostTask(FROM_HERE
, base::Bind(&ToggleValue
,
177 EXPECT_TRUE(was_invoked
);
180 TEST_F(ThreadTest
, StopSoon
) {
181 Thread
a("StopSoon");
182 EXPECT_TRUE(a
.Start());
183 EXPECT_TRUE(a
.message_loop());
184 EXPECT_TRUE(a
.IsRunning());
188 EXPECT_FALSE(a
.message_loop());
189 EXPECT_FALSE(a
.IsRunning());
192 TEST_F(ThreadTest
, ThreadName
) {
193 Thread
a("ThreadName");
194 EXPECT_TRUE(a
.Start());
195 EXPECT_EQ("ThreadName", a
.thread_name());
198 // Make sure we can't use a thread between Start() and Init().
199 TEST_F(ThreadTest
, SleepInsideInit
) {
200 SleepInsideInitThread t
;
201 EXPECT_FALSE(t
.InitCalled());
203 EXPECT_TRUE(t
.InitCalled());
206 // Make sure that the destruction sequence is:
208 // (1) Thread::CleanUp()
209 // (2) MessageLoop::~MessageLoop()
210 // MessageLoop::DestructionObservers called.
211 TEST_F(ThreadTest
, CleanUp
) {
212 EventList captured_events
;
213 CapturingDestructionObserver
loop_destruction_observer(&captured_events
);
216 // Start a thread which writes its event into |captured_events|.
217 CaptureToEventList
t(&captured_events
);
218 EXPECT_TRUE(t
.Start());
219 EXPECT_TRUE(t
.message_loop());
220 EXPECT_TRUE(t
.IsRunning());
222 // Register an observer that writes into |captured_events| once the
223 // thread's message loop is destroyed.
224 t
.message_loop()->PostTask(
225 FROM_HERE
, base::Bind(&RegisterDestructionObserver
,
226 base::Unretained(&loop_destruction_observer
)));
228 // Upon leaving this scope, the thread is deleted.
231 // Check the order of events during shutdown.
232 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS
), captured_events
.size());
233 EXPECT_EQ(THREAD_EVENT_INIT
, captured_events
[0]);
234 EXPECT_EQ(THREAD_EVENT_CLEANUP
, captured_events
[1]);
235 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED
, captured_events
[2]);