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 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include "webrtc/base/asyncinvoker.h" |
| 14 | #include "webrtc/base/asyncudpsocket.h" |
| 15 | #include "webrtc/base/event.h" |
| 16 | #include "webrtc/base/gunit.h" |
| 17 | #include "webrtc/base/physicalsocketserver.h" |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 18 | #include "webrtc/base/sigslot.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #include "webrtc/base/socketaddress.h" |
| 20 | #include "webrtc/base/thread.h" |
| 21 | |
| 22 | #if defined(WEBRTC_WIN) |
| 23 | #include <comdef.h> // NOLINT |
| 24 | #endif |
| 25 | |
| 26 | using namespace rtc; |
| 27 | |
| 28 | // Generates a sequence of numbers (collaboratively). |
| 29 | class TestGenerator { |
| 30 | public: |
| 31 | TestGenerator() : last(0), count(0) {} |
| 32 | |
| 33 | int Next(int prev) { |
| 34 | int result = prev + last; |
| 35 | last = result; |
| 36 | count += 1; |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | int last; |
| 41 | int count; |
| 42 | }; |
| 43 | |
| 44 | struct TestMessage : public MessageData { |
| 45 | explicit TestMessage(int v) : value(v) {} |
| 46 | virtual ~TestMessage() {} |
| 47 | |
| 48 | int value; |
| 49 | }; |
| 50 | |
| 51 | // Receives on a socket and sends by posting messages. |
| 52 | class SocketClient : public TestGenerator, public sigslot::has_slots<> { |
| 53 | public: |
| 54 | SocketClient(AsyncSocket* socket, const SocketAddress& addr, |
| 55 | Thread* post_thread, MessageHandler* phandler) |
| 56 | : socket_(AsyncUDPSocket::Create(socket, addr)), |
| 57 | post_thread_(post_thread), |
| 58 | post_handler_(phandler) { |
| 59 | socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket); |
| 60 | } |
| 61 | |
| 62 | ~SocketClient() { |
| 63 | delete socket_; |
| 64 | } |
| 65 | |
| 66 | SocketAddress address() const { return socket_->GetLocalAddress(); } |
| 67 | |
| 68 | void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t size, |
| 69 | const SocketAddress& remote_addr, |
| 70 | const PacketTime& packet_time) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 71 | EXPECT_EQ(size, sizeof(uint32_t)); |
| 72 | uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0]; |
| 73 | uint32_t result = Next(prev); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 75 | post_thread_->PostDelayed(RTC_FROM_HERE, 200, post_handler_, 0, |
| 76 | new TestMessage(result)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private: |
| 80 | AsyncUDPSocket* socket_; |
| 81 | Thread* post_thread_; |
| 82 | MessageHandler* post_handler_; |
| 83 | }; |
| 84 | |
| 85 | // Receives messages and sends on a socket. |
| 86 | class MessageClient : public MessageHandler, public TestGenerator { |
| 87 | public: |
| 88 | MessageClient(Thread* pth, Socket* socket) |
| 89 | : socket_(socket) { |
| 90 | } |
| 91 | |
| 92 | virtual ~MessageClient() { |
| 93 | delete socket_; |
| 94 | } |
| 95 | |
| 96 | virtual void OnMessage(Message *pmsg) { |
| 97 | TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata); |
| 98 | int result = Next(msg->value); |
| 99 | EXPECT_GE(socket_->Send(&result, sizeof(result)), 0); |
| 100 | delete msg; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | Socket* socket_; |
| 105 | }; |
| 106 | |
| 107 | class CustomThread : public rtc::Thread { |
| 108 | public: |
| 109 | CustomThread() {} |
| 110 | virtual ~CustomThread() { Stop(); } |
| 111 | bool Start() { return false; } |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 112 | |
| 113 | bool WrapCurrent() { |
| 114 | return Thread::WrapCurrent(); |
| 115 | } |
| 116 | void UnwrapCurrent() { |
| 117 | Thread::UnwrapCurrent(); |
| 118 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | |
| 122 | // A thread that does nothing when it runs and signals an event |
| 123 | // when it is destroyed. |
| 124 | class SignalWhenDestroyedThread : public Thread { |
| 125 | public: |
| 126 | SignalWhenDestroyedThread(Event* event) |
| 127 | : event_(event) { |
| 128 | } |
| 129 | |
| 130 | virtual ~SignalWhenDestroyedThread() { |
| 131 | Stop(); |
| 132 | event_->Set(); |
| 133 | } |
| 134 | |
| 135 | virtual void Run() { |
| 136 | // Do nothing. |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | Event* event_; |
| 141 | }; |
| 142 | |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 143 | // A bool wrapped in a mutex, to avoid data races. Using a volatile |
| 144 | // bool should be sufficient for correct code ("eventual consistency" |
| 145 | // between caches is sufficient), but we can't tell the compiler about |
| 146 | // that, and then tsan complains about a data race. |
| 147 | |
| 148 | // See also discussion at |
| 149 | // http://stackoverflow.com/questions/7223164/is-mutex-needed-to-synchronize-a-simple-flag-between-pthreads |
| 150 | |
| 151 | // Using std::atomic<bool> or std::atomic_flag in C++11 is probably |
| 152 | // the right thing to do, but those features are not yet allowed. Or |
| 153 | // rtc::AtomicInt, if/when that is added. Since the use isn't |
| 154 | // performance critical, use a plain critical section for the time |
| 155 | // being. |
| 156 | |
| 157 | class AtomicBool { |
| 158 | public: |
| 159 | explicit AtomicBool(bool value = false) : flag_(value) {} |
| 160 | AtomicBool& operator=(bool value) { |
| 161 | CritScope scoped_lock(&cs_); |
| 162 | flag_ = value; |
| 163 | return *this; |
| 164 | } |
| 165 | bool get() const { |
| 166 | CritScope scoped_lock(&cs_); |
| 167 | return flag_; |
| 168 | } |
| 169 | |
| 170 | private: |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 171 | CriticalSection cs_; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 172 | bool flag_; |
| 173 | }; |
| 174 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | // Function objects to test Thread::Invoke. |
| 176 | struct FunctorA { |
| 177 | int operator()() { return 42; } |
| 178 | }; |
| 179 | class FunctorB { |
| 180 | public: |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 181 | explicit FunctorB(AtomicBool* flag) : flag_(flag) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 182 | void operator()() { if (flag_) *flag_ = true; } |
| 183 | private: |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 184 | AtomicBool* flag_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | }; |
| 186 | struct FunctorC { |
| 187 | int operator()() { |
| 188 | Thread::Current()->ProcessMessages(50); |
| 189 | return 24; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | // See: https://code.google.com/p/webrtc/issues/detail?id=2409 |
| 194 | TEST(ThreadTest, DISABLED_Main) { |
| 195 | const SocketAddress addr("127.0.0.1", 0); |
| 196 | |
| 197 | // Create the messaging client on its own thread. |
| 198 | Thread th1; |
| 199 | Socket* socket = th1.socketserver()->CreateAsyncSocket(addr.family(), |
| 200 | SOCK_DGRAM); |
| 201 | MessageClient msg_client(&th1, socket); |
| 202 | |
| 203 | // Create the socket client on its own thread. |
| 204 | Thread th2; |
| 205 | AsyncSocket* asocket = |
| 206 | th2.socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
| 207 | SocketClient sock_client(asocket, addr, &th1, &msg_client); |
| 208 | |
| 209 | socket->Connect(sock_client.address()); |
| 210 | |
| 211 | th1.Start(); |
| 212 | th2.Start(); |
| 213 | |
| 214 | // Get the messages started. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 215 | th1.PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | |
| 217 | // Give the clients a little while to run. |
| 218 | // Messages will be processed at 100, 300, 500, 700, 900. |
| 219 | Thread* th_main = Thread::Current(); |
| 220 | th_main->ProcessMessages(1000); |
| 221 | |
| 222 | // Stop the sending client. Give the receiver a bit longer to run, in case |
| 223 | // it is running on a machine that is under load (e.g. the build machine). |
| 224 | th1.Stop(); |
| 225 | th_main->ProcessMessages(200); |
| 226 | th2.Stop(); |
| 227 | |
| 228 | // Make sure the results were correct |
| 229 | EXPECT_EQ(5, msg_client.count); |
| 230 | EXPECT_EQ(34, msg_client.last); |
| 231 | EXPECT_EQ(5, sock_client.count); |
| 232 | EXPECT_EQ(55, sock_client.last); |
| 233 | } |
| 234 | |
| 235 | // Test that setting thread names doesn't cause a malfunction. |
| 236 | // There's no easy way to verify the name was set properly at this time. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 237 | TEST(ThreadTest, Names) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 238 | // Default name |
| 239 | Thread *thread; |
| 240 | thread = new Thread(); |
| 241 | EXPECT_TRUE(thread->Start()); |
| 242 | thread->Stop(); |
| 243 | delete thread; |
| 244 | thread = new Thread(); |
| 245 | // Name with no object parameter |
| 246 | EXPECT_TRUE(thread->SetName("No object", NULL)); |
| 247 | EXPECT_TRUE(thread->Start()); |
| 248 | thread->Stop(); |
| 249 | delete thread; |
| 250 | // Really long name |
| 251 | thread = new Thread(); |
| 252 | EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); |
| 253 | EXPECT_TRUE(thread->Start()); |
| 254 | thread->Stop(); |
| 255 | delete thread; |
| 256 | } |
| 257 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 258 | TEST(ThreadTest, Wrap) { |
| 259 | Thread* current_thread = Thread::Current(); |
| 260 | current_thread->UnwrapCurrent(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 261 | CustomThread* cthread = new CustomThread(); |
| 262 | EXPECT_TRUE(cthread->WrapCurrent()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 263 | EXPECT_TRUE(cthread->RunningForTest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 264 | EXPECT_FALSE(cthread->IsOwned()); |
| 265 | cthread->UnwrapCurrent(); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 266 | EXPECT_FALSE(cthread->RunningForTest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 267 | delete cthread; |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 268 | current_thread->WrapCurrent(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | } |
| 270 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 271 | TEST(ThreadTest, Invoke) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 272 | // Create and start the thread. |
| 273 | Thread thread; |
| 274 | thread.Start(); |
| 275 | // Try calling functors. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 276 | EXPECT_EQ(42, thread.Invoke<int>(RTC_FROM_HERE, FunctorA())); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 277 | AtomicBool called; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 278 | FunctorB f2(&called); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 279 | thread.Invoke<void>(RTC_FROM_HERE, f2); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 280 | EXPECT_TRUE(called.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 281 | // Try calling bare functions. |
| 282 | struct LocalFuncs { |
| 283 | static int Func1() { return 999; } |
| 284 | static void Func2() {} |
| 285 | }; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 286 | EXPECT_EQ(999, thread.Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); |
| 287 | thread.Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 288 | } |
| 289 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 290 | // Verifies that two threads calling Invoke on each other at the same time does |
| 291 | // not deadlock. |
| 292 | TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { |
| 293 | AutoThread thread; |
| 294 | Thread* current_thread = Thread::Current(); |
| 295 | ASSERT_TRUE(current_thread != NULL); |
| 296 | |
| 297 | Thread other_thread; |
| 298 | other_thread.Start(); |
| 299 | |
| 300 | struct LocalFuncs { |
| 301 | static void Set(bool* out) { *out = true; } |
| 302 | static void InvokeSet(Thread* thread, bool* out) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 303 | thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 304 | } |
| 305 | }; |
| 306 | |
| 307 | bool called = false; |
| 308 | other_thread.Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 309 | RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 310 | |
| 311 | EXPECT_TRUE(called); |
| 312 | } |
| 313 | |
| 314 | // Verifies that if thread A invokes a call on thread B and thread C is trying |
| 315 | // to invoke A at the same time, thread A does not handle C's invoke while |
| 316 | // invoking B. |
| 317 | TEST(ThreadTest, ThreeThreadsInvoke) { |
| 318 | AutoThread thread; |
| 319 | Thread* thread_a = Thread::Current(); |
| 320 | Thread thread_b, thread_c; |
| 321 | thread_b.Start(); |
| 322 | thread_c.Start(); |
| 323 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 324 | class LockedBool { |
| 325 | public: |
| 326 | explicit LockedBool(bool value) : value_(value) {} |
| 327 | |
| 328 | void Set(bool value) { |
| 329 | CritScope lock(&crit_); |
| 330 | value_ = value; |
| 331 | } |
| 332 | |
| 333 | bool Get() { |
| 334 | CritScope lock(&crit_); |
| 335 | return value_; |
| 336 | } |
| 337 | |
| 338 | private: |
| 339 | CriticalSection crit_; |
| 340 | bool value_ GUARDED_BY(crit_); |
| 341 | }; |
| 342 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 343 | struct LocalFuncs { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 344 | static void Set(LockedBool* out) { out->Set(true); } |
| 345 | static void InvokeSet(Thread* thread, LockedBool* out) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 346 | thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // Set |out| true and call InvokeSet on |thread|. |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 350 | static void SetAndInvokeSet(LockedBool* out, |
| 351 | Thread* thread, |
| 352 | LockedBool* out_inner) { |
| 353 | out->Set(true); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 354 | InvokeSet(thread, out_inner); |
| 355 | } |
| 356 | |
| 357 | // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until |
| 358 | // |thread1| starts the call. |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 359 | static void AsyncInvokeSetAndWait(AsyncInvoker* invoker, |
| 360 | Thread* thread1, |
| 361 | Thread* thread2, |
| 362 | LockedBool* out) { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 363 | CriticalSection crit; |
| 364 | LockedBool async_invoked(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 365 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 366 | invoker->AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 367 | RTC_FROM_HERE, thread1, |
| 368 | Bind(&SetAndInvokeSet, &async_invoked, thread2, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 369 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 370 | EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 371 | } |
| 372 | }; |
| 373 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 374 | AsyncInvoker invoker; |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 375 | LockedBool thread_a_called(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 376 | |
| 377 | // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. |
| 378 | // Thread B returns when C receives the call and C should be blocked until A |
| 379 | // starts to process messages. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 380 | thread_b.Invoke<void>(RTC_FROM_HERE, |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 381 | Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker, |
| 382 | &thread_c, thread_a, &thread_a_called)); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 383 | EXPECT_FALSE(thread_a_called.Get()); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 384 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 385 | EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 386 | } |
| 387 | |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 388 | // Set the name on a thread when the underlying QueueDestroyed signal is |
| 389 | // triggered. This causes an error if the object is already partially |
| 390 | // destroyed. |
| 391 | class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { |
| 392 | public: |
| 393 | SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) { |
| 394 | thread->SignalQueueDestroyed.connect( |
| 395 | this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed); |
| 396 | } |
| 397 | |
| 398 | void OnQueueDestroyed() { |
| 399 | // Makes sure that if we access the Thread while it's being destroyed, that |
| 400 | // it doesn't cause a problem because the vtable has been modified. |
| 401 | thread_->SetName("foo", nullptr); |
| 402 | } |
| 403 | |
| 404 | private: |
| 405 | Thread* thread_; |
| 406 | }; |
| 407 | |
| 408 | TEST(ThreadTest, SetNameOnSignalQueueDestroyed) { |
| 409 | Thread* thread1 = new Thread(); |
| 410 | SetNameOnSignalQueueDestroyedTester tester1(thread1); |
| 411 | delete thread1; |
| 412 | |
| 413 | Thread* thread2 = new AutoThread(); |
| 414 | SetNameOnSignalQueueDestroyedTester tester2(thread2); |
| 415 | delete thread2; |
| 416 | |
| 417 | #if defined(WEBRTC_WIN) |
| 418 | Thread* thread3 = new ComThread(); |
| 419 | SetNameOnSignalQueueDestroyedTester tester3(thread3); |
| 420 | delete thread3; |
| 421 | #endif |
| 422 | } |
| 423 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 424 | class AsyncInvokeTest : public testing::Test { |
| 425 | public: |
| 426 | void IntCallback(int value) { |
| 427 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 428 | int_value_ = value; |
| 429 | } |
| 430 | void AsyncInvokeIntCallback(AsyncInvoker* invoker, Thread* thread) { |
| 431 | expected_thread_ = thread; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 432 | invoker->AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, thread, FunctorC(), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 433 | &AsyncInvokeTest::IntCallback, |
| 434 | static_cast<AsyncInvokeTest*>(this)); |
| 435 | invoke_started_.Set(); |
| 436 | } |
| 437 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 438 | expected_thread_ = thread; |
| 439 | } |
| 440 | |
| 441 | protected: |
| 442 | enum { kWaitTimeout = 1000 }; |
| 443 | AsyncInvokeTest() |
| 444 | : int_value_(0), |
| 445 | invoke_started_(true, false), |
| 446 | expected_thread_(NULL) {} |
| 447 | |
| 448 | int int_value_; |
| 449 | Event invoke_started_; |
| 450 | Thread* expected_thread_; |
| 451 | }; |
| 452 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 453 | TEST_F(AsyncInvokeTest, FireAndForget) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 454 | AsyncInvoker invoker; |
| 455 | // Create and start the thread. |
| 456 | Thread thread; |
| 457 | thread.Start(); |
| 458 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 459 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 460 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, FunctorB(&called)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 461 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 462 | } |
| 463 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 464 | TEST_F(AsyncInvokeTest, WithCallback) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 465 | AsyncInvoker invoker; |
| 466 | // Create and start the thread. |
| 467 | Thread thread; |
| 468 | thread.Start(); |
| 469 | // Try calling functor. |
| 470 | SetExpectedThreadForIntCallback(Thread::Current()); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 471 | invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, &thread, FunctorA(), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 472 | &AsyncInvokeTest::IntCallback, |
| 473 | static_cast<AsyncInvokeTest*>(this)); |
| 474 | EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); |
| 475 | } |
| 476 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 477 | TEST_F(AsyncInvokeTest, CancelInvoker) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 478 | // Create and start the thread. |
| 479 | Thread thread; |
| 480 | thread.Start(); |
| 481 | // Try destroying invoker during call. |
| 482 | { |
| 483 | AsyncInvoker invoker; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 484 | invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, &thread, FunctorC(), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 485 | &AsyncInvokeTest::IntCallback, |
| 486 | static_cast<AsyncInvokeTest*>(this)); |
| 487 | } |
| 488 | // With invoker gone, callback should be cancelled. |
| 489 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 490 | EXPECT_EQ(0, int_value_); |
| 491 | } |
| 492 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 493 | TEST_F(AsyncInvokeTest, CancelCallingThread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 494 | AsyncInvoker invoker; |
| 495 | { // Create and start the thread. |
| 496 | Thread thread; |
| 497 | thread.Start(); |
| 498 | // Try calling functor. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 499 | thread.Invoke<void>( |
| 500 | RTC_FROM_HERE, |
| 501 | Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, |
| 502 | static_cast<AsyncInvokeTest*>(this), &invoker, Thread::Current())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 503 | // Wait for the call to begin. |
| 504 | ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
| 505 | } |
| 506 | // Calling thread is gone. Return message shouldn't happen. |
| 507 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 508 | EXPECT_EQ(0, int_value_); |
| 509 | } |
| 510 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 511 | TEST_F(AsyncInvokeTest, KillInvokerBeforeExecute) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 512 | Thread thread; |
| 513 | thread.Start(); |
| 514 | { |
| 515 | AsyncInvoker invoker; |
| 516 | // Try calling functor. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 517 | thread.Invoke<void>( |
| 518 | RTC_FROM_HERE, |
| 519 | Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, |
| 520 | static_cast<AsyncInvokeTest*>(this), &invoker, Thread::Current())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 521 | // Wait for the call to begin. |
| 522 | ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
| 523 | } |
| 524 | // Invoker is destroyed. Function should not execute. |
| 525 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 526 | EXPECT_EQ(0, int_value_); |
| 527 | } |
| 528 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 529 | TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) { |
| 530 | // Use these events to get in a state where the functor is in the middle of |
| 531 | // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE" |
| 532 | // is run. |
| 533 | Event functor_started(false, false); |
| 534 | Event functor_continue(false, false); |
| 535 | Event functor_finished(false, false); |
| 536 | |
| 537 | Thread thread; |
| 538 | thread.Start(); |
| 539 | volatile bool invoker_destroyed = false; |
| 540 | { |
| 541 | AsyncInvoker invoker; |
| 542 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, |
| 543 | [&functor_started, &functor_continue, |
| 544 | &functor_finished, &invoker_destroyed] { |
| 545 | functor_started.Set(); |
| 546 | functor_continue.Wait(Event::kForever); |
| 547 | rtc::Thread::Current()->SleepMs(kWaitTimeout); |
| 548 | EXPECT_FALSE(invoker_destroyed); |
| 549 | functor_finished.Set(); |
| 550 | }); |
| 551 | functor_started.Wait(Event::kForever); |
| 552 | |
| 553 | // Allow the functor to continue and immediately destroy the invoker. |
| 554 | functor_continue.Set(); |
| 555 | } |
| 556 | |
| 557 | // If the destructor DIDN'T wait for the functor to finish executing, it will |
| 558 | // hit the EXPECT_FALSE(invoker_destroyed) after it finishes sleeping for a |
| 559 | // second. |
| 560 | invoker_destroyed = true; |
| 561 | functor_finished.Wait(Event::kForever); |
| 562 | } |
| 563 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 564 | TEST_F(AsyncInvokeTest, Flush) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 565 | AsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 566 | AtomicBool flag1; |
| 567 | AtomicBool flag2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 568 | // Queue two async calls to the current thread. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 569 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1)); |
| 570 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 571 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 572 | EXPECT_FALSE(flag1.get()); |
| 573 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 574 | // Force them to run now. |
| 575 | invoker.Flush(Thread::Current()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 576 | EXPECT_TRUE(flag1.get()); |
| 577 | EXPECT_TRUE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 578 | } |
| 579 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 580 | TEST_F(AsyncInvokeTest, FlushWithIds) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 581 | AsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 582 | AtomicBool flag1; |
| 583 | AtomicBool flag2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 584 | // Queue two async calls to the current thread, one with a message id. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 585 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 586 | 5); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 587 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 588 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 589 | EXPECT_FALSE(flag1.get()); |
| 590 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 591 | // Execute pending calls with id == 5. |
| 592 | invoker.Flush(Thread::Current(), 5); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 593 | EXPECT_TRUE(flag1.get()); |
| 594 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 595 | flag1 = false; |
| 596 | // Execute all pending calls. The id == 5 call should not execute again. |
| 597 | invoker.Flush(Thread::Current()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 598 | EXPECT_FALSE(flag1.get()); |
| 599 | EXPECT_TRUE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 602 | class GuardedAsyncInvokeTest : public testing::Test { |
| 603 | public: |
| 604 | void IntCallback(int value) { |
| 605 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 606 | int_value_ = value; |
| 607 | } |
| 608 | void AsyncInvokeIntCallback(GuardedAsyncInvoker* invoker, Thread* thread) { |
| 609 | expected_thread_ = thread; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 610 | invoker->AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorC(), |
| 611 | &GuardedAsyncInvokeTest::IntCallback, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 612 | static_cast<GuardedAsyncInvokeTest*>(this)); |
| 613 | invoke_started_.Set(); |
| 614 | } |
| 615 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 616 | expected_thread_ = thread; |
| 617 | } |
| 618 | |
| 619 | protected: |
| 620 | const static int kWaitTimeout = 1000; |
| 621 | GuardedAsyncInvokeTest() |
| 622 | : int_value_(0), |
| 623 | invoke_started_(true, false), |
| 624 | expected_thread_(nullptr) {} |
| 625 | |
| 626 | int int_value_; |
| 627 | Event invoke_started_; |
| 628 | Thread* expected_thread_; |
| 629 | }; |
| 630 | |
| 631 | // Functor for creating an invoker. |
| 632 | struct CreateInvoker { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 633 | CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker) |
| 634 | : invoker_(invoker) {} |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 635 | void operator()() { invoker_->reset(new GuardedAsyncInvoker()); } |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 636 | std::unique_ptr<GuardedAsyncInvoker>* invoker_; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 637 | }; |
| 638 | |
| 639 | // Test that we can call AsyncInvoke<void>() after the thread died. |
| 640 | TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { |
| 641 | // Create and start the thread. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 642 | std::unique_ptr<Thread> thread(new Thread()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 643 | thread->Start(); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 644 | std::unique_ptr<GuardedAsyncInvoker> invoker; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 645 | // Create the invoker on |thread|. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 646 | thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 647 | // Kill |thread|. |
| 648 | thread = nullptr; |
| 649 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 650 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 651 | EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 652 | // With thread gone, nothing should happen. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 653 | WAIT(called.get(), kWaitTimeout); |
| 654 | EXPECT_FALSE(called.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Test that we can call AsyncInvoke with callback after the thread died. |
| 658 | TEST_F(GuardedAsyncInvokeTest, KillThreadWithCallback) { |
| 659 | // Create and start the thread. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 660 | std::unique_ptr<Thread> thread(new Thread()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 661 | thread->Start(); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 662 | std::unique_ptr<GuardedAsyncInvoker> invoker; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 663 | // Create the invoker on |thread|. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 664 | thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 665 | // Kill |thread|. |
| 666 | thread = nullptr; |
| 667 | // Try calling functor. |
| 668 | EXPECT_FALSE( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 669 | invoker->AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorC(), |
| 670 | &GuardedAsyncInvokeTest::IntCallback, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 671 | static_cast<GuardedAsyncInvokeTest*>(this))); |
| 672 | // With thread gone, callback should be cancelled. |
| 673 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 674 | EXPECT_EQ(0, int_value_); |
| 675 | } |
| 676 | |
| 677 | // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker |
| 678 | // when Thread is still alive. |
| 679 | TEST_F(GuardedAsyncInvokeTest, FireAndForget) { |
| 680 | GuardedAsyncInvoker invoker; |
| 681 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 682 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 683 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 684 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | TEST_F(GuardedAsyncInvokeTest, WithCallback) { |
| 688 | GuardedAsyncInvoker invoker; |
| 689 | // Try calling functor. |
| 690 | SetExpectedThreadForIntCallback(Thread::Current()); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 691 | EXPECT_TRUE(invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorA(), |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 692 | &GuardedAsyncInvokeTest::IntCallback, |
| 693 | static_cast<GuardedAsyncInvokeTest*>(this))); |
| 694 | EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); |
| 695 | } |
| 696 | |
| 697 | TEST_F(GuardedAsyncInvokeTest, CancelInvoker) { |
| 698 | // Try destroying invoker during call. |
| 699 | { |
| 700 | GuardedAsyncInvoker invoker; |
| 701 | EXPECT_TRUE( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 702 | invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorC(), |
| 703 | &GuardedAsyncInvokeTest::IntCallback, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 704 | static_cast<GuardedAsyncInvokeTest*>(this))); |
| 705 | } |
| 706 | // With invoker gone, callback should be cancelled. |
| 707 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 708 | EXPECT_EQ(0, int_value_); |
| 709 | } |
| 710 | |
| 711 | TEST_F(GuardedAsyncInvokeTest, CancelCallingThread) { |
| 712 | GuardedAsyncInvoker invoker; |
| 713 | // Try destroying calling thread during call. |
| 714 | { |
| 715 | Thread thread; |
| 716 | thread.Start(); |
| 717 | // Try calling functor. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 718 | thread.Invoke<void>(RTC_FROM_HERE, |
| 719 | Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 720 | static_cast<GuardedAsyncInvokeTest*>(this), |
| 721 | &invoker, Thread::Current())); |
| 722 | // Wait for the call to begin. |
| 723 | ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
| 724 | } |
| 725 | // Calling thread is gone. Return message shouldn't happen. |
| 726 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 727 | EXPECT_EQ(0, int_value_); |
| 728 | } |
| 729 | |
| 730 | TEST_F(GuardedAsyncInvokeTest, KillInvokerBeforeExecute) { |
| 731 | Thread thread; |
| 732 | thread.Start(); |
| 733 | { |
| 734 | GuardedAsyncInvoker invoker; |
| 735 | // Try calling functor. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 736 | thread.Invoke<void>(RTC_FROM_HERE, |
| 737 | Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 738 | static_cast<GuardedAsyncInvokeTest*>(this), |
| 739 | &invoker, Thread::Current())); |
| 740 | // Wait for the call to begin. |
| 741 | ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
| 742 | } |
| 743 | // Invoker is destroyed. Function should not execute. |
| 744 | Thread::Current()->ProcessMessages(kWaitTimeout); |
| 745 | EXPECT_EQ(0, int_value_); |
| 746 | } |
| 747 | |
| 748 | TEST_F(GuardedAsyncInvokeTest, Flush) { |
| 749 | GuardedAsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 750 | AtomicBool flag1; |
| 751 | AtomicBool flag2; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 752 | // Queue two async calls to the current thread. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 753 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1))); |
| 754 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 755 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 756 | EXPECT_FALSE(flag1.get()); |
| 757 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 758 | // Force them to run now. |
| 759 | EXPECT_TRUE(invoker.Flush()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 760 | EXPECT_TRUE(flag1.get()); |
| 761 | EXPECT_TRUE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | TEST_F(GuardedAsyncInvokeTest, FlushWithIds) { |
| 765 | GuardedAsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 766 | AtomicBool flag1; |
| 767 | AtomicBool flag2; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 768 | // Queue two async calls to the current thread, one with a message id. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 769 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1), 5)); |
| 770 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 771 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 772 | EXPECT_FALSE(flag1.get()); |
| 773 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 774 | // Execute pending calls with id == 5. |
| 775 | EXPECT_TRUE(invoker.Flush(5)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 776 | EXPECT_TRUE(flag1.get()); |
| 777 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 778 | flag1 = false; |
| 779 | // Execute all pending calls. The id == 5 call should not execute again. |
| 780 | EXPECT_TRUE(invoker.Flush()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 781 | EXPECT_FALSE(flag1.get()); |
| 782 | EXPECT_TRUE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 783 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 784 | |
| 785 | #if defined(WEBRTC_WIN) |
| 786 | class ComThreadTest : public testing::Test, public MessageHandler { |
| 787 | public: |
| 788 | ComThreadTest() : done_(false) {} |
| 789 | protected: |
| 790 | virtual void OnMessage(Message* message) { |
| 791 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 792 | // S_FALSE means the thread was already inited for a multithread apartment. |
| 793 | EXPECT_EQ(S_FALSE, hr); |
| 794 | if (SUCCEEDED(hr)) { |
| 795 | CoUninitialize(); |
| 796 | } |
| 797 | done_ = true; |
| 798 | } |
| 799 | bool done_; |
| 800 | }; |
| 801 | |
| 802 | TEST_F(ComThreadTest, ComInited) { |
| 803 | Thread* thread = new ComThread(); |
| 804 | EXPECT_TRUE(thread->Start()); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 805 | thread->Post(RTC_FROM_HERE, this, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 806 | EXPECT_TRUE_WAIT(done_, 1000); |
| 807 | delete thread; |
| 808 | } |
| 809 | #endif |