WebKit Roll 61390:61491.
[chromium-blink-merge.git] / base / thread_unittest.cc
blob696cfc2a99494cdd37bab9366276958e808434fd
1 // Copyright (c) 2006-2008 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/thread.h"
7 #include <vector>
9 #include "base/lock.h"
10 #include "base/message_loop.h"
11 #include "base/string_util.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"
16 using base::Thread;
18 typedef PlatformTest ThreadTest;
20 namespace {
22 class ToggleValue : public Task {
23 public:
24 explicit ToggleValue(bool* value) : value_(value) {
25 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean "
26 "in base/thread_unittest");
28 virtual void Run() {
29 *value_ = !*value_;
31 private:
32 bool* value_;
35 class SleepSome : public Task {
36 public:
37 explicit SleepSome(int msec) : msec_(msec) {
39 virtual void Run() {
40 PlatformThread::Sleep(msec_);
42 private:
43 int msec_;
46 class SleepInsideInitThread : public Thread {
47 public:
48 SleepInsideInitThread() : Thread("none") { init_called_ = false; }
49 virtual ~SleepInsideInitThread() { }
51 virtual void Init() {
52 PlatformThread::Sleep(500);
53 init_called_ = true;
55 bool InitCalled() { return init_called_; }
56 private:
57 bool init_called_;
60 enum ThreadEvent {
61 // Thread::Init() was called.
62 THREAD_EVENT_INIT,
64 // The MessageLoop for the thread was deleted.
65 THREAD_EVENT_MESSAGE_LOOP_DESTROYED,
67 // Thread::CleanUp() was called.
68 THREAD_EVENT_CLEANUP,
70 // Thread::CleanUpAfterMessageLoopDestruction() was called.
71 THREAD_EVENT_CLEANUP_AFTER_LOOP,
74 typedef std::vector<ThreadEvent> EventList;
76 class CaptureToEventList : public Thread {
77 public:
78 // This Thread pushes events into the vector |event_list| to show
79 // the order they occured in. |event_list| must remain valid for the
80 // lifetime of this thread.
81 explicit CaptureToEventList(EventList* event_list)
82 : Thread("none"), event_list_(event_list) {
85 virtual ~CaptureToEventList() {
86 // Must call Stop() manually to have our CleanUp() function called.
87 Stop();
90 virtual void Init() {
91 event_list_->push_back(THREAD_EVENT_INIT);
94 virtual void CleanUp() {
95 event_list_->push_back(THREAD_EVENT_CLEANUP);
98 virtual void CleanUpAfterMessageLoopDestruction() {
99 event_list_->push_back(THREAD_EVENT_CLEANUP_AFTER_LOOP);
102 private:
103 EventList* event_list_;
106 // Observer that writes a value into |event_list| when a message loop has been
107 // destroyed.
108 class CapturingDestructionObserver : public MessageLoop::DestructionObserver {
109 public:
110 // |event_list| must remain valid throughout the observer's lifetime.
111 explicit CapturingDestructionObserver(EventList* event_list)
112 : event_list_(event_list) {
115 // DestructionObserver implementation:
116 virtual void WillDestroyCurrentMessageLoop() {
117 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED);
118 event_list_ = NULL;
121 private:
122 EventList* event_list_;
125 // Task that adds a destruction observer to the current message loop.
126 class RegisterDestructionObserver : public Task {
127 public:
128 explicit RegisterDestructionObserver(
129 MessageLoop::DestructionObserver* observer)
130 : observer_(observer) {
133 virtual void Run() {
134 MessageLoop::current()->AddDestructionObserver(observer_);
135 observer_ = NULL;
138 private:
139 MessageLoop::DestructionObserver* observer_;
142 } // namespace
144 TEST_F(ThreadTest, Restart) {
145 Thread a("Restart");
146 a.Stop();
147 EXPECT_FALSE(a.message_loop());
148 EXPECT_FALSE(a.IsRunning());
149 EXPECT_TRUE(a.Start());
150 EXPECT_TRUE(a.message_loop());
151 EXPECT_TRUE(a.IsRunning());
152 a.Stop();
153 EXPECT_FALSE(a.message_loop());
154 EXPECT_FALSE(a.IsRunning());
155 EXPECT_TRUE(a.Start());
156 EXPECT_TRUE(a.message_loop());
157 EXPECT_TRUE(a.IsRunning());
158 a.Stop();
159 EXPECT_FALSE(a.message_loop());
160 EXPECT_FALSE(a.IsRunning());
161 a.Stop();
162 EXPECT_FALSE(a.message_loop());
163 EXPECT_FALSE(a.IsRunning());
166 TEST_F(ThreadTest, StartWithOptions_StackSize) {
167 Thread a("StartWithStackSize");
168 // Ensure that the thread can work with only 12 kb and still process a
169 // message.
170 Thread::Options options;
171 options.stack_size = 12*1024;
172 EXPECT_TRUE(a.StartWithOptions(options));
173 EXPECT_TRUE(a.message_loop());
174 EXPECT_TRUE(a.IsRunning());
176 bool was_invoked = false;
177 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
179 // wait for the task to run (we could use a kernel event here
180 // instead to avoid busy waiting, but this is sufficient for
181 // testing purposes).
182 for (int i = 100; i >= 0 && !was_invoked; --i) {
183 PlatformThread::Sleep(10);
185 EXPECT_TRUE(was_invoked);
188 TEST_F(ThreadTest, TwoTasks) {
189 bool was_invoked = false;
191 Thread a("TwoTasks");
192 EXPECT_TRUE(a.Start());
193 EXPECT_TRUE(a.message_loop());
195 // Test that all events are dispatched before the Thread object is
196 // destroyed. We do this by dispatching a sleep event before the
197 // event that will toggle our sentinel value.
198 a.message_loop()->PostTask(FROM_HERE, new SleepSome(20));
199 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
201 EXPECT_TRUE(was_invoked);
204 TEST_F(ThreadTest, StopSoon) {
205 Thread a("StopSoon");
206 EXPECT_TRUE(a.Start());
207 EXPECT_TRUE(a.message_loop());
208 EXPECT_TRUE(a.IsRunning());
209 a.StopSoon();
210 a.StopSoon();
211 a.Stop();
212 EXPECT_FALSE(a.message_loop());
213 EXPECT_FALSE(a.IsRunning());
216 TEST_F(ThreadTest, ThreadName) {
217 Thread a("ThreadName");
218 EXPECT_TRUE(a.Start());
219 EXPECT_EQ("ThreadName", a.thread_name());
222 // Make sure we can't use a thread between Start() and Init().
223 TEST_F(ThreadTest, SleepInsideInit) {
224 SleepInsideInitThread t;
225 EXPECT_FALSE(t.InitCalled());
226 t.Start();
227 EXPECT_TRUE(t.InitCalled());
230 // Make sure that the destruction sequence is:
232 // (1) Thread::CleanUp()
233 // (2) MessageLoop::~MessageLoop()
234 // MessageLoop::DestructionObservers called.
235 // (3) Thread::CleanUpAfterMessageLoopDestruction
236 TEST_F(ThreadTest, CleanUp) {
237 EventList captured_events;
238 CapturingDestructionObserver loop_destruction_observer(&captured_events);
241 // Start a thread which writes its event into |captured_events|.
242 CaptureToEventList t(&captured_events);
243 EXPECT_TRUE(t.Start());
244 EXPECT_TRUE(t.message_loop());
245 EXPECT_TRUE(t.IsRunning());
247 // Register an observer that writes into |captured_events| once the
248 // thread's message loop is destroyed.
249 t.message_loop()->PostTask(
250 FROM_HERE,
251 new RegisterDestructionObserver(&loop_destruction_observer));
253 // Upon leaving this scope, the thread is deleted.
256 // Check the order of events during shutdown.
257 ASSERT_EQ(4u, captured_events.size());
258 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]);
259 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]);
260 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]);
261 EXPECT_EQ(THREAD_EVENT_CLEANUP_AFTER_LOOP, captured_events[3]);