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 | |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 13 | #include "webrtc/rtc_base/asyncinvoker.h" |
| 14 | #include "webrtc/rtc_base/asyncudpsocket.h" |
| 15 | #include "webrtc/rtc_base/event.h" |
| 16 | #include "webrtc/rtc_base/gunit.h" |
| 17 | #include "webrtc/rtc_base/physicalsocketserver.h" |
| 18 | #include "webrtc/rtc_base/sigslot.h" |
| 19 | #include "webrtc/rtc_base/socketaddress.h" |
| 20 | #include "webrtc/rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 107 | class CustomThread : public rtc::Thread { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 153 | // rtc::AtomicInt, if/when that is added. Since the use isn't |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 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. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 198 | auto th1 = Thread::CreateWithSocketServer(); |
| 199 | Socket* socket = |
| 200 | th1->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
| 201 | MessageClient msg_client(th1.get(), socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | |
| 203 | // Create the socket client on its own thread. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 204 | auto th2 = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | AsyncSocket* asocket = |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 206 | th2->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
| 207 | SocketClient sock_client(asocket, addr, th1.get(), &msg_client); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | |
| 209 | socket->Connect(sock_client.address()); |
| 210 | |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 211 | th1->Start(); |
| 212 | th2->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 213 | |
| 214 | // Get the messages started. |
tommi | a8a3515 | 2017-07-13 05:47:25 -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). |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 224 | th1->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 225 | th_main->ProcessMessages(200); |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 226 | th2->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 239 | auto thread = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 240 | EXPECT_TRUE(thread->Start()); |
| 241 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | // Name with no object parameter |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 243 | thread = Thread::CreateWithSocketServer(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 244 | EXPECT_TRUE(thread->SetName("No object", nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 245 | EXPECT_TRUE(thread->Start()); |
| 246 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 247 | // Really long name |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 248 | thread = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); |
| 250 | EXPECT_TRUE(thread->Start()); |
| 251 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 252 | } |
| 253 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 254 | TEST(ThreadTest, Wrap) { |
| 255 | Thread* current_thread = Thread::Current(); |
| 256 | current_thread->UnwrapCurrent(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 257 | CustomThread* cthread = new CustomThread(); |
| 258 | EXPECT_TRUE(cthread->WrapCurrent()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 259 | EXPECT_TRUE(cthread->RunningForTest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | EXPECT_FALSE(cthread->IsOwned()); |
| 261 | cthread->UnwrapCurrent(); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 262 | EXPECT_FALSE(cthread->RunningForTest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 263 | delete cthread; |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 264 | current_thread->WrapCurrent(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | } |
| 266 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 267 | TEST(ThreadTest, Invoke) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | // Create and start the thread. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 269 | auto thread = Thread::CreateWithSocketServer(); |
| 270 | thread->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 271 | // Try calling functors. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 272 | EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA())); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 273 | AtomicBool called; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 274 | FunctorB f2(&called); |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 275 | thread->Invoke<void>(RTC_FROM_HERE, f2); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 276 | EXPECT_TRUE(called.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 277 | // Try calling bare functions. |
| 278 | struct LocalFuncs { |
| 279 | static int Func1() { return 999; } |
| 280 | static void Func2() {} |
| 281 | }; |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 282 | EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); |
| 283 | thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 284 | } |
| 285 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 286 | // Verifies that two threads calling Invoke on each other at the same time does |
| 287 | // not deadlock. |
| 288 | TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { |
| 289 | AutoThread thread; |
| 290 | Thread* current_thread = Thread::Current(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 291 | ASSERT_TRUE(current_thread != nullptr); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 292 | |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 293 | auto other_thread = Thread::CreateWithSocketServer(); |
| 294 | other_thread->Start(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 295 | |
| 296 | struct LocalFuncs { |
| 297 | static void Set(bool* out) { *out = true; } |
| 298 | static void InvokeSet(Thread* thread, bool* out) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 299 | thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 300 | } |
| 301 | }; |
| 302 | |
| 303 | bool called = false; |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 304 | other_thread->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 305 | RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 306 | |
| 307 | EXPECT_TRUE(called); |
| 308 | } |
| 309 | |
| 310 | // Verifies that if thread A invokes a call on thread B and thread C is trying |
| 311 | // to invoke A at the same time, thread A does not handle C's invoke while |
| 312 | // invoking B. |
| 313 | TEST(ThreadTest, ThreeThreadsInvoke) { |
| 314 | AutoThread thread; |
| 315 | Thread* thread_a = Thread::Current(); |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 316 | auto thread_b = Thread::CreateWithSocketServer(); |
| 317 | auto thread_c = Thread::CreateWithSocketServer(); |
| 318 | thread_b->Start(); |
| 319 | thread_c->Start(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 320 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 321 | class LockedBool { |
| 322 | public: |
| 323 | explicit LockedBool(bool value) : value_(value) {} |
| 324 | |
| 325 | void Set(bool value) { |
| 326 | CritScope lock(&crit_); |
| 327 | value_ = value; |
| 328 | } |
| 329 | |
| 330 | bool Get() { |
| 331 | CritScope lock(&crit_); |
| 332 | return value_; |
| 333 | } |
| 334 | |
| 335 | private: |
| 336 | CriticalSection crit_; |
| 337 | bool value_ GUARDED_BY(crit_); |
| 338 | }; |
| 339 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 340 | struct LocalFuncs { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 341 | static void Set(LockedBool* out) { out->Set(true); } |
| 342 | static void InvokeSet(Thread* thread, LockedBool* out) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 343 | thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Set |out| true and call InvokeSet on |thread|. |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 347 | static void SetAndInvokeSet(LockedBool* out, |
| 348 | Thread* thread, |
| 349 | LockedBool* out_inner) { |
| 350 | out->Set(true); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 351 | InvokeSet(thread, out_inner); |
| 352 | } |
| 353 | |
| 354 | // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until |
| 355 | // |thread1| starts the call. |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 356 | static void AsyncInvokeSetAndWait(AsyncInvoker* invoker, |
| 357 | Thread* thread1, |
| 358 | Thread* thread2, |
| 359 | LockedBool* out) { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 360 | CriticalSection crit; |
| 361 | LockedBool async_invoked(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 362 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 363 | invoker->AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 364 | RTC_FROM_HERE, thread1, |
| 365 | Bind(&SetAndInvokeSet, &async_invoked, thread2, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 366 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 367 | EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 368 | } |
| 369 | }; |
| 370 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 371 | AsyncInvoker invoker; |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 372 | LockedBool thread_a_called(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 373 | |
| 374 | // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. |
| 375 | // Thread B returns when C receives the call and C should be blocked until A |
| 376 | // starts to process messages. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 377 | thread_b->Invoke<void>(RTC_FROM_HERE, |
| 378 | Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker, |
| 379 | thread_c.get(), thread_a, &thread_a_called)); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 380 | EXPECT_FALSE(thread_a_called.Get()); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 381 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 382 | EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 383 | } |
| 384 | |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 385 | // Set the name on a thread when the underlying QueueDestroyed signal is |
| 386 | // triggered. This causes an error if the object is already partially |
| 387 | // destroyed. |
| 388 | class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { |
| 389 | public: |
| 390 | SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) { |
| 391 | thread->SignalQueueDestroyed.connect( |
| 392 | this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed); |
| 393 | } |
| 394 | |
| 395 | void OnQueueDestroyed() { |
| 396 | // Makes sure that if we access the Thread while it's being destroyed, that |
| 397 | // it doesn't cause a problem because the vtable has been modified. |
| 398 | thread_->SetName("foo", nullptr); |
| 399 | } |
| 400 | |
| 401 | private: |
| 402 | Thread* thread_; |
| 403 | }; |
| 404 | |
| 405 | TEST(ThreadTest, SetNameOnSignalQueueDestroyed) { |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 406 | auto thread1 = Thread::CreateWithSocketServer(); |
| 407 | SetNameOnSignalQueueDestroyedTester tester1(thread1.get()); |
| 408 | thread1.reset(); |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 409 | |
| 410 | Thread* thread2 = new AutoThread(); |
| 411 | SetNameOnSignalQueueDestroyedTester tester2(thread2); |
| 412 | delete thread2; |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 413 | } |
| 414 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 415 | class AsyncInvokeTest : public testing::Test { |
| 416 | public: |
| 417 | void IntCallback(int value) { |
| 418 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 419 | int_value_ = value; |
| 420 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 421 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 422 | expected_thread_ = thread; |
| 423 | } |
| 424 | |
| 425 | protected: |
| 426 | enum { kWaitTimeout = 1000 }; |
| 427 | AsyncInvokeTest() |
| 428 | : int_value_(0), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 429 | expected_thread_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 430 | |
| 431 | int int_value_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 432 | Thread* expected_thread_; |
| 433 | }; |
| 434 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 435 | TEST_F(AsyncInvokeTest, FireAndForget) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 436 | AsyncInvoker invoker; |
| 437 | // Create and start the thread. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 438 | auto thread = Thread::CreateWithSocketServer(); |
| 439 | thread->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 440 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 441 | AtomicBool called; |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 442 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 443 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 444 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 445 | } |
| 446 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 447 | TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) { |
| 448 | // Use these events to get in a state where the functor is in the middle of |
| 449 | // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE" |
| 450 | // is run. |
| 451 | Event functor_started(false, false); |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 452 | Event functor_continue(false, false); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 453 | Event functor_finished(false, false); |
| 454 | |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 455 | auto thread = Thread::CreateWithSocketServer(); |
| 456 | thread->Start(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 457 | volatile bool invoker_destroyed = false; |
| 458 | { |
| 459 | AsyncInvoker invoker; |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 460 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 461 | [&functor_started, &functor_continue, |
| 462 | &functor_finished, &invoker_destroyed] { |
| 463 | functor_started.Set(); |
| 464 | functor_continue.Wait(Event::kForever); |
| 465 | rtc::Thread::Current()->SleepMs(kWaitTimeout); |
| 466 | EXPECT_FALSE(invoker_destroyed); |
| 467 | functor_finished.Set(); |
| 468 | }); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 469 | functor_started.Wait(Event::kForever); |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 470 | |
| 471 | // Allow the functor to continue and immediately destroy the invoker. |
| 472 | functor_continue.Set(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // If the destructor DIDN'T wait for the functor to finish executing, it will |
| 476 | // hit the EXPECT_FALSE(invoker_destroyed) after it finishes sleeping for a |
| 477 | // second. |
| 478 | invoker_destroyed = true; |
| 479 | functor_finished.Wait(Event::kForever); |
| 480 | } |
| 481 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 482 | TEST_F(AsyncInvokeTest, Flush) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 483 | AsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 484 | AtomicBool flag1; |
| 485 | AtomicBool flag2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 486 | // Queue two async calls to the current thread. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 487 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1)); |
| 488 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 489 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 490 | EXPECT_FALSE(flag1.get()); |
| 491 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 492 | // Force them to run now. |
| 493 | invoker.Flush(Thread::Current()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 494 | EXPECT_TRUE(flag1.get()); |
| 495 | EXPECT_TRUE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 496 | } |
| 497 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 498 | TEST_F(AsyncInvokeTest, FlushWithIds) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 499 | AsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 500 | AtomicBool flag1; |
| 501 | AtomicBool flag2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 502 | // 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] | 503 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 504 | 5); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 505 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 506 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 507 | EXPECT_FALSE(flag1.get()); |
| 508 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 509 | // Execute pending calls with id == 5. |
| 510 | invoker.Flush(Thread::Current(), 5); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 511 | EXPECT_TRUE(flag1.get()); |
| 512 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 513 | flag1 = false; |
| 514 | // Execute all pending calls. The id == 5 call should not execute again. |
| 515 | invoker.Flush(Thread::Current()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 516 | EXPECT_FALSE(flag1.get()); |
| 517 | EXPECT_TRUE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 520 | class GuardedAsyncInvokeTest : public testing::Test { |
| 521 | public: |
| 522 | void IntCallback(int value) { |
| 523 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 524 | int_value_ = value; |
| 525 | } |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 526 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 527 | expected_thread_ = thread; |
| 528 | } |
| 529 | |
| 530 | protected: |
| 531 | const static int kWaitTimeout = 1000; |
| 532 | GuardedAsyncInvokeTest() |
| 533 | : int_value_(0), |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 534 | expected_thread_(nullptr) {} |
| 535 | |
| 536 | int int_value_; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 537 | Thread* expected_thread_; |
| 538 | }; |
| 539 | |
| 540 | // Functor for creating an invoker. |
| 541 | struct CreateInvoker { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 542 | CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker) |
| 543 | : invoker_(invoker) {} |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 544 | void operator()() { invoker_->reset(new GuardedAsyncInvoker()); } |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 545 | std::unique_ptr<GuardedAsyncInvoker>* invoker_; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | // Test that we can call AsyncInvoke<void>() after the thread died. |
| 549 | TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { |
| 550 | // Create and start the thread. |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 551 | std::unique_ptr<Thread> thread(Thread::Create()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 552 | thread->Start(); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 553 | std::unique_ptr<GuardedAsyncInvoker> invoker; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 554 | // Create the invoker on |thread|. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 555 | thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 556 | // Kill |thread|. |
| 557 | thread = nullptr; |
| 558 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 559 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 560 | EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 561 | // With thread gone, nothing should happen. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 562 | WAIT(called.get(), kWaitTimeout); |
| 563 | EXPECT_FALSE(called.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 566 | // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker |
| 567 | // when Thread is still alive. |
| 568 | TEST_F(GuardedAsyncInvokeTest, FireAndForget) { |
| 569 | GuardedAsyncInvoker invoker; |
| 570 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 571 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 572 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 573 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 576 | TEST_F(GuardedAsyncInvokeTest, Flush) { |
| 577 | GuardedAsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 578 | AtomicBool flag1; |
| 579 | AtomicBool flag2; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 580 | // Queue two async calls to the current thread. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 581 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1))); |
| 582 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 583 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 584 | EXPECT_FALSE(flag1.get()); |
| 585 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 586 | // Force them to run now. |
| 587 | EXPECT_TRUE(invoker.Flush()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 588 | EXPECT_TRUE(flag1.get()); |
| 589 | EXPECT_TRUE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | TEST_F(GuardedAsyncInvokeTest, FlushWithIds) { |
| 593 | GuardedAsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 594 | AtomicBool flag1; |
| 595 | AtomicBool flag2; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 596 | // 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] | 597 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1), 5)); |
| 598 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 599 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 600 | EXPECT_FALSE(flag1.get()); |
| 601 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 602 | // Execute pending calls with id == 5. |
| 603 | EXPECT_TRUE(invoker.Flush(5)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 604 | EXPECT_TRUE(flag1.get()); |
| 605 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 606 | flag1 = false; |
| 607 | // Execute all pending calls. The id == 5 call should not execute again. |
| 608 | EXPECT_TRUE(invoker.Flush()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 609 | EXPECT_FALSE(flag1.get()); |
| 610 | EXPECT_TRUE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 611 | } |