| 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/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 Loading... |
| 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(); |
| 186 a.StopSoon(); | 185 a.StopSoon(); |
| 187 a.Stop(); | 186 a.Stop(); |
| 188 EXPECT_FALSE(a.message_loop()); | 187 EXPECT_FALSE(a.message_loop()); |
| 189 EXPECT_FALSE(a.IsRunning()); | 188 EXPECT_FALSE(a.IsRunning()); |
| 190 } | 189 } |
| 191 | 190 |
| 192 TEST_F(ThreadTest, ThreadName) { | 191 TEST_F(ThreadTest, ThreadName) { |
| 193 Thread a("ThreadName"); | 192 Thread a("ThreadName"); |
| 194 EXPECT_TRUE(a.Start()); | 193 EXPECT_TRUE(a.Start()); |
| 195 EXPECT_EQ("ThreadName", a.thread_name()); | 194 EXPECT_EQ("ThreadName", a.thread_name()); |
| 196 } | 195 } |
| 197 | 196 |
| 198 // Make sure we can't use a thread between Start() and Init(). | 197 // Make sure Init() is called after Start() and before |
| 198 // WaitUntilThreadInitialized() returns. |
| 199 TEST_F(ThreadTest, SleepInsideInit) { | 199 TEST_F(ThreadTest, SleepInsideInit) { |
| 200 SleepInsideInitThread t; | 200 SleepInsideInitThread t; |
| 201 EXPECT_FALSE(t.InitCalled()); | 201 EXPECT_FALSE(t.InitCalled()); |
| 202 t.Start(); | 202 t.StartAndWaitForTesting(); |
| 203 EXPECT_TRUE(t.InitCalled()); | 203 EXPECT_TRUE(t.InitCalled()); |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Make sure that the destruction sequence is: | 206 // Make sure that the destruction sequence is: |
| 207 // | 207 // |
| 208 // (1) Thread::CleanUp() | 208 // (1) Thread::CleanUp() |
| 209 // (2) MessageLoop::~MessageLoop() | 209 // (2) MessageLoop::~MessageLoop() |
| 210 // MessageLoop::DestructionObservers called. | 210 // MessageLoop::DestructionObservers called. |
| 211 TEST_F(ThreadTest, CleanUp) { | 211 TEST_F(ThreadTest, CleanUp) { |
| 212 EventList captured_events; | 212 EventList captured_events; |
| 213 CapturingDestructionObserver loop_destruction_observer(&captured_events); | 213 CapturingDestructionObserver loop_destruction_observer(&captured_events); |
| 214 | 214 |
| 215 { | 215 { |
| 216 // Start a thread which writes its event into |captured_events|. | 216 // Start a thread which writes its event into |captured_events|. |
| 217 CaptureToEventList t(&captured_events); | 217 CaptureToEventList t(&captured_events); |
| 218 EXPECT_TRUE(t.Start()); | 218 EXPECT_TRUE(t.Start()); |
| 219 EXPECT_TRUE(t.message_loop()); | 219 EXPECT_TRUE(t.message_loop()); |
| 220 EXPECT_TRUE(t.IsRunning()); | 220 EXPECT_TRUE(t.IsRunning()); |
| 221 | 221 |
| 222 // Register an observer that writes into |captured_events| once the | 222 // Register an observer that writes into |captured_events| once the |
| 223 // thread's message loop is destroyed. | 223 // thread's message loop is destroyed. |
| 224 t.message_loop()->PostTask( | 224 t.task_runner()->PostTask( |
| 225 FROM_HERE, base::Bind(&RegisterDestructionObserver, | 225 FROM_HERE, base::Bind(&RegisterDestructionObserver, |
| 226 base::Unretained(&loop_destruction_observer))); | 226 base::Unretained(&loop_destruction_observer))); |
| 227 | 227 |
| 228 // Upon leaving this scope, the thread is deleted. | 228 // Upon leaving this scope, the thread is deleted. |
| 229 } | 229 } |
| 230 | 230 |
| 231 // Check the order of events during shutdown. | 231 // Check the order of events during shutdown. |
| 232 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()); |
| 233 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 233 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
| 234 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 234 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
| 235 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 235 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
| 236 } | 236 } |
| OLD | NEW |