[email protected] | e7b3a61 | 2012-01-05 02:18:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 5 | #include "base/threading/thread.h" |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 6 | |
| 7 | #include <vector> |
| 8 | |
[email protected] | 6e238ba | 2011-11-23 20:45:43 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
[email protected] | ee85751 | 2010-05-14 08:24:42 | [diff] [blame] | 11 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 13 | #include "testing/platform_test.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 15 | using base::Thread; |
| 16 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 17 | typedef PlatformTest ThreadTest; |
| 18 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | |
[email protected] | 6e238ba | 2011-11-23 20:45:43 | [diff] [blame] | 21 | void ToggleValue(bool* value) { |
| 22 | ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean " |
| 23 | "in base/thread_unittest"); |
| 24 | *value = !*value; |
| 25 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 27 | class SleepInsideInitThread : public Thread { |
| 28 | public: |
[email protected] | ded6e7e | 2011-09-27 14:11:44 | [diff] [blame] | 29 | SleepInsideInitThread() : Thread("none") { |
| 30 | init_called_ = false; |
| 31 | ANNOTATE_BENIGN_RACE( |
| 32 | this, "Benign test-only data race on vptr - http://crbug.com/98219"); |
| 33 | } |
[email protected] | d583c3a | 2011-11-02 15:31:56 | [diff] [blame] | 34 | virtual ~SleepInsideInitThread() { |
| 35 | Stop(); |
| 36 | } |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 37 | |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 38 | virtual void Init() OVERRIDE { |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 39 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 40 | init_called_ = true; |
| 41 | } |
| 42 | bool InitCalled() { return init_called_; } |
| 43 | private: |
| 44 | bool init_called_; |
| 45 | }; |
| 46 | |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 47 | enum ThreadEvent { |
| 48 | // Thread::Init() was called. |
[email protected] | 569c760 | 2011-03-03 20:40:32 | [diff] [blame] | 49 | THREAD_EVENT_INIT = 0, |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 50 | |
| 51 | // The MessageLoop for the thread was deleted. |
| 52 | THREAD_EVENT_MESSAGE_LOOP_DESTROYED, |
| 53 | |
| 54 | // Thread::CleanUp() was called. |
| 55 | THREAD_EVENT_CLEANUP, |
| 56 | |
[email protected] | 569c760 | 2011-03-03 20:40:32 | [diff] [blame] | 57 | // Keep at end of list. |
| 58 | THREAD_NUM_EVENTS |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | typedef std::vector<ThreadEvent> EventList; |
| 62 | |
| 63 | class CaptureToEventList : public Thread { |
| 64 | public: |
| 65 | // This Thread pushes events into the vector |event_list| to show |
| 66 | // the order they occured in. |event_list| must remain valid for the |
| 67 | // lifetime of this thread. |
| 68 | explicit CaptureToEventList(EventList* event_list) |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 69 | : Thread("none"), |
| 70 | event_list_(event_list) { |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | virtual ~CaptureToEventList() { |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 74 | Stop(); |
| 75 | } |
| 76 | |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 77 | virtual void Init() OVERRIDE { |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 78 | event_list_->push_back(THREAD_EVENT_INIT); |
| 79 | } |
| 80 | |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 81 | virtual void CleanUp() OVERRIDE { |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 82 | event_list_->push_back(THREAD_EVENT_CLEANUP); |
| 83 | } |
| 84 | |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 85 | private: |
| 86 | EventList* event_list_; |
| 87 | }; |
| 88 | |
| 89 | // Observer that writes a value into |event_list| when a message loop has been |
| 90 | // destroyed. |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 91 | class CapturingDestructionObserver |
| 92 | : public base::MessageLoop::DestructionObserver { |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 93 | public: |
| 94 | // |event_list| must remain valid throughout the observer's lifetime. |
| 95 | explicit CapturingDestructionObserver(EventList* event_list) |
| 96 | : event_list_(event_list) { |
| 97 | } |
| 98 | |
| 99 | // DestructionObserver implementation: |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 100 | virtual void WillDestroyCurrentMessageLoop() OVERRIDE { |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 101 | event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); |
| 102 | event_list_ = NULL; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | EventList* event_list_; |
| 107 | }; |
| 108 | |
| 109 | // Task that adds a destruction observer to the current message loop. |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 110 | void RegisterDestructionObserver( |
| 111 | base::MessageLoop::DestructionObserver* observer) { |
| 112 | base::MessageLoop::current()->AddDestructionObserver(observer); |
[email protected] | e7b3a61 | 2012-01-05 02:18:18 | [diff] [blame] | 113 | } |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 114 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 115 | } // namespace |
| 116 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 117 | TEST_F(ThreadTest, Restart) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 118 | Thread a("Restart"); |
| 119 | a.Stop(); |
| 120 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 121 | EXPECT_FALSE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 122 | EXPECT_TRUE(a.Start()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 124 | EXPECT_TRUE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | a.Stop(); |
| 126 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 127 | EXPECT_FALSE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 128 | EXPECT_TRUE(a.Start()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 130 | EXPECT_TRUE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | a.Stop(); |
| 132 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 133 | EXPECT_FALSE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 134 | a.Stop(); |
| 135 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 136 | EXPECT_FALSE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 139 | TEST_F(ThreadTest, StartWithOptions_StackSize) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 140 | Thread a("StartWithStackSize"); |
| 141 | // Ensure that the thread can work with only 12 kb and still process a |
| 142 | // message. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 143 | Thread::Options options; |
| 144 | options.stack_size = 12*1024; |
| 145 | EXPECT_TRUE(a.StartWithOptions(options)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 146 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 147 | EXPECT_TRUE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | |
| 149 | bool was_invoked = false; |
[email protected] | 6e238ba | 2011-11-23 20:45:43 | [diff] [blame] | 150 | a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | |
| 152 | // wait for the task to run (we could use a kernel event here |
| 153 | // instead to avoid busy waiting, but this is sufficient for |
| 154 | // testing purposes). |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 155 | for (int i = 100; i >= 0 && !was_invoked; --i) { |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 156 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 157 | } |
| 158 | EXPECT_TRUE(was_invoked); |
| 159 | } |
| 160 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 161 | TEST_F(ThreadTest, TwoTasks) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | bool was_invoked = false; |
| 163 | { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 164 | Thread a("TwoTasks"); |
| 165 | EXPECT_TRUE(a.Start()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | EXPECT_TRUE(a.message_loop()); |
| 167 | |
| 168 | // Test that all events are dispatched before the Thread object is |
| 169 | // destroyed. We do this by dispatching a sleep event before the |
| 170 | // event that will toggle our sentinel value. |
[email protected] | 76784292 | 2011-12-30 01:04:36 | [diff] [blame] | 171 | a.message_loop()->PostTask( |
| 172 | FROM_HERE, |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 173 | base::Bind( |
| 174 | static_cast<void (*)(base::TimeDelta)>( |
| 175 | &base::PlatformThread::Sleep), |
| 176 | base::TimeDelta::FromMilliseconds(20))); |
[email protected] | 6e238ba | 2011-11-23 20:45:43 | [diff] [blame] | 177 | a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, |
| 178 | &was_invoked)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | } |
| 180 | EXPECT_TRUE(was_invoked); |
| 181 | } |
| 182 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 183 | TEST_F(ThreadTest, StopSoon) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 184 | Thread a("StopSoon"); |
| 185 | EXPECT_TRUE(a.Start()); |
| 186 | EXPECT_TRUE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 187 | EXPECT_TRUE(a.IsRunning()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 188 | a.StopSoon(); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 189 | a.StopSoon(); |
[email protected] | 98c74f8 | 2008-12-01 14:34:42 | [diff] [blame] | 190 | a.Stop(); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 191 | EXPECT_FALSE(a.message_loop()); |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 192 | EXPECT_FALSE(a.IsRunning()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 6e683db | 2008-08-28 01:17:02 | [diff] [blame] | 195 | TEST_F(ThreadTest, ThreadName) { |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 196 | Thread a("ThreadName"); |
| 197 | EXPECT_TRUE(a.Start()); |
| 198 | EXPECT_EQ("ThreadName", a.thread_name()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | } |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 200 | |
| 201 | // Make sure we can't use a thread between Start() and Init(). |
| 202 | TEST_F(ThreadTest, SleepInsideInit) { |
| 203 | SleepInsideInitThread t; |
| 204 | EXPECT_FALSE(t.InitCalled()); |
| 205 | t.Start(); |
| 206 | EXPECT_TRUE(t.InitCalled()); |
| 207 | } |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 208 | |
| 209 | // Make sure that the destruction sequence is: |
| 210 | // |
| 211 | // (1) Thread::CleanUp() |
| 212 | // (2) MessageLoop::~MessageLoop() |
| 213 | // MessageLoop::DestructionObservers called. |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 214 | TEST_F(ThreadTest, CleanUp) { |
| 215 | EventList captured_events; |
| 216 | CapturingDestructionObserver loop_destruction_observer(&captured_events); |
| 217 | |
| 218 | { |
| 219 | // Start a thread which writes its event into |captured_events|. |
| 220 | CaptureToEventList t(&captured_events); |
| 221 | EXPECT_TRUE(t.Start()); |
| 222 | EXPECT_TRUE(t.message_loop()); |
| 223 | EXPECT_TRUE(t.IsRunning()); |
| 224 | |
| 225 | // Register an observer that writes into |captured_events| once the |
| 226 | // thread's message loop is destroyed. |
| 227 | t.message_loop()->PostTask( |
[email protected] | e7b3a61 | 2012-01-05 02:18:18 | [diff] [blame] | 228 | FROM_HERE, base::Bind(&RegisterDestructionObserver, |
| 229 | base::Unretained(&loop_destruction_observer))); |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 230 | |
| 231 | // Upon leaving this scope, the thread is deleted. |
| 232 | } |
| 233 | |
| 234 | // Check the order of events during shutdown. |
[email protected] | 569c760 | 2011-03-03 20:40:32 | [diff] [blame] | 235 | ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 236 | EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
| 237 | EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
| 238 | EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
[email protected] | f93ff3f | 2010-04-14 20:41:34 | [diff] [blame] | 239 | } |