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

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

Issue 1100773004: base: Remove most uses of MessageLoopProxy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/thread.h" 5 #include "base/threading/thread.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h" 14 #include "testing/platform_test.h"
14 15
15 using base::Thread; 16 using base::Thread;
16 17
17 typedef PlatformTest ThreadTest; 18 typedef PlatformTest ThreadTest;
18 19
19 namespace { 20 namespace {
20 21
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 // ASan bloats the stack variables and overflows the 12 kb stack on OSX. 138 // ASan bloats the stack variables and overflows the 12 kb stack on OSX.
138 options.stack_size = 24*1024; 139 options.stack_size = 24*1024;
139 #else 140 #else
140 options.stack_size = 12*1024; 141 options.stack_size = 12*1024;
141 #endif 142 #endif
142 EXPECT_TRUE(a.StartWithOptions(options)); 143 EXPECT_TRUE(a.StartWithOptions(options));
143 EXPECT_TRUE(a.message_loop()); 144 EXPECT_TRUE(a.message_loop());
144 EXPECT_TRUE(a.IsRunning()); 145 EXPECT_TRUE(a.IsRunning());
145 146
146 bool was_invoked = false; 147 bool was_invoked = false;
147 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked)); 148 a.task_runner()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked));
148 149
149 // wait for the task to run (we could use a kernel event here 150 // wait for the task to run (we could use a kernel event here
150 // instead to avoid busy waiting, but this is sufficient for 151 // instead to avoid busy waiting, but this is sufficient for
151 // testing purposes). 152 // testing purposes).
152 for (int i = 100; i >= 0 && !was_invoked; --i) { 153 for (int i = 100; i >= 0 && !was_invoked; --i) {
153 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 154 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
154 } 155 }
155 EXPECT_TRUE(was_invoked); 156 EXPECT_TRUE(was_invoked);
156 } 157 }
157 158
158 TEST_F(ThreadTest, TwoTasks) { 159 TEST_F(ThreadTest, TwoTasks) {
159 bool was_invoked = false; 160 bool was_invoked = false;
160 { 161 {
161 Thread a("TwoTasks"); 162 Thread a("TwoTasks");
162 EXPECT_TRUE(a.Start()); 163 EXPECT_TRUE(a.Start());
163 EXPECT_TRUE(a.message_loop()); 164 EXPECT_TRUE(a.message_loop());
164 165
165 // Test that all events are dispatched before the Thread object is 166 // Test that all events are dispatched before the Thread object is
166 // destroyed. We do this by dispatching a sleep event before the 167 // destroyed. We do this by dispatching a sleep event before the
167 // event that will toggle our sentinel value. 168 // event that will toggle our sentinel value.
168 a.message_loop()->PostTask( 169 a.task_runner()->PostTask(
169 FROM_HERE, 170 FROM_HERE, base::Bind(static_cast<void (*)(base::TimeDelta)>(
170 base::Bind( 171 &base::PlatformThread::Sleep),
171 static_cast<void (*)(base::TimeDelta)>( 172 base::TimeDelta::FromMilliseconds(20)));
172 &base::PlatformThread::Sleep), 173 a.task_runner()->PostTask(FROM_HERE,
173 base::TimeDelta::FromMilliseconds(20))); 174 base::Bind(&ToggleValue, &was_invoked));
174 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue,
175 &was_invoked));
176 } 175 }
177 EXPECT_TRUE(was_invoked); 176 EXPECT_TRUE(was_invoked);
178 } 177 }
179 178
180 TEST_F(ThreadTest, StopSoon) { 179 TEST_F(ThreadTest, StopSoon) {
181 Thread a("StopSoon"); 180 Thread a("StopSoon");
182 EXPECT_TRUE(a.Start()); 181 EXPECT_TRUE(a.Start());
183 EXPECT_TRUE(a.message_loop()); 182 EXPECT_TRUE(a.message_loop());
184 EXPECT_TRUE(a.IsRunning()); 183 EXPECT_TRUE(a.IsRunning());
185 a.StopSoon(); 184 a.StopSoon();
(...skipping 28 matching lines...) Expand all
214 213
215 { 214 {
216 // Start a thread which writes its event into |captured_events|. 215 // Start a thread which writes its event into |captured_events|.
217 CaptureToEventList t(&captured_events); 216 CaptureToEventList t(&captured_events);
218 EXPECT_TRUE(t.Start()); 217 EXPECT_TRUE(t.Start());
219 EXPECT_TRUE(t.message_loop()); 218 EXPECT_TRUE(t.message_loop());
220 EXPECT_TRUE(t.IsRunning()); 219 EXPECT_TRUE(t.IsRunning());
221 220
222 // Register an observer that writes into |captured_events| once the 221 // Register an observer that writes into |captured_events| once the
223 // thread's message loop is destroyed. 222 // thread's message loop is destroyed.
224 t.message_loop()->PostTask( 223 t.task_runner()->PostTask(
225 FROM_HERE, base::Bind(&RegisterDestructionObserver, 224 FROM_HERE, base::Bind(&RegisterDestructionObserver,
226 base::Unretained(&loop_destruction_observer))); 225 base::Unretained(&loop_destruction_observer)));
227 226
228 // Upon leaving this scope, the thread is deleted. 227 // Upon leaving this scope, the thread is deleted.
229 } 228 }
230 229
231 // Check the order of events during shutdown. 230 // Check the order of events during shutdown.
232 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); 231 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size());
233 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); 232 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]);
234 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); 233 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]);
235 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); 234 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]);
236 } 235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698