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 Init() is called after Start() and before
198 // WaitUntilThreadInitialized() returns.
199 TEST_F(ThreadTest
, SleepInsideInit
) {
200 SleepInsideInitThread t
;
201 EXPECT_FALSE(t
.InitCalled());
202 t
.StartAndWaitForTesting();
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
.task_runner()->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]);
238 TEST_F(ThreadTest
, ThreadNotStarted
) {
240 EXPECT_EQ(nullptr, a
.task_runner());