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