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 options
.stack_size
= 12*1024;
137 EXPECT_TRUE(a
.StartWithOptions(options
));
138 EXPECT_TRUE(a
.message_loop());
139 EXPECT_TRUE(a
.IsRunning());
141 bool was_invoked
= false;
142 a
.message_loop()->PostTask(FROM_HERE
, base::Bind(&ToggleValue
, &was_invoked
));
144 // wait for the task to run (we could use a kernel event here
145 // instead to avoid busy waiting, but this is sufficient for
146 // testing purposes).
147 for (int i
= 100; i
>= 0 && !was_invoked
; --i
) {
148 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
150 EXPECT_TRUE(was_invoked
);
153 TEST_F(ThreadTest
, TwoTasks
) {
154 bool was_invoked
= false;
156 Thread
a("TwoTasks");
157 EXPECT_TRUE(a
.Start());
158 EXPECT_TRUE(a
.message_loop());
160 // Test that all events are dispatched before the Thread object is
161 // destroyed. We do this by dispatching a sleep event before the
162 // event that will toggle our sentinel value.
163 a
.message_loop()->PostTask(
166 static_cast<void (*)(base::TimeDelta
)>(
167 &base::PlatformThread::Sleep
),
168 base::TimeDelta::FromMilliseconds(20)));
169 a
.message_loop()->PostTask(FROM_HERE
, base::Bind(&ToggleValue
,
172 EXPECT_TRUE(was_invoked
);
175 TEST_F(ThreadTest
, StopSoon
) {
176 Thread
a("StopSoon");
177 EXPECT_TRUE(a
.Start());
178 EXPECT_TRUE(a
.message_loop());
179 EXPECT_TRUE(a
.IsRunning());
183 EXPECT_FALSE(a
.message_loop());
184 EXPECT_FALSE(a
.IsRunning());
187 TEST_F(ThreadTest
, ThreadName
) {
188 Thread
a("ThreadName");
189 EXPECT_TRUE(a
.Start());
190 EXPECT_EQ("ThreadName", a
.thread_name());
193 // Make sure we can't use a thread between Start() and Init().
194 TEST_F(ThreadTest
, SleepInsideInit
) {
195 SleepInsideInitThread t
;
196 EXPECT_FALSE(t
.InitCalled());
198 EXPECT_TRUE(t
.InitCalled());
201 // Make sure that the destruction sequence is:
203 // (1) Thread::CleanUp()
204 // (2) MessageLoop::~MessageLoop()
205 // MessageLoop::DestructionObservers called.
206 TEST_F(ThreadTest
, CleanUp
) {
207 EventList captured_events
;
208 CapturingDestructionObserver
loop_destruction_observer(&captured_events
);
211 // Start a thread which writes its event into |captured_events|.
212 CaptureToEventList
t(&captured_events
);
213 EXPECT_TRUE(t
.Start());
214 EXPECT_TRUE(t
.message_loop());
215 EXPECT_TRUE(t
.IsRunning());
217 // Register an observer that writes into |captured_events| once the
218 // thread's message loop is destroyed.
219 t
.message_loop()->PostTask(
220 FROM_HERE
, base::Bind(&RegisterDestructionObserver
,
221 base::Unretained(&loop_destruction_observer
)));
223 // Upon leaving this scope, the thread is deleted.
226 // Check the order of events during shutdown.
227 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS
), captured_events
.size());
228 EXPECT_EQ(THREAD_EVENT_INIT
, captured_events
[0]);
229 EXPECT_EQ(THREAD_EVENT_CLEANUP
, captured_events
[1]);
230 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED
, captured_events
[2]);