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

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

Issue 9055001: Change code in base (primarily unit tests) to use Sleep(TimeDelta). (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Qualify windows Sleep calls to go through PlatformThread. Created 8 years, 11 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_collision_warner_unittest.cc ('k') | base/threading/watchdog_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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.h" 10 #include "base/message_loop.h"
(...skipping 18 matching lines...) Expand all
29 SleepInsideInitThread() : Thread("none") { 29 SleepInsideInitThread() : Thread("none") {
30 init_called_ = false; 30 init_called_ = false;
31 ANNOTATE_BENIGN_RACE( 31 ANNOTATE_BENIGN_RACE(
32 this, "Benign test-only data race on vptr - http://crbug.com/98219"); 32 this, "Benign test-only data race on vptr - http://crbug.com/98219");
33 } 33 }
34 virtual ~SleepInsideInitThread() { 34 virtual ~SleepInsideInitThread() {
35 Stop(); 35 Stop();
36 } 36 }
37 37
38 virtual void Init() { 38 virtual void Init() {
39 base::PlatformThread::Sleep(500); 39 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
40 init_called_ = true; 40 init_called_ = true;
41 } 41 }
42 bool InitCalled() { return init_called_; } 42 bool InitCalled() { return init_called_; }
43 private: 43 private:
44 bool init_called_; 44 bool init_called_;
45 }; 45 };
46 46
47 enum ThreadEvent { 47 enum ThreadEvent {
48 // Thread::Init() was called. 48 // Thread::Init() was called.
49 THREAD_EVENT_INIT = 0, 49 THREAD_EVENT_INIT = 0,
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 EXPECT_TRUE(a.message_loop()); 155 EXPECT_TRUE(a.message_loop());
156 EXPECT_TRUE(a.IsRunning()); 156 EXPECT_TRUE(a.IsRunning());
157 157
158 bool was_invoked = false; 158 bool was_invoked = false;
159 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked)); 159 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked));
160 160
161 // wait for the task to run (we could use a kernel event here 161 // wait for the task to run (we could use a kernel event here
162 // instead to avoid busy waiting, but this is sufficient for 162 // instead to avoid busy waiting, but this is sufficient for
163 // testing purposes). 163 // testing purposes).
164 for (int i = 100; i >= 0 && !was_invoked; --i) { 164 for (int i = 100; i >= 0 && !was_invoked; --i) {
165 base::PlatformThread::Sleep(10); 165 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
166 } 166 }
167 EXPECT_TRUE(was_invoked); 167 EXPECT_TRUE(was_invoked);
168 } 168 }
169 169
170 TEST_F(ThreadTest, TwoTasks) { 170 TEST_F(ThreadTest, TwoTasks) {
171 bool was_invoked = false; 171 bool was_invoked = false;
172 { 172 {
173 Thread a("TwoTasks"); 173 Thread a("TwoTasks");
174 EXPECT_TRUE(a.Start()); 174 EXPECT_TRUE(a.Start());
175 EXPECT_TRUE(a.message_loop()); 175 EXPECT_TRUE(a.message_loop());
176 176
177 // Test that all events are dispatched before the Thread object is 177 // Test that all events are dispatched before the Thread object is
178 // destroyed. We do this by dispatching a sleep event before the 178 // destroyed. We do this by dispatching a sleep event before the
179 // event that will toggle our sentinel value. 179 // event that will toggle our sentinel value.
180 a.message_loop()->PostTask( 180 a.message_loop()->PostTask(
181 FROM_HERE, 181 FROM_HERE,
182 base::Bind(static_cast<void (*)(int)>(&base::PlatformThread::Sleep), 182 base::Bind(
183 20)); 183 static_cast<void (*)(base::TimeDelta)>(
184 &base::PlatformThread::Sleep),
185 base::TimeDelta::FromMilliseconds(20)));
184 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, 186 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue,
185 &was_invoked)); 187 &was_invoked));
186 } 188 }
187 EXPECT_TRUE(was_invoked); 189 EXPECT_TRUE(was_invoked);
188 } 190 }
189 191
190 TEST_F(ThreadTest, StopSoon) { 192 TEST_F(ThreadTest, StopSoon) {
191 Thread a("StopSoon"); 193 Thread a("StopSoon");
192 EXPECT_TRUE(a.Start()); 194 EXPECT_TRUE(a.Start());
193 EXPECT_TRUE(a.message_loop()); 195 EXPECT_TRUE(a.message_loop());
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 239
238 // Upon leaving this scope, the thread is deleted. 240 // Upon leaving this scope, the thread is deleted.
239 } 241 }
240 242
241 // Check the order of events during shutdown. 243 // Check the order of events during shutdown.
242 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); 244 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size());
243 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); 245 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]);
244 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); 246 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]);
245 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); 247 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]);
246 } 248 }
OLDNEW
« no previous file with comments | « base/threading/thread_collision_warner_unittest.cc ('k') | base/threading/watchdog_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698