Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: base/threading/thread_unittest.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/threading/thread_restrictions.cc ('k') | base/threading/watchdog.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/threading/thread.h"
6
7 #include <vector>
8
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"
15
16 using base::Thread;
17
18 typedef PlatformTest ThreadTest;
19
20 namespace {
21
22 void ToggleValue(bool* value) {
23 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean "
24 "in base/thread_unittest");
25 *value = !*value;
26 }
27
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");
34 }
35 ~SleepInsideInitThread() override { Stop(); }
36
37 void Init() override {
38 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
39 init_called_ = true;
40 }
41 bool InitCalled() { return init_called_; }
42 private:
43 bool init_called_;
44 };
45
46 enum ThreadEvent {
47 // Thread::Init() was called.
48 THREAD_EVENT_INIT = 0,
49
50 // The MessageLoop for the thread was deleted.
51 THREAD_EVENT_MESSAGE_LOOP_DESTROYED,
52
53 // Thread::CleanUp() was called.
54 THREAD_EVENT_CLEANUP,
55
56 // Keep at end of list.
57 THREAD_NUM_EVENTS
58 };
59
60 typedef std::vector<ThreadEvent> EventList;
61
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) {
70 }
71
72 ~CaptureToEventList() override { Stop(); }
73
74 void Init() override { event_list_->push_back(THREAD_EVENT_INIT); }
75
76 void CleanUp() override { event_list_->push_back(THREAD_EVENT_CLEANUP); }
77
78 private:
79 EventList* event_list_;
80 };
81
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) {
90 }
91
92 // DestructionObserver implementation:
93 void WillDestroyCurrentMessageLoop() override {
94 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED);
95 event_list_ = NULL;
96 }
97
98 private:
99 EventList* event_list_;
100 };
101
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);
106 }
107
108 } // namespace
109
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());
130 }
131
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());
146
147 bool was_invoked = false;
148 a.task_runner()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked));
149
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));
155 }
156 EXPECT_TRUE(was_invoked);
157 }
158
159 TEST_F(ThreadTest, TwoTasks) {
160 bool was_invoked = false;
161 {
162 Thread a("TwoTasks");
163 EXPECT_TRUE(a.Start());
164 EXPECT_TRUE(a.message_loop());
165
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));
175 }
176 EXPECT_TRUE(was_invoked);
177 }
178
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());
189 }
190
191 TEST_F(ThreadTest, ThreadName) {
192 Thread a("ThreadName");
193 EXPECT_TRUE(a.Start());
194 EXPECT_EQ("ThreadName", a.thread_name());
195 }
196
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());
204 }
205
206 // Make sure that the destruction sequence is:
207 //
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);
214
215 {
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());
221
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)));
227
228 // Upon leaving this scope, the thread is deleted.
229 }
230
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]);
236 }
237
238 TEST_F(ThreadTest, ThreadNotStarted) {
239 Thread a("Inert");
240 EXPECT_EQ(nullptr, a.task_runner());
241 }
OLDNEW
« no previous file with comments | « base/threading/thread_restrictions.cc ('k') | base/threading/watchdog.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698