OLD | NEW |
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/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
13 | 14 |
14 using base::Thread; | 15 using base::Thread; |
15 | 16 |
16 typedef PlatformTest ThreadTest; | 17 typedef PlatformTest ThreadTest; |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 class ToggleValue : public Task { | 21 void ToggleValue(bool* value) { |
21 public: | 22 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean " |
22 explicit ToggleValue(bool* value) : value_(value) { | 23 "in base/thread_unittest"); |
23 ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean " | 24 *value = !*value; |
24 "in base/thread_unittest"); | 25 } |
25 } | |
26 virtual void Run() { | |
27 *value_ = !*value_; | |
28 } | |
29 private: | |
30 bool* value_; | |
31 }; | |
32 | |
33 class SleepSome : public Task { | |
34 public: | |
35 explicit SleepSome(int msec) : msec_(msec) { | |
36 } | |
37 virtual void Run() { | |
38 base::PlatformThread::Sleep(msec_); | |
39 } | |
40 private: | |
41 int msec_; | |
42 }; | |
43 | 26 |
44 class SleepInsideInitThread : public Thread { | 27 class SleepInsideInitThread : public Thread { |
45 public: | 28 public: |
46 SleepInsideInitThread() : Thread("none") { | 29 SleepInsideInitThread() : Thread("none") { |
47 init_called_ = false; | 30 init_called_ = false; |
48 ANNOTATE_BENIGN_RACE( | 31 ANNOTATE_BENIGN_RACE( |
49 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"); |
50 } | 33 } |
51 virtual ~SleepInsideInitThread() { | 34 virtual ~SleepInsideInitThread() { |
52 Stop(); | 35 Stop(); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 Thread a("StartWithStackSize"); | 149 Thread a("StartWithStackSize"); |
167 // Ensure that the thread can work with only 12 kb and still process a | 150 // Ensure that the thread can work with only 12 kb and still process a |
168 // message. | 151 // message. |
169 Thread::Options options; | 152 Thread::Options options; |
170 options.stack_size = 12*1024; | 153 options.stack_size = 12*1024; |
171 EXPECT_TRUE(a.StartWithOptions(options)); | 154 EXPECT_TRUE(a.StartWithOptions(options)); |
172 EXPECT_TRUE(a.message_loop()); | 155 EXPECT_TRUE(a.message_loop()); |
173 EXPECT_TRUE(a.IsRunning()); | 156 EXPECT_TRUE(a.IsRunning()); |
174 | 157 |
175 bool was_invoked = false; | 158 bool was_invoked = false; |
176 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); | 159 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked)); |
177 | 160 |
178 // 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 |
179 // instead to avoid busy waiting, but this is sufficient for | 162 // instead to avoid busy waiting, but this is sufficient for |
180 // testing purposes). | 163 // testing purposes). |
181 for (int i = 100; i >= 0 && !was_invoked; --i) { | 164 for (int i = 100; i >= 0 && !was_invoked; --i) { |
182 base::PlatformThread::Sleep(10); | 165 base::PlatformThread::Sleep(10); |
183 } | 166 } |
184 EXPECT_TRUE(was_invoked); | 167 EXPECT_TRUE(was_invoked); |
185 } | 168 } |
186 | 169 |
187 TEST_F(ThreadTest, TwoTasks) { | 170 TEST_F(ThreadTest, TwoTasks) { |
188 bool was_invoked = false; | 171 bool was_invoked = false; |
189 { | 172 { |
190 Thread a("TwoTasks"); | 173 Thread a("TwoTasks"); |
191 EXPECT_TRUE(a.Start()); | 174 EXPECT_TRUE(a.Start()); |
192 EXPECT_TRUE(a.message_loop()); | 175 EXPECT_TRUE(a.message_loop()); |
193 | 176 |
194 // Test that all events are dispatched before the Thread object is | 177 // Test that all events are dispatched before the Thread object is |
195 // destroyed. We do this by dispatching a sleep event before the | 178 // destroyed. We do this by dispatching a sleep event before the |
196 // event that will toggle our sentinel value. | 179 // event that will toggle our sentinel value. |
197 a.message_loop()->PostTask(FROM_HERE, new SleepSome(20)); | 180 a.message_loop()->PostTask(FROM_HERE, |
198 a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); | 181 base::Bind(&base::PlatformThread::Sleep, 20)); |
| 182 a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, |
| 183 &was_invoked)); |
199 } | 184 } |
200 EXPECT_TRUE(was_invoked); | 185 EXPECT_TRUE(was_invoked); |
201 } | 186 } |
202 | 187 |
203 TEST_F(ThreadTest, StopSoon) { | 188 TEST_F(ThreadTest, StopSoon) { |
204 Thread a("StopSoon"); | 189 Thread a("StopSoon"); |
205 EXPECT_TRUE(a.Start()); | 190 EXPECT_TRUE(a.Start()); |
206 EXPECT_TRUE(a.message_loop()); | 191 EXPECT_TRUE(a.message_loop()); |
207 EXPECT_TRUE(a.IsRunning()); | 192 EXPECT_TRUE(a.IsRunning()); |
208 a.StopSoon(); | 193 a.StopSoon(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 235 |
251 // Upon leaving this scope, the thread is deleted. | 236 // Upon leaving this scope, the thread is deleted. |
252 } | 237 } |
253 | 238 |
254 // Check the order of events during shutdown. | 239 // Check the order of events during shutdown. |
255 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); | 240 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); |
256 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 241 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
257 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 242 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
258 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 243 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
259 } | 244 } |
OLD | NEW |