Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / base / threading / thread_unittest.cc
blob3c3541657ebffbe5c2b0cbd5e37340509764598e
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"
7 #include <vector>
9 #include "base/bind.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"
16 using base::Thread;
18 typedef PlatformTest ThreadTest;
20 namespace {
22 void ToggleValue(bool* value) {
23 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean "
24 "in base/thread_unittest");
25 *value = !*value;
28 class SleepInsideInitThread : public Thread {
29 public:
30 SleepInsideInitThread() : Thread("none") {
31 init_called_ = false;
32 ANNOTATE_BENIGN_RACE(
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));
39 init_called_ = true;
41 bool InitCalled() { return init_called_; }
42 private:
43 bool init_called_;
46 enum ThreadEvent {
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.
54 THREAD_EVENT_CLEANUP,
56 // Keep at end of list.
57 THREAD_NUM_EVENTS
60 typedef std::vector<ThreadEvent> EventList;
62 class CaptureToEventList : public Thread {
63 public:
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)
68 : Thread("none"),
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); }
78 private:
79 EventList* event_list_;
82 // Observer that writes a value into |event_list| when a message loop has been
83 // destroyed.
84 class CapturingDestructionObserver
85 : public base::MessageLoop::DestructionObserver {
86 public:
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);
95 event_list_ = NULL;
98 private:
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);
108 } // namespace
110 TEST_F(ThreadTest, Restart) {
111 Thread a("Restart");
112 a.Stop();
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());
118 a.Stop();
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());
124 a.Stop();
125 EXPECT_FALSE(a.message_loop());
126 EXPECT_FALSE(a.IsRunning());
127 a.Stop();
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
135 // message.
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;
140 #else
141 options.stack_size = 12*1024;
142 #endif
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());
184 a.StopSoon();
185 a.StopSoon();
186 a.Stop();
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) {
239 Thread a("Inert");
240 EXPECT_EQ(nullptr, a.task_runner());