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