henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/asyncinvoker.h" |
| 12 | #include "webrtc/base/asyncudpsocket.h" |
| 13 | #include "webrtc/base/event.h" |
| 14 | #include "webrtc/base/gunit.h" |
| 15 | #include "webrtc/base/physicalsocketserver.h" |
| 16 | #include "webrtc/base/socketaddress.h" |
| 17 | #include "webrtc/base/thread.h" |
| 18 | |
| 19 | #if defined(WEBRTC_WIN) |
| 20 | #include <comdef.h> // NOLINT |
| 21 | #endif |
| 22 | |
| 23 | using namespace rtc; |
| 24 | |
| 25 | // Generates a sequence of numbers (collaboratively). |
| 26 | class TestGenerator { |
| 27 | public: |
| 28 | TestGenerator() : last(0), count(0) {} |
| 29 | |
| 30 | int Next(int prev) { |
| 31 | int result = prev + last; |
| 32 | last = result; |
| 33 | count += 1; |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | int last; |
| 38 | int count; |
| 39 | }; |
| 40 | |
| 41 | struct TestMessage : public MessageData { |
| 42 | explicit TestMessage(int v) : value(v) {} |
| 43 | virtual ~TestMessage() {} |
| 44 | |
| 45 | int value; |
| 46 | }; |
| 47 | |
| 48 | // Receives on a socket and sends by posting messages. |
| 49 | class SocketClient : public TestGenerator, public sigslot::has_slots<> { |
| 50 | public: |
| 51 | SocketClient(AsyncSocket* socket, const SocketAddress& addr, |
| 52 | Thread* post_thread, MessageHandler* phandler) |
| 53 | : socket_(AsyncUDPSocket::Create(socket, addr)), |
| 54 | post_thread_(post_thread), |
| 55 | post_handler_(phandler) { |
| 56 | socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket); |
| 57 | } |
| 58 | |
| 59 | ~SocketClient() { |
| 60 | delete socket_; |
| 61 | } |
| 62 | |
| 63 | SocketAddress address() const { return socket_->GetLocalAddress(); } |
| 64 | |
| 65 | void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t size, |
| 66 | const SocketAddress& remote_addr, |
| 67 | const PacketTime& packet_time) { |
| 68 | EXPECT_EQ(size, sizeof(uint32)); |
| 69 | uint32 prev = reinterpret_cast<const uint32*>(buf)[0]; |
| 70 | uint32 result = Next(prev); |
| 71 | |
| 72 | post_thread_->PostDelayed(200, post_handler_, 0, new TestMessage(result)); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | AsyncUDPSocket* socket_; |
| 77 | Thread* post_thread_; |
| 78 | MessageHandler* post_handler_; |
| 79 | }; |
| 80 | |
| 81 | // Receives messages and sends on a socket. |
| 82 | class MessageClient : public MessageHandler, public TestGenerator { |
| 83 | public: |
| 84 | MessageClient(Thread* pth, Socket* socket) |
| 85 | : socket_(socket) { |
| 86 | } |
| 87 | |
| 88 | virtual ~MessageClient() { |
| 89 | delete socket_; |
| 90 | } |
| 91 | |
| 92 | virtual void OnMessage(Message *pmsg) { |
| 93 | TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata); |
| 94 | int result = Next(msg->value); |
| 95 | EXPECT_GE(socket_->Send(&result, sizeof(result)), 0); |
| 96 | delete msg; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | Socket* socket_; |
| 101 | }; |
| 102 | |
| 103 | class CustomThread : public rtc::Thread { |
| 104 | public: |
| 105 | CustomThread() {} |
| 106 | virtual ~CustomThread() { Stop(); } |
| 107 | bool Start() { return false; } |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | // A thread that does nothing when it runs and signals an event |
| 112 | // when it is destroyed. |
| 113 | class SignalWhenDestroyedThread : public Thread { |
| 114 | public: |
| 115 | SignalWhenDestroyedThread(Event* event) |
| 116 | : event_(event) { |
| 117 | } |
| 118 | |
| 119 | virtual ~SignalWhenDestroyedThread() { |
| 120 | Stop(); |
| 121 | event_->Set(); |
| 122 | } |
| 123 | |
| 124 | virtual void Run() { |
| 125 | // Do nothing. |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | Event* event_; |
| 130 | }; |
| 131 | |
| 132 | // Function objects to test Thread::Invoke. |
| 133 | struct FunctorA { |
| 134 | int operator()() { return 42; } |
| 135 | }; |
| 136 | class FunctorB { |
| 137 | public: |
| 138 | explicit FunctorB(bool* flag) : flag_(flag) {} |
| 139 | void operator()() { if (flag_) *flag_ = true; } |
| 140 | private: |
| 141 | bool* flag_; |
| 142 | }; |
| 143 | struct FunctorC { |
| 144 | int operator()() { |
| 145 | Thread::Current()->ProcessMessages(50); |
| 146 | return 24; |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | // See: https://code.google.com/p/webrtc/issues/detail?id=2409 |
| 151 | TEST(ThreadTest, DISABLED_Main) { |
| 152 | const SocketAddress addr("127.0.0.1", 0); |
| 153 | |
| 154 | // Create the messaging client on its own thread. |
| 155 | Thread th1; |
| 156 | Socket* socket = th1.socketserver()->CreateAsyncSocket(addr.family(), |
| 157 | SOCK_DGRAM); |
| 158 | MessageClient msg_client(&th1, socket); |
| 159 | |
| 160 | // Create the socket client on its own thread. |
| 161 | Thread th2; |
| 162 | AsyncSocket* asocket = |
| 163 | th2.socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
| 164 | SocketClient sock_client(asocket, addr, &th1, &msg_client); |
| 165 | |
| 166 | socket->Connect(sock_client.address()); |
| 167 | |
| 168 | th1.Start(); |
| 169 | th2.Start(); |
| 170 | |
| 171 | // Get the messages started. |
| 172 | th1.PostDelayed(100, &msg_client, 0, new TestMessage(1)); |
| 173 | |
| 174 | // Give the clients a little while to run. |
| 175 | // Messages will be processed at 100, 300, 500, 700, 900. |
| 176 | Thread* th_main = Thread::Current(); |
| 177 | th_main->ProcessMessages(1000); |
| 178 | |
| 179 | // Stop the sending client. Give the receiver a bit longer to run, in case |
| 180 | // it is running on a machine that is under load (e.g. the build machine). |
| 181 | th1.Stop(); |
| 182 | th_main->ProcessMessages(200); |
| 183 | th2.Stop(); |
| 184 | |
| 185 | // Make sure the results were correct |
| 186 | EXPECT_EQ(5, msg_client.count); |
| 187 | EXPECT_EQ(34, msg_client.last); |
| 188 | EXPECT_EQ(5, sock_client.count); |
| 189 | EXPECT_EQ(55, sock_client.last); |
| 190 | } |
| 191 | |
| 192 | // Test that setting thread names doesn't cause a malfunction. |
| 193 | // There's no easy way to verify the name was set properly at this time. |
| 194 | TEST(ThreadTest, Names) { |
| 195 | // Default name |
| 196 | Thread *thread; |
| 197 | thread = new Thread(); |
| 198 | EXPECT_TRUE(thread->Start()); |
| 199 | thread->Stop(); |
| 200 | delete thread; |
| 201 | thread = new Thread(); |
| 202 | // Name with no object parameter |
| 203 | EXPECT_TRUE(thread->SetName("No object", NULL)); |
| 204 | EXPECT_TRUE(thread->Start()); |
| 205 | thread->Stop(); |
| 206 | delete thread; |
| 207 | // Really long name |
| 208 | thread = new Thread(); |
| 209 | EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); |
| 210 | EXPECT_TRUE(thread->Start()); |
| 211 | thread->Stop(); |
| 212 | delete thread; |
| 213 | } |
| 214 | |
| 215 | // Test that setting thread priorities doesn't cause a malfunction. |
| 216 | // There's no easy way to verify the priority was set properly at this time. |
| 217 | TEST(ThreadTest, Priorities) { |
| 218 | Thread *thread; |
| 219 | thread = new Thread(); |
| 220 | EXPECT_TRUE(thread->SetPriority(PRIORITY_HIGH)); |
| 221 | EXPECT_TRUE(thread->Start()); |
| 222 | thread->Stop(); |
| 223 | delete thread; |
| 224 | thread = new Thread(); |
| 225 | EXPECT_TRUE(thread->SetPriority(PRIORITY_ABOVE_NORMAL)); |
| 226 | EXPECT_TRUE(thread->Start()); |
| 227 | thread->Stop(); |
| 228 | delete thread; |
| 229 | |
| 230 | thread = new Thread(); |
| 231 | EXPECT_TRUE(thread->Start()); |
| 232 | #if defined(WEBRTC_WIN) |
| 233 | EXPECT_TRUE(thread->SetPriority(PRIORITY_ABOVE_NORMAL)); |
| 234 | #else |
| 235 | EXPECT_FALSE(thread->SetPriority(PRIORITY_ABOVE_NORMAL)); |
| 236 | #endif |
| 237 | thread->Stop(); |
| 238 | delete thread; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | TEST(ThreadTest, Wrap) { |
| 243 | Thread* current_thread = Thread::Current(); |
| 244 | current_thread->UnwrapCurrent(); |
| 245 | CustomThread* cthread = new CustomThread(); |
| 246 | EXPECT_TRUE(cthread->WrapCurrent()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 247 | EXPECT_TRUE(cthread->RunningForTest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | EXPECT_FALSE(cthread->IsOwned()); |
| 249 | cthread->UnwrapCurrent(); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 250 | EXPECT_FALSE(cthread->RunningForTest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 251 | delete cthread; |
| 252 | current_thread->WrapCurrent(); |
| 253 | } |
| 254 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | TEST(ThreadTest, Invoke) { |
| 256 | // Create and start the thread. |
| 257 | Thread thread; |
| 258 | thread.Start(); |
| 259 | // Try calling functors. |
| 260 | EXPECT_EQ(42, thread.Invoke<int>(FunctorA())); |
| 261 | bool called = false; |
| 262 | FunctorB f2(&called); |
| 263 | thread.Invoke<void>(f2); |
| 264 | EXPECT_TRUE(called); |
| 265 | // Try calling bare functions. |
| 266 | struct LocalFuncs { |
| 267 | static int Func1() { return 999; } |
| 268 | static void Func2() {} |
| 269 | }; |
| 270 | EXPECT_EQ(999, thread.Invoke<int>(&LocalFuncs::Func1)); |
| 271 | thread.Invoke<void>(&LocalFuncs::Func2); |
| 272 | } |
| 273 | |
| 274 | class AsyncInvokeTest : public testing::Test { |
| 275 | public: |
| 276 | void IntCallback(int value) { |
| 277 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 278 | int_value_ = value; |
| 279 | } |
| 280 | void AsyncInvokeIntCallback(AsyncInvoker* invoker, Thread* thread) { |
| 281 | expected_thread_ = thread; |
| 282 | invoker->AsyncInvoke(thread, FunctorC(), |
| 283 | &AsyncInvokeTest::IntCallback, |
| 284 | static_cast<AsyncInvokeTest*>(this)); |
| 285 | invoke_started_.Set(); |
| 286 | } |
| 287 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 288 | expected_thread_ = thread; |
| 289 | } |
| 290 | |
| 291 | protected: |
| 292 | enum { kWaitTimeout = 1000 }; |
| 293 | AsyncInvokeTest() |
| 294 | : int_value_(0), |
| 295 | invoke_started_(true, false), |
| 296 | expected_thread_(NULL) {} |
| 297 | |
| 298 | int int_value_; |
| 299 | Event invoke_started_; |
| 300 | Thread* expected_thread_; |
| 301 | }; |
| 302 | |
| 303 | TEST_F(AsyncInvokeTest, FireAndForget) { |
| 304 | AsyncInvoker invoker; |
| 305 | // Create and start the thread. |
| 306 | Thread thread; |
| 307 | thread.Start(); |
| 308 | // Try calling functor. |
| 309 | bool called = false; |
| 310 | invoker.AsyncInvoke<void>(&thread, FunctorB(&called)); |
| 311 | EXPECT_TRUE_WAIT(called, kWaitTimeout); |
| 312 | } |
| 313 | |
| 314 | TEST_F(AsyncInvokeTest, WithCallback) { |
| 315 | AsyncInvoker invoker; |
| 316 | // Create and start the thread. |
| 317 | Thread thread; |
| 318 | thread.Start(); |
| 319 | // Try calling functor. |
| 320 | SetExpectedThreadForIntCallback(Thread::Current()); |
| 321 | invoker.AsyncInvoke(&thread, FunctorA(), |
| 322 | &AsyncInvokeTest::IntCallback, |
| 323 | static_cast<AsyncInvokeTest*>(this)); |
| 324 | EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); |
| 325 | } |
| 326 | |
| 327 | TEST_F(AsyncInvokeTest, CancelInvoker) { |
| 328 | // Create and start the thread. |
| 329 | Thread thread; |
| 330 | thread.Start(); |
| 331 | // Try destroying invoker during call. |
| 332 | { |
| 333 | AsyncInvoker invoker; |
| 334 | invoker.AsyncInvoke(&thread, FunctorC(), |
| 335 | &AsyncInvokeTest::IntCallback, |
| 336 | static_cast<AsyncInvokeTest*>(this)); |
| 337 | } |
| 338 | // With invoker gone, callback should be cancelled. |
| 339 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 340 | EXPECT_EQ(0, int_value_); |
| 341 | } |
| 342 | |
| 343 | TEST_F(AsyncInvokeTest, CancelCallingThread) { |
| 344 | AsyncInvoker invoker; |
| 345 | { // Create and start the thread. |
| 346 | Thread thread; |
| 347 | thread.Start(); |
| 348 | // Try calling functor. |
| 349 | thread.Invoke<void>(Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, |
| 350 | static_cast<AsyncInvokeTest*>(this), |
| 351 | &invoker, Thread::Current())); |
| 352 | // Wait for the call to begin. |
| 353 | ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
| 354 | } |
| 355 | // Calling thread is gone. Return message shouldn't happen. |
| 356 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 357 | EXPECT_EQ(0, int_value_); |
| 358 | } |
| 359 | |
| 360 | TEST_F(AsyncInvokeTest, KillInvokerBeforeExecute) { |
| 361 | Thread thread; |
| 362 | thread.Start(); |
| 363 | { |
| 364 | AsyncInvoker invoker; |
| 365 | // Try calling functor. |
| 366 | thread.Invoke<void>(Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, |
| 367 | static_cast<AsyncInvokeTest*>(this), |
| 368 | &invoker, Thread::Current())); |
| 369 | // Wait for the call to begin. |
| 370 | ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
| 371 | } |
| 372 | // Invoker is destroyed. Function should not execute. |
| 373 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 374 | EXPECT_EQ(0, int_value_); |
| 375 | } |
| 376 | |
| 377 | TEST_F(AsyncInvokeTest, Flush) { |
| 378 | AsyncInvoker invoker; |
| 379 | bool flag1 = false; |
| 380 | bool flag2 = false; |
| 381 | // Queue two async calls to the current thread. |
| 382 | invoker.AsyncInvoke<void>(Thread::Current(), |
| 383 | FunctorB(&flag1)); |
| 384 | invoker.AsyncInvoke<void>(Thread::Current(), |
| 385 | FunctorB(&flag2)); |
| 386 | // Because we haven't pumped messages, these should not have run yet. |
| 387 | EXPECT_FALSE(flag1); |
| 388 | EXPECT_FALSE(flag2); |
| 389 | // Force them to run now. |
| 390 | invoker.Flush(Thread::Current()); |
| 391 | EXPECT_TRUE(flag1); |
| 392 | EXPECT_TRUE(flag2); |
| 393 | } |
| 394 | |
| 395 | TEST_F(AsyncInvokeTest, FlushWithIds) { |
| 396 | AsyncInvoker invoker; |
| 397 | bool flag1 = false; |
| 398 | bool flag2 = false; |
| 399 | // Queue two async calls to the current thread, one with a message id. |
| 400 | invoker.AsyncInvoke<void>(Thread::Current(), |
| 401 | FunctorB(&flag1), |
| 402 | 5); |
| 403 | invoker.AsyncInvoke<void>(Thread::Current(), |
| 404 | FunctorB(&flag2)); |
| 405 | // Because we haven't pumped messages, these should not have run yet. |
| 406 | EXPECT_FALSE(flag1); |
| 407 | EXPECT_FALSE(flag2); |
| 408 | // Execute pending calls with id == 5. |
| 409 | invoker.Flush(Thread::Current(), 5); |
| 410 | EXPECT_TRUE(flag1); |
| 411 | EXPECT_FALSE(flag2); |
| 412 | flag1 = false; |
| 413 | // Execute all pending calls. The id == 5 call should not execute again. |
| 414 | invoker.Flush(Thread::Current()); |
| 415 | EXPECT_FALSE(flag1); |
| 416 | EXPECT_TRUE(flag2); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | #if defined(WEBRTC_WIN) |
| 421 | class ComThreadTest : public testing::Test, public MessageHandler { |
| 422 | public: |
| 423 | ComThreadTest() : done_(false) {} |
| 424 | protected: |
| 425 | virtual void OnMessage(Message* message) { |
| 426 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 427 | // S_FALSE means the thread was already inited for a multithread apartment. |
| 428 | EXPECT_EQ(S_FALSE, hr); |
| 429 | if (SUCCEEDED(hr)) { |
| 430 | CoUninitialize(); |
| 431 | } |
| 432 | done_ = true; |
| 433 | } |
| 434 | bool done_; |
| 435 | }; |
| 436 | |
| 437 | TEST_F(ComThreadTest, ComInited) { |
| 438 | Thread* thread = new ComThread(); |
| 439 | EXPECT_TRUE(thread->Start()); |
| 440 | thread->Post(this, 0); |
| 441 | EXPECT_TRUE_WAIT(done_, 1000); |
| 442 | delete thread; |
| 443 | } |
| 444 | #endif |