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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "rtc_base/thread.h" |
| 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 15 | #include "api/task_queue/task_queue_factory.h" |
| 16 | #include "api/task_queue/task_queue_test.h" |
Artem Titov | c374d11 | 2022-06-16 21:27:45 +0200 | [diff] [blame] | 17 | #include "api/task_queue/to_queued_task.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/async_invoker.h" |
| 19 | #include "rtc_base/async_udp_socket.h" |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/event.h" |
| 22 | #include "rtc_base/gunit.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 10:41:01 +0100 | [diff] [blame] | 23 | #include "rtc_base/internal/default_socket_server.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/null_socket_server.h" |
| 25 | #include "rtc_base/physical_socket_server.h" |
| 26 | #include "rtc_base/socket_address.h" |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 27 | #include "rtc_base/synchronization/mutex.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 28 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 29 | #include "test/testsupport/rtc_expect_death.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | |
| 31 | #if defined(WEBRTC_WIN) |
| 32 | #include <comdef.h> // NOLINT |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 33 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 36 | namespace rtc { |
| 37 | namespace { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 39 | using ::webrtc::ToQueuedTask; |
| 40 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | // Generates a sequence of numbers (collaboratively). |
| 42 | class TestGenerator { |
| 43 | public: |
| 44 | TestGenerator() : last(0), count(0) {} |
| 45 | |
| 46 | int Next(int prev) { |
| 47 | int result = prev + last; |
| 48 | last = result; |
| 49 | count += 1; |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | int last; |
| 54 | int count; |
| 55 | }; |
| 56 | |
| 57 | struct TestMessage : public MessageData { |
| 58 | explicit TestMessage(int v) : value(v) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | |
| 60 | int value; |
| 61 | }; |
| 62 | |
| 63 | // Receives on a socket and sends by posting messages. |
| 64 | class SocketClient : public TestGenerator, public sigslot::has_slots<> { |
| 65 | public: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 66 | SocketClient(Socket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | const SocketAddress& addr, |
| 68 | Thread* post_thread, |
| 69 | MessageHandler* phandler) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | : socket_(AsyncUDPSocket::Create(socket, addr)), |
| 71 | post_thread_(post_thread), |
| 72 | post_handler_(phandler) { |
| 73 | socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket); |
| 74 | } |
| 75 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 76 | ~SocketClient() override { delete socket_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | |
| 78 | SocketAddress address() const { return socket_->GetLocalAddress(); } |
| 79 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 80 | void OnPacket(AsyncPacketSocket* socket, |
| 81 | const char* buf, |
| 82 | size_t size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 84 | const int64_t& packet_time_us) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 85 | EXPECT_EQ(size, sizeof(uint32_t)); |
| 86 | uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0]; |
| 87 | uint32_t result = Next(prev); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 89 | post_thread_->PostDelayed(RTC_FROM_HERE, 200, post_handler_, 0, |
| 90 | new TestMessage(result)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | AsyncUDPSocket* socket_; |
| 95 | Thread* post_thread_; |
| 96 | MessageHandler* post_handler_; |
| 97 | }; |
| 98 | |
| 99 | // Receives messages and sends on a socket. |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 100 | class MessageClient : public MessageHandlerAutoCleanup, public TestGenerator { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | MessageClient(Thread* pth, Socket* socket) : socket_(socket) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 104 | ~MessageClient() override { delete socket_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 106 | void OnMessage(Message* pmsg) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata); |
| 108 | int result = Next(msg->value); |
| 109 | EXPECT_GE(socket_->Send(&result, sizeof(result)), 0); |
| 110 | delete msg; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | Socket* socket_; |
| 115 | }; |
| 116 | |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 117 | class CustomThread : public rtc::Thread { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | public: |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 119 | CustomThread() |
| 120 | : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())) {} |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 121 | ~CustomThread() override { Stop(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | bool Start() { return false; } |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 123 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 124 | bool WrapCurrent() { return Thread::WrapCurrent(); } |
| 125 | void UnwrapCurrent() { Thread::UnwrapCurrent(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | // A thread that does nothing when it runs and signals an event |
| 129 | // when it is destroyed. |
| 130 | class SignalWhenDestroyedThread : public Thread { |
| 131 | public: |
| 132 | SignalWhenDestroyedThread(Event* event) |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 133 | : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())), |
| 134 | event_(event) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 136 | ~SignalWhenDestroyedThread() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | Stop(); |
| 138 | event_->Set(); |
| 139 | } |
| 140 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 141 | void Run() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | // Do nothing. |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | Event* event_; |
| 147 | }; |
| 148 | |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 149 | // A bool wrapped in a mutex, to avoid data races. Using a volatile |
| 150 | // bool should be sufficient for correct code ("eventual consistency" |
| 151 | // between caches is sufficient), but we can't tell the compiler about |
| 152 | // that, and then tsan complains about a data race. |
| 153 | |
| 154 | // See also discussion at |
| 155 | // http://stackoverflow.com/questions/7223164/is-mutex-needed-to-synchronize-a-simple-flag-between-pthreads |
| 156 | |
| 157 | // Using std::atomic<bool> or std::atomic_flag in C++11 is probably |
| 158 | // the right thing to do, but those features are not yet allowed. Or |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 159 | // rtc::AtomicInt, if/when that is added. Since the use isn't |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 160 | // performance critical, use a plain critical section for the time |
| 161 | // being. |
| 162 | |
| 163 | class AtomicBool { |
| 164 | public: |
| 165 | explicit AtomicBool(bool value = false) : flag_(value) {} |
| 166 | AtomicBool& operator=(bool value) { |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 167 | webrtc::MutexLock scoped_lock(&mutex_); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 168 | flag_ = value; |
| 169 | return *this; |
| 170 | } |
| 171 | bool get() const { |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 172 | webrtc::MutexLock scoped_lock(&mutex_); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 173 | return flag_; |
| 174 | } |
| 175 | |
| 176 | private: |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 177 | mutable webrtc::Mutex mutex_; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 178 | bool flag_; |
| 179 | }; |
| 180 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 181 | // Function objects to test Thread::Invoke. |
| 182 | struct FunctorA { |
| 183 | int operator()() { return 42; } |
| 184 | }; |
| 185 | class FunctorB { |
| 186 | public: |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 187 | explicit FunctorB(AtomicBool* flag) : flag_(flag) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 188 | void operator()() { |
| 189 | if (flag_) |
| 190 | *flag_ = true; |
| 191 | } |
| 192 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 193 | private: |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 194 | AtomicBool* flag_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 195 | }; |
| 196 | struct FunctorC { |
| 197 | int operator()() { |
| 198 | Thread::Current()->ProcessMessages(50); |
| 199 | return 24; |
| 200 | } |
| 201 | }; |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 202 | struct FunctorD { |
| 203 | public: |
| 204 | explicit FunctorD(AtomicBool* flag) : flag_(flag) {} |
| 205 | FunctorD(FunctorD&&) = default; |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 206 | |
| 207 | FunctorD(const FunctorD&) = delete; |
| 208 | FunctorD& operator=(const FunctorD&) = delete; |
| 209 | |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 210 | FunctorD& operator=(FunctorD&&) = default; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 211 | void operator()() { |
| 212 | if (flag_) |
| 213 | *flag_ = true; |
| 214 | } |
| 215 | |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 216 | private: |
| 217 | AtomicBool* flag_; |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 218 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 219 | |
| 220 | // See: https://code.google.com/p/webrtc/issues/detail?id=2409 |
| 221 | TEST(ThreadTest, DISABLED_Main) { |
| 222 | const SocketAddress addr("127.0.0.1", 0); |
| 223 | |
| 224 | // Create the messaging client on its own thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 225 | auto th1 = Thread::CreateWithSocketServer(); |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 226 | Socket* socket = th1->socketserver()->CreateSocket(addr.family(), SOCK_DGRAM); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 227 | MessageClient msg_client(th1.get(), socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 228 | |
| 229 | // Create the socket client on its own thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 230 | auto th2 = Thread::CreateWithSocketServer(); |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 231 | Socket* asocket = |
| 232 | th2->socketserver()->CreateSocket(addr.family(), SOCK_DGRAM); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 233 | SocketClient sock_client(asocket, addr, th1.get(), &msg_client); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 234 | |
| 235 | socket->Connect(sock_client.address()); |
| 236 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 237 | th1->Start(); |
| 238 | th2->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | |
| 240 | // Get the messages started. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 241 | 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] | 242 | |
| 243 | // Give the clients a little while to run. |
| 244 | // Messages will be processed at 100, 300, 500, 700, 900. |
| 245 | Thread* th_main = Thread::Current(); |
| 246 | th_main->ProcessMessages(1000); |
| 247 | |
| 248 | // Stop the sending client. Give the receiver a bit longer to run, in case |
| 249 | // 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] | 250 | th1->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 251 | th_main->ProcessMessages(200); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 252 | th2->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 253 | |
| 254 | // Make sure the results were correct |
| 255 | EXPECT_EQ(5, msg_client.count); |
| 256 | EXPECT_EQ(34, msg_client.last); |
| 257 | EXPECT_EQ(5, sock_client.count); |
| 258 | EXPECT_EQ(55, sock_client.last); |
| 259 | } |
| 260 | |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 261 | TEST(ThreadTest, CountBlockingCalls) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 262 | rtc::AutoThread current; |
| 263 | |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 264 | // When the test runs, this will print out: |
| 265 | // (thread_unittest.cc:262): Blocking TestBody: total=2 (actual=1, could=1) |
| 266 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 267 | #if RTC_DCHECK_IS_ON |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 268 | rtc::Thread::ScopedCountBlockingCalls blocked_calls( |
| 269 | [&](uint32_t actual_block, uint32_t could_block) { |
| 270 | EXPECT_EQ(1u, actual_block); |
| 271 | EXPECT_EQ(1u, could_block); |
| 272 | }); |
| 273 | |
| 274 | EXPECT_EQ(0u, blocked_calls.GetBlockingCallCount()); |
| 275 | EXPECT_EQ(0u, blocked_calls.GetCouldBeBlockingCallCount()); |
| 276 | EXPECT_EQ(0u, blocked_calls.GetTotalBlockedCallCount()); |
| 277 | |
| 278 | // Test invoking on the current thread. This should not count as an 'actual' |
| 279 | // invoke, but should still count as an invoke that could block since we |
| 280 | // that the call to Invoke serves a purpose in some configurations (and should |
| 281 | // not be used a general way to call methods on the same thread). |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 282 | current.Invoke<void>(RTC_FROM_HERE, []() {}); |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 283 | EXPECT_EQ(0u, blocked_calls.GetBlockingCallCount()); |
| 284 | EXPECT_EQ(1u, blocked_calls.GetCouldBeBlockingCallCount()); |
| 285 | EXPECT_EQ(1u, blocked_calls.GetTotalBlockedCallCount()); |
| 286 | |
| 287 | // Create a new thread to invoke on. |
| 288 | auto thread = Thread::CreateWithSocketServer(); |
| 289 | thread->Start(); |
| 290 | EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, []() { return 42; })); |
| 291 | EXPECT_EQ(1u, blocked_calls.GetBlockingCallCount()); |
| 292 | EXPECT_EQ(1u, blocked_calls.GetCouldBeBlockingCallCount()); |
| 293 | EXPECT_EQ(2u, blocked_calls.GetTotalBlockedCallCount()); |
| 294 | thread->Stop(); |
| 295 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
| 296 | #else |
| 297 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); |
| 298 | RTC_LOG(LS_INFO) << "Test not active in this config"; |
| 299 | #endif |
| 300 | } |
| 301 | |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 302 | #if RTC_DCHECK_IS_ON |
| 303 | TEST(ThreadTest, CountBlockingCallsOneCallback) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 304 | rtc::AutoThread current; |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 305 | bool was_called_back = false; |
| 306 | { |
| 307 | rtc::Thread::ScopedCountBlockingCalls blocked_calls( |
| 308 | [&](uint32_t actual_block, uint32_t could_block) { |
| 309 | was_called_back = true; |
| 310 | }); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 311 | current.Invoke<void>(RTC_FROM_HERE, []() {}); |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 312 | } |
| 313 | EXPECT_TRUE(was_called_back); |
| 314 | } |
| 315 | |
| 316 | TEST(ThreadTest, CountBlockingCallsSkipCallback) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 317 | rtc::AutoThread current; |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 318 | bool was_called_back = false; |
| 319 | { |
| 320 | rtc::Thread::ScopedCountBlockingCalls blocked_calls( |
| 321 | [&](uint32_t actual_block, uint32_t could_block) { |
| 322 | was_called_back = true; |
| 323 | }); |
| 324 | // Changed `blocked_calls` to not issue the callback if there are 1 or |
| 325 | // fewer blocking calls (i.e. we set the minimum required number to 2). |
| 326 | blocked_calls.set_minimum_call_count_for_callback(2); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 327 | current.Invoke<void>(RTC_FROM_HERE, []() {}); |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 328 | } |
| 329 | // We should not have gotten a call back. |
| 330 | EXPECT_FALSE(was_called_back); |
| 331 | } |
| 332 | #endif |
| 333 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 334 | // Test that setting thread names doesn't cause a malfunction. |
| 335 | // 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] | 336 | TEST(ThreadTest, Names) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 337 | // Default name |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 338 | auto thread = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 339 | EXPECT_TRUE(thread->Start()); |
| 340 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 341 | // Name with no object parameter |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 342 | thread = Thread::CreateWithSocketServer(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 343 | EXPECT_TRUE(thread->SetName("No object", nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 344 | EXPECT_TRUE(thread->Start()); |
| 345 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 346 | // Really long name |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 347 | thread = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 348 | EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); |
| 349 | EXPECT_TRUE(thread->Start()); |
| 350 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 351 | } |
| 352 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 353 | TEST(ThreadTest, Wrap) { |
| 354 | Thread* current_thread = Thread::Current(); |
Niels Möller | 5a8f860 | 2019-06-12 11:30:59 +0200 | [diff] [blame] | 355 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
| 356 | |
| 357 | { |
| 358 | CustomThread cthread; |
| 359 | EXPECT_TRUE(cthread.WrapCurrent()); |
| 360 | EXPECT_EQ(&cthread, Thread::Current()); |
| 361 | EXPECT_TRUE(cthread.RunningForTest()); |
| 362 | EXPECT_FALSE(cthread.IsOwned()); |
| 363 | cthread.UnwrapCurrent(); |
| 364 | EXPECT_FALSE(cthread.RunningForTest()); |
| 365 | } |
| 366 | ThreadManager::Instance()->SetCurrentThread(current_thread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 369 | #if (!defined(NDEBUG) || RTC_DCHECK_IS_ON) |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 370 | TEST(ThreadTest, InvokeToThreadAllowedReturnsTrueWithoutPolicies) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 371 | rtc::AutoThread main_thread; |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 372 | // Create and start the thread. |
| 373 | auto thread1 = Thread::CreateWithSocketServer(); |
| 374 | auto thread2 = Thread::CreateWithSocketServer(); |
| 375 | |
| 376 | thread1->PostTask(ToQueuedTask( |
| 377 | [&]() { EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread2.get())); })); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 378 | main_thread.ProcessMessages(100); |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | TEST(ThreadTest, InvokeAllowedWhenThreadsAdded) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 382 | rtc::AutoThread main_thread; |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 383 | // Create and start the thread. |
| 384 | auto thread1 = Thread::CreateWithSocketServer(); |
| 385 | auto thread2 = Thread::CreateWithSocketServer(); |
| 386 | auto thread3 = Thread::CreateWithSocketServer(); |
| 387 | auto thread4 = Thread::CreateWithSocketServer(); |
| 388 | |
| 389 | thread1->AllowInvokesToThread(thread2.get()); |
| 390 | thread1->AllowInvokesToThread(thread3.get()); |
| 391 | |
| 392 | thread1->PostTask(ToQueuedTask([&]() { |
| 393 | EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread2.get())); |
| 394 | EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread3.get())); |
| 395 | EXPECT_FALSE(thread1->IsInvokeToThreadAllowed(thread4.get())); |
| 396 | })); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 397 | main_thread.ProcessMessages(100); |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | TEST(ThreadTest, InvokesDisallowedWhenDisallowAllInvokes) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 401 | rtc::AutoThread main_thread; |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 402 | // Create and start the thread. |
| 403 | auto thread1 = Thread::CreateWithSocketServer(); |
| 404 | auto thread2 = Thread::CreateWithSocketServer(); |
| 405 | |
| 406 | thread1->DisallowAllInvokes(); |
| 407 | |
| 408 | thread1->PostTask(ToQueuedTask([&]() { |
| 409 | EXPECT_FALSE(thread1->IsInvokeToThreadAllowed(thread2.get())); |
| 410 | })); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 411 | main_thread.ProcessMessages(100); |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 412 | } |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 413 | #endif // (!defined(NDEBUG) || RTC_DCHECK_IS_ON) |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 414 | |
| 415 | TEST(ThreadTest, InvokesAllowedByDefault) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 416 | rtc::AutoThread main_thread; |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 417 | // Create and start the thread. |
| 418 | auto thread1 = Thread::CreateWithSocketServer(); |
| 419 | auto thread2 = Thread::CreateWithSocketServer(); |
| 420 | |
| 421 | thread1->PostTask(ToQueuedTask( |
| 422 | [&]() { EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread2.get())); })); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 423 | main_thread.ProcessMessages(100); |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 424 | } |
| 425 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 426 | TEST(ThreadTest, Invoke) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 427 | // Create and start the thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 428 | auto thread = Thread::CreateWithSocketServer(); |
| 429 | thread->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 430 | // Try calling functors. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 431 | EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA())); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 432 | AtomicBool called; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 433 | FunctorB f2(&called); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 434 | thread->Invoke<void>(RTC_FROM_HERE, f2); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 435 | EXPECT_TRUE(called.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 436 | // Try calling bare functions. |
| 437 | struct LocalFuncs { |
| 438 | static int Func1() { return 999; } |
| 439 | static void Func2() {} |
| 440 | }; |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 441 | EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); |
| 442 | thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 443 | } |
| 444 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 445 | // Verifies that two threads calling Invoke on each other at the same time does |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 446 | // not deadlock but crash. |
| 447 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 448 | TEST(ThreadTest, TwoThreadsInvokeDeathTest) { |
Mirko Bonadei | 386b5c3 | 2021-07-28 08:55:52 +0200 | [diff] [blame] | 449 | GTEST_FLAG_SET(death_test_style, "threadsafe"); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 450 | AutoThread thread; |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 451 | Thread* main_thread = Thread::Current(); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 452 | auto other_thread = Thread::CreateWithSocketServer(); |
| 453 | other_thread->Start(); |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 454 | other_thread->Invoke<void>(RTC_FROM_HERE, [main_thread] { |
| 455 | RTC_EXPECT_DEATH(main_thread->Invoke<void>(RTC_FROM_HERE, [] {}), "loop"); |
| 456 | }); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 459 | TEST(ThreadTest, ThreeThreadsInvokeDeathTest) { |
Mirko Bonadei | 386b5c3 | 2021-07-28 08:55:52 +0200 | [diff] [blame] | 460 | GTEST_FLAG_SET(death_test_style, "threadsafe"); |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 461 | AutoThread thread; |
| 462 | Thread* first = Thread::Current(); |
| 463 | |
| 464 | auto second = Thread::Create(); |
| 465 | second->Start(); |
| 466 | auto third = Thread::Create(); |
| 467 | third->Start(); |
| 468 | |
| 469 | second->Invoke<void>(RTC_FROM_HERE, [&] { |
| 470 | third->Invoke<void>(RTC_FROM_HERE, [&] { |
| 471 | RTC_EXPECT_DEATH(first->Invoke<void>(RTC_FROM_HERE, [] {}), "loop"); |
| 472 | }); |
| 473 | }); |
| 474 | } |
| 475 | |
| 476 | #endif |
| 477 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 478 | // Verifies that if thread A invokes a call on thread B and thread C is trying |
| 479 | // to invoke A at the same time, thread A does not handle C's invoke while |
| 480 | // invoking B. |
| 481 | TEST(ThreadTest, ThreeThreadsInvoke) { |
| 482 | AutoThread thread; |
| 483 | Thread* thread_a = Thread::Current(); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 484 | auto thread_b = Thread::CreateWithSocketServer(); |
| 485 | auto thread_c = Thread::CreateWithSocketServer(); |
| 486 | thread_b->Start(); |
| 487 | thread_c->Start(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 488 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 489 | class LockedBool { |
| 490 | public: |
| 491 | explicit LockedBool(bool value) : value_(value) {} |
| 492 | |
| 493 | void Set(bool value) { |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 494 | webrtc::MutexLock lock(&mutex_); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 495 | value_ = value; |
| 496 | } |
| 497 | |
| 498 | bool Get() { |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 499 | webrtc::MutexLock lock(&mutex_); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 500 | return value_; |
| 501 | } |
| 502 | |
| 503 | private: |
Markus Handell | 4ab7dde | 2020-07-10 13:23:25 +0200 | [diff] [blame] | 504 | webrtc::Mutex mutex_; |
| 505 | bool value_ RTC_GUARDED_BY(mutex_); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 508 | struct LocalFuncs { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 509 | static void Set(LockedBool* out) { out->Set(true); } |
| 510 | static void InvokeSet(Thread* thread, LockedBool* out) { |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 511 | thread->Invoke<void>(RTC_FROM_HERE, [out] { Set(out); }); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 514 | // Set `out` true and call InvokeSet on `thread`. |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 515 | static void SetAndInvokeSet(LockedBool* out, |
| 516 | Thread* thread, |
| 517 | LockedBool* out_inner) { |
| 518 | out->Set(true); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 519 | InvokeSet(thread, out_inner); |
| 520 | } |
| 521 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 522 | // Asynchronously invoke SetAndInvokeSet on `thread1` and wait until |
| 523 | // `thread1` starts the call. |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame] | 524 | static void AsyncInvokeSetAndWait(DEPRECATED_AsyncInvoker* invoker, |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 525 | Thread* thread1, |
| 526 | Thread* thread2, |
| 527 | LockedBool* out) { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 528 | LockedBool async_invoked(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 529 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 530 | invoker->AsyncInvoke<void>( |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 531 | RTC_FROM_HERE, thread1, [&async_invoked, thread2, out] { |
| 532 | SetAndInvokeSet(&async_invoked, thread2, out); |
| 533 | }); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 534 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 535 | EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 536 | } |
| 537 | }; |
| 538 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame] | 539 | DEPRECATED_AsyncInvoker invoker; |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 540 | LockedBool thread_a_called(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 541 | |
| 542 | // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. |
| 543 | // Thread B returns when C receives the call and C should be blocked until A |
| 544 | // starts to process messages. |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 545 | Thread* thread_c_ptr = thread_c.get(); |
| 546 | thread_b->Invoke<void>( |
| 547 | RTC_FROM_HERE, [&invoker, thread_c_ptr, thread_a, &thread_a_called] { |
| 548 | LocalFuncs::AsyncInvokeSetAndWait(&invoker, thread_c_ptr, thread_a, |
| 549 | &thread_a_called); |
| 550 | }); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 551 | EXPECT_FALSE(thread_a_called.Get()); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 552 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 553 | EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 556 | class ThreadQueueTest : public ::testing::Test, public Thread { |
| 557 | public: |
Mirko Bonadei | e5f4c6b | 2021-01-15 10:41:01 +0100 | [diff] [blame] | 558 | ThreadQueueTest() : Thread(CreateDefaultSocketServer(), true) {} |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 559 | bool IsLocked_Worker() { |
| 560 | if (!CritForTest()->TryEnter()) { |
| 561 | return true; |
| 562 | } |
| 563 | CritForTest()->Leave(); |
| 564 | return false; |
| 565 | } |
| 566 | bool IsLocked() { |
| 567 | // We have to do this on a worker thread, or else the TryEnter will |
| 568 | // succeed, since our critical sections are reentrant. |
| 569 | std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer()); |
| 570 | worker->Start(); |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 571 | return worker->Invoke<bool>(RTC_FROM_HERE, |
| 572 | [this] { return IsLocked_Worker(); }); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 573 | } |
| 574 | }; |
| 575 | |
| 576 | struct DeletedLockChecker { |
| 577 | DeletedLockChecker(ThreadQueueTest* test, bool* was_locked, bool* deleted) |
| 578 | : test(test), was_locked(was_locked), deleted(deleted) {} |
| 579 | ~DeletedLockChecker() { |
| 580 | *deleted = true; |
| 581 | *was_locked = test->IsLocked(); |
| 582 | } |
| 583 | ThreadQueueTest* test; |
| 584 | bool* was_locked; |
| 585 | bool* deleted; |
| 586 | }; |
| 587 | |
| 588 | static void DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(Thread* q) { |
| 589 | EXPECT_TRUE(q != nullptr); |
| 590 | int64_t now = TimeMillis(); |
| 591 | q->PostAt(RTC_FROM_HERE, now, nullptr, 3); |
| 592 | q->PostAt(RTC_FROM_HERE, now - 2, nullptr, 0); |
| 593 | q->PostAt(RTC_FROM_HERE, now - 1, nullptr, 1); |
| 594 | q->PostAt(RTC_FROM_HERE, now, nullptr, 4); |
| 595 | q->PostAt(RTC_FROM_HERE, now - 1, nullptr, 2); |
| 596 | |
| 597 | Message msg; |
| 598 | for (size_t i = 0; i < 5; ++i) { |
| 599 | memset(&msg, 0, sizeof(msg)); |
| 600 | EXPECT_TRUE(q->Get(&msg, 0)); |
| 601 | EXPECT_EQ(i, msg.message_id); |
| 602 | } |
| 603 | |
| 604 | EXPECT_FALSE(q->Get(&msg, 0)); // No more messages |
| 605 | } |
| 606 | |
| 607 | TEST_F(ThreadQueueTest, DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder) { |
Mirko Bonadei | e5f4c6b | 2021-01-15 10:41:01 +0100 | [diff] [blame] | 608 | Thread q(CreateDefaultSocketServer(), true); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 609 | DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(&q); |
| 610 | |
| 611 | NullSocketServer nullss; |
| 612 | Thread q_nullss(&nullss, true); |
| 613 | DelayedPostsWithIdenticalTimesAreProcessedInFifoOrder(&q_nullss); |
| 614 | } |
| 615 | |
| 616 | TEST_F(ThreadQueueTest, DisposeNotLocked) { |
| 617 | bool was_locked = true; |
| 618 | bool deleted = false; |
| 619 | DeletedLockChecker* d = new DeletedLockChecker(this, &was_locked, &deleted); |
| 620 | Dispose(d); |
| 621 | Message msg; |
| 622 | EXPECT_FALSE(Get(&msg, 0)); |
| 623 | EXPECT_TRUE(deleted); |
| 624 | EXPECT_FALSE(was_locked); |
| 625 | } |
| 626 | |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 627 | class DeletedMessageHandler : public MessageHandlerAutoCleanup { |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 628 | public: |
| 629 | explicit DeletedMessageHandler(bool* deleted) : deleted_(deleted) {} |
| 630 | ~DeletedMessageHandler() override { *deleted_ = true; } |
| 631 | void OnMessage(Message* msg) override {} |
| 632 | |
| 633 | private: |
| 634 | bool* deleted_; |
| 635 | }; |
| 636 | |
| 637 | TEST_F(ThreadQueueTest, DiposeHandlerWithPostedMessagePending) { |
| 638 | bool deleted = false; |
| 639 | DeletedMessageHandler* handler = new DeletedMessageHandler(&deleted); |
| 640 | // First, post a dispose. |
| 641 | Dispose(handler); |
| 642 | // Now, post a message, which should *not* be returned by Get(). |
| 643 | Post(RTC_FROM_HERE, handler, 1); |
| 644 | Message msg; |
| 645 | EXPECT_FALSE(Get(&msg, 0)); |
| 646 | EXPECT_TRUE(deleted); |
| 647 | } |
| 648 | |
| 649 | // Ensure that ProcessAllMessageQueues does its essential function; process |
| 650 | // all messages (both delayed and non delayed) up until the current time, on |
| 651 | // all registered message queues. |
| 652 | TEST(ThreadManager, ProcessAllMessageQueues) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 653 | rtc::AutoThread main_thread; |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 654 | Event entered_process_all_message_queues(true, false); |
| 655 | auto a = Thread::CreateWithSocketServer(); |
| 656 | auto b = Thread::CreateWithSocketServer(); |
| 657 | a->Start(); |
| 658 | b->Start(); |
| 659 | |
Niels Möller | 7a66900 | 2022-06-27 09:47:02 +0200 | [diff] [blame] | 660 | std::atomic<int> messages_processed(0); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 661 | auto incrementer = [&messages_processed, |
| 662 | &entered_process_all_message_queues] { |
| 663 | // Wait for event as a means to ensure Increment doesn't occur outside |
| 664 | // of ProcessAllMessageQueues. The event is set by a message posted to |
| 665 | // the main thread, which is guaranteed to be handled inside |
| 666 | // ProcessAllMessageQueues. |
| 667 | entered_process_all_message_queues.Wait(Event::kForever); |
Niels Möller | 7a66900 | 2022-06-27 09:47:02 +0200 | [diff] [blame] | 668 | messages_processed.fetch_add(1); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 669 | }; |
| 670 | auto event_signaler = [&entered_process_all_message_queues] { |
| 671 | entered_process_all_message_queues.Set(); |
| 672 | }; |
| 673 | |
| 674 | // Post messages (both delayed and non delayed) to both threads. |
| 675 | a->PostTask(ToQueuedTask(incrementer)); |
| 676 | b->PostTask(ToQueuedTask(incrementer)); |
| 677 | a->PostDelayedTask(ToQueuedTask(incrementer), 0); |
| 678 | b->PostDelayedTask(ToQueuedTask(incrementer), 0); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 679 | main_thread.PostTask(ToQueuedTask(event_signaler)); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 680 | |
| 681 | ThreadManager::ProcessAllMessageQueuesForTesting(); |
Niels Möller | 7a66900 | 2022-06-27 09:47:02 +0200 | [diff] [blame] | 682 | EXPECT_EQ(4, messages_processed.load(std::memory_order_acquire)); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // Test that ProcessAllMessageQueues doesn't hang if a thread is quitting. |
| 686 | TEST(ThreadManager, ProcessAllMessageQueuesWithQuittingThread) { |
| 687 | auto t = Thread::CreateWithSocketServer(); |
| 688 | t->Start(); |
| 689 | t->Quit(); |
| 690 | ThreadManager::ProcessAllMessageQueuesForTesting(); |
| 691 | } |
| 692 | |
| 693 | // Test that ProcessAllMessageQueues doesn't hang if a queue clears its |
| 694 | // messages. |
| 695 | TEST(ThreadManager, ProcessAllMessageQueuesWithClearedQueue) { |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 696 | rtc::AutoThread main_thread; |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 697 | Event entered_process_all_message_queues(true, false); |
| 698 | auto t = Thread::CreateWithSocketServer(); |
| 699 | t->Start(); |
| 700 | |
| 701 | auto clearer = [&entered_process_all_message_queues] { |
| 702 | // Wait for event as a means to ensure Clear doesn't occur outside of |
| 703 | // ProcessAllMessageQueues. The event is set by a message posted to the |
| 704 | // main thread, which is guaranteed to be handled inside |
| 705 | // ProcessAllMessageQueues. |
| 706 | entered_process_all_message_queues.Wait(Event::kForever); |
| 707 | rtc::Thread::Current()->Clear(nullptr); |
| 708 | }; |
| 709 | auto event_signaler = [&entered_process_all_message_queues] { |
| 710 | entered_process_all_message_queues.Set(); |
| 711 | }; |
| 712 | |
| 713 | // Post messages (both delayed and non delayed) to both threads. |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame] | 714 | t->PostTask(clearer); |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 715 | main_thread.PostTask(event_signaler); |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 716 | ThreadManager::ProcessAllMessageQueuesForTesting(); |
| 717 | } |
| 718 | |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 719 | class RefCountedHandler : public MessageHandlerAutoCleanup, |
| 720 | public rtc::RefCountInterface { |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 721 | public: |
| 722 | void OnMessage(Message* msg) override {} |
| 723 | }; |
| 724 | |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 725 | class EmptyHandler : public MessageHandlerAutoCleanup { |
Sebastian Jansson | 7338782 | 2020-01-16 11:15:35 +0100 | [diff] [blame] | 726 | public: |
| 727 | void OnMessage(Message* msg) override {} |
| 728 | }; |
| 729 | |
| 730 | TEST(ThreadManager, ClearReentrant) { |
| 731 | std::unique_ptr<Thread> t(Thread::Create()); |
| 732 | EmptyHandler handler; |
| 733 | RefCountedHandler* inner_handler( |
| 734 | new rtc::RefCountedObject<RefCountedHandler>()); |
| 735 | // When the empty handler is destroyed, it will clear messages queued for |
| 736 | // itself. The message to be cleared itself wraps a MessageHandler object |
| 737 | // (RefCountedHandler) so this will cause the message queue to be cleared |
| 738 | // again in a re-entrant fashion, which previously triggered a DCHECK. |
| 739 | // The inner handler will be removed in a re-entrant fashion from the |
| 740 | // message queue of the thread while the outer handler is removed, verifying |
| 741 | // that the iterator is not invalidated in "MessageQueue::Clear". |
| 742 | t->Post(RTC_FROM_HERE, inner_handler, 0); |
| 743 | t->Post(RTC_FROM_HERE, &handler, 0, |
| 744 | new ScopedRefMessageData<RefCountedHandler>(inner_handler)); |
| 745 | } |
| 746 | |
Tommi | 9ebe6d7 | 2021-11-16 16:07:34 +0100 | [diff] [blame] | 747 | class DEPRECATED_AsyncInvokeTest : public ::testing::Test { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 748 | public: |
| 749 | void IntCallback(int value) { |
| 750 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 751 | int_value_ = value; |
| 752 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 753 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 754 | expected_thread_ = thread; |
| 755 | } |
| 756 | |
| 757 | protected: |
| 758 | enum { kWaitTimeout = 1000 }; |
Tommi | 9ebe6d7 | 2021-11-16 16:07:34 +0100 | [diff] [blame] | 759 | DEPRECATED_AsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 760 | |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 761 | rtc::AutoThread main_thread_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 762 | int int_value_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 763 | Thread* expected_thread_; |
| 764 | }; |
| 765 | |
Tommi | 9ebe6d7 | 2021-11-16 16:07:34 +0100 | [diff] [blame] | 766 | TEST_F(DEPRECATED_AsyncInvokeTest, FireAndForget) { |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame] | 767 | DEPRECATED_AsyncInvoker invoker; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 768 | // Create and start the thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 769 | auto thread = Thread::CreateWithSocketServer(); |
| 770 | thread->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 771 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 772 | AtomicBool called; |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 773 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 774 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 775 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Tommi | 9ebe6d7 | 2021-11-16 16:07:34 +0100 | [diff] [blame] | 778 | TEST_F(DEPRECATED_AsyncInvokeTest, NonCopyableFunctor) { |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame] | 779 | DEPRECATED_AsyncInvoker invoker; |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 780 | // Create and start the thread. |
| 781 | auto thread = Thread::CreateWithSocketServer(); |
| 782 | thread->Start(); |
| 783 | // Try calling functor. |
| 784 | AtomicBool called; |
| 785 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorD(&called)); |
| 786 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
| 787 | thread->Stop(); |
| 788 | } |
| 789 | |
Tommi | 9ebe6d7 | 2021-11-16 16:07:34 +0100 | [diff] [blame] | 790 | TEST_F(DEPRECATED_AsyncInvokeTest, KillInvokerDuringExecute) { |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 791 | // Use these events to get in a state where the functor is in the middle of |
| 792 | // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE" |
| 793 | // is run. |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 794 | Event functor_started; |
| 795 | Event functor_continue; |
| 796 | Event functor_finished; |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 797 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 798 | auto thread = Thread::CreateWithSocketServer(); |
| 799 | thread->Start(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 800 | volatile bool invoker_destroyed = false; |
| 801 | { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 802 | auto functor = [&functor_started, &functor_continue, &functor_finished, |
| 803 | &invoker_destroyed] { |
| 804 | functor_started.Set(); |
| 805 | functor_continue.Wait(Event::kForever); |
| 806 | rtc::Thread::Current()->SleepMs(kWaitTimeout); |
| 807 | EXPECT_FALSE(invoker_destroyed); |
| 808 | functor_finished.Set(); |
| 809 | }; |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame] | 810 | DEPRECATED_AsyncInvoker invoker; |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 811 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), functor); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 812 | functor_started.Wait(Event::kForever); |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 813 | |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 814 | // Destroy the invoker while the functor is still executing (doing |
| 815 | // SleepMs). |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 816 | functor_continue.Set(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | // If the destructor DIDN'T wait for the functor to finish executing, it will |
| 820 | // hit the EXPECT_FALSE(invoker_destroyed) after it finishes sleeping for a |
| 821 | // second. |
| 822 | invoker_destroyed = true; |
| 823 | functor_finished.Wait(Event::kForever); |
| 824 | } |
| 825 | |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 826 | // Variant of the above test where the async-invoked task calls AsyncInvoke |
Tommi | 9ebe6d7 | 2021-11-16 16:07:34 +0100 | [diff] [blame] | 827 | // *again*, for the thread on which the invoker is currently being destroyed. |
| 828 | // This shouldn't deadlock or crash. The second invocation should be ignored. |
| 829 | TEST_F(DEPRECATED_AsyncInvokeTest, |
| 830 | KillInvokerDuringExecuteWithReentrantInvoke) { |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 831 | Event functor_started; |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 832 | // Flag used to verify that the recursively invoked task never actually runs. |
| 833 | bool reentrant_functor_run = false; |
| 834 | |
| 835 | Thread* main = Thread::Current(); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 836 | Thread thread(std::make_unique<NullSocketServer>()); |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 837 | thread.Start(); |
| 838 | { |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame] | 839 | DEPRECATED_AsyncInvoker invoker; |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 840 | auto reentrant_functor = [&reentrant_functor_run] { |
| 841 | reentrant_functor_run = true; |
| 842 | }; |
| 843 | auto functor = [&functor_started, &invoker, main, reentrant_functor] { |
| 844 | functor_started.Set(); |
| 845 | Thread::Current()->SleepMs(kWaitTimeout); |
| 846 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, main, reentrant_functor); |
| 847 | }; |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 848 | // This queues a task on `thread` to sleep for `kWaitTimeout` then queue a |
| 849 | // task on `main`. But this second queued task should never run, since the |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 850 | // destructor will be entered before it's even invoked. |
| 851 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, functor); |
| 852 | functor_started.Wait(Event::kForever); |
| 853 | } |
| 854 | EXPECT_FALSE(reentrant_functor_run); |
| 855 | } |
| 856 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 857 | void WaitAndSetEvent(Event* wait_event, Event* set_event) { |
| 858 | wait_event->Wait(Event::kForever); |
| 859 | set_event->Set(); |
| 860 | } |
| 861 | |
| 862 | // A functor that keeps track of the number of copies and moves. |
| 863 | class LifeCycleFunctor { |
| 864 | public: |
| 865 | struct Stats { |
| 866 | size_t copy_count = 0; |
| 867 | size_t move_count = 0; |
| 868 | }; |
| 869 | |
| 870 | LifeCycleFunctor(Stats* stats, Event* event) : stats_(stats), event_(event) {} |
| 871 | LifeCycleFunctor(const LifeCycleFunctor& other) { *this = other; } |
| 872 | LifeCycleFunctor(LifeCycleFunctor&& other) { *this = std::move(other); } |
| 873 | |
| 874 | LifeCycleFunctor& operator=(const LifeCycleFunctor& other) { |
| 875 | stats_ = other.stats_; |
| 876 | event_ = other.event_; |
| 877 | ++stats_->copy_count; |
| 878 | return *this; |
| 879 | } |
| 880 | |
| 881 | LifeCycleFunctor& operator=(LifeCycleFunctor&& other) { |
| 882 | stats_ = other.stats_; |
| 883 | event_ = other.event_; |
| 884 | ++stats_->move_count; |
| 885 | return *this; |
| 886 | } |
| 887 | |
| 888 | void operator()() { event_->Set(); } |
| 889 | |
| 890 | private: |
| 891 | Stats* stats_; |
| 892 | Event* event_; |
| 893 | }; |
| 894 | |
| 895 | // A functor that verifies the thread it was destroyed on. |
| 896 | class DestructionFunctor { |
| 897 | public: |
| 898 | DestructionFunctor(Thread* thread, bool* thread_was_current, Event* event) |
| 899 | : thread_(thread), |
| 900 | thread_was_current_(thread_was_current), |
| 901 | event_(event) {} |
| 902 | ~DestructionFunctor() { |
| 903 | // Only signal the event if this was the functor that was invoked to avoid |
| 904 | // the event being signaled due to the destruction of temporary/moved |
| 905 | // versions of this object. |
| 906 | if (was_invoked_) { |
| 907 | *thread_was_current_ = thread_->IsCurrent(); |
| 908 | event_->Set(); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | void operator()() { was_invoked_ = true; } |
| 913 | |
| 914 | private: |
| 915 | Thread* thread_; |
| 916 | bool* thread_was_current_; |
| 917 | Event* event_; |
| 918 | bool was_invoked_ = false; |
| 919 | }; |
| 920 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 921 | TEST(ThreadPostTaskTest, InvokesWithLambda) { |
| 922 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 923 | background_thread->Start(); |
| 924 | |
| 925 | Event event; |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 926 | background_thread->PostTask([&event] { event.Set(); }); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 927 | event.Wait(Event::kForever); |
| 928 | } |
| 929 | |
| 930 | TEST(ThreadPostTaskTest, InvokesWithCopiedFunctor) { |
| 931 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 932 | background_thread->Start(); |
| 933 | |
| 934 | LifeCycleFunctor::Stats stats; |
| 935 | Event event; |
| 936 | LifeCycleFunctor functor(&stats, &event); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 937 | background_thread->PostTask(functor); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 938 | event.Wait(Event::kForever); |
| 939 | |
| 940 | EXPECT_EQ(1u, stats.copy_count); |
| 941 | EXPECT_EQ(0u, stats.move_count); |
| 942 | } |
| 943 | |
| 944 | TEST(ThreadPostTaskTest, InvokesWithMovedFunctor) { |
| 945 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 946 | background_thread->Start(); |
| 947 | |
| 948 | LifeCycleFunctor::Stats stats; |
| 949 | Event event; |
| 950 | LifeCycleFunctor functor(&stats, &event); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 951 | background_thread->PostTask(std::move(functor)); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 952 | event.Wait(Event::kForever); |
| 953 | |
| 954 | EXPECT_EQ(0u, stats.copy_count); |
| 955 | EXPECT_EQ(1u, stats.move_count); |
| 956 | } |
| 957 | |
| 958 | TEST(ThreadPostTaskTest, InvokesWithReferencedFunctorShouldCopy) { |
| 959 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 960 | background_thread->Start(); |
| 961 | |
| 962 | LifeCycleFunctor::Stats stats; |
| 963 | Event event; |
| 964 | LifeCycleFunctor functor(&stats, &event); |
| 965 | LifeCycleFunctor& functor_ref = functor; |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 966 | background_thread->PostTask(functor_ref); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 967 | event.Wait(Event::kForever); |
| 968 | |
| 969 | EXPECT_EQ(1u, stats.copy_count); |
| 970 | EXPECT_EQ(0u, stats.move_count); |
| 971 | } |
| 972 | |
| 973 | TEST(ThreadPostTaskTest, InvokesWithCopiedFunctorDestroyedOnTargetThread) { |
| 974 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 975 | background_thread->Start(); |
| 976 | |
| 977 | Event event; |
| 978 | bool was_invoked_on_background_thread = false; |
| 979 | DestructionFunctor functor(background_thread.get(), |
| 980 | &was_invoked_on_background_thread, &event); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 981 | background_thread->PostTask(functor); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 982 | event.Wait(Event::kForever); |
| 983 | |
| 984 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 985 | } |
| 986 | |
| 987 | TEST(ThreadPostTaskTest, InvokesWithMovedFunctorDestroyedOnTargetThread) { |
| 988 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 989 | background_thread->Start(); |
| 990 | |
| 991 | Event event; |
| 992 | bool was_invoked_on_background_thread = false; |
| 993 | DestructionFunctor functor(background_thread.get(), |
| 994 | &was_invoked_on_background_thread, &event); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 995 | background_thread->PostTask(std::move(functor)); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 996 | event.Wait(Event::kForever); |
| 997 | |
| 998 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 999 | } |
| 1000 | |
| 1001 | TEST(ThreadPostTaskTest, |
| 1002 | InvokesWithReferencedFunctorShouldCopyAndDestroyedOnTargetThread) { |
| 1003 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 1004 | background_thread->Start(); |
| 1005 | |
| 1006 | Event event; |
| 1007 | bool was_invoked_on_background_thread = false; |
| 1008 | DestructionFunctor functor(background_thread.get(), |
| 1009 | &was_invoked_on_background_thread, &event); |
| 1010 | DestructionFunctor& functor_ref = functor; |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1011 | background_thread->PostTask(functor_ref); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 1012 | event.Wait(Event::kForever); |
| 1013 | |
| 1014 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 1015 | } |
| 1016 | |
| 1017 | TEST(ThreadPostTaskTest, InvokesOnBackgroundThread) { |
| 1018 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 1019 | background_thread->Start(); |
| 1020 | |
| 1021 | Event event; |
| 1022 | bool was_invoked_on_background_thread = false; |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 1023 | Thread* background_thread_ptr = background_thread.get(); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1024 | background_thread->PostTask( |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 1025 | [background_thread_ptr, &was_invoked_on_background_thread, &event] { |
| 1026 | was_invoked_on_background_thread = background_thread_ptr->IsCurrent(); |
| 1027 | event.Set(); |
| 1028 | }); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 1029 | event.Wait(Event::kForever); |
| 1030 | |
| 1031 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 1032 | } |
| 1033 | |
| 1034 | TEST(ThreadPostTaskTest, InvokesAsynchronously) { |
| 1035 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 1036 | background_thread->Start(); |
| 1037 | |
| 1038 | // The first event ensures that SendSingleMessage() is not blocking this |
| 1039 | // thread. The second event ensures that the message is processed. |
| 1040 | Event event_set_by_test_thread; |
| 1041 | Event event_set_by_background_thread; |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1042 | background_thread->PostTask( |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame] | 1043 | [&event_set_by_test_thread, &event_set_by_background_thread] { |
| 1044 | WaitAndSetEvent(&event_set_by_test_thread, |
| 1045 | &event_set_by_background_thread); |
| 1046 | }); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 1047 | event_set_by_test_thread.Set(); |
| 1048 | event_set_by_background_thread.Wait(Event::kForever); |
| 1049 | } |
| 1050 | |
| 1051 | TEST(ThreadPostTaskTest, InvokesInPostedOrder) { |
| 1052 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 1053 | background_thread->Start(); |
| 1054 | |
| 1055 | Event first; |
| 1056 | Event second; |
| 1057 | Event third; |
| 1058 | Event fourth; |
| 1059 | |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1060 | background_thread->PostTask( |
| 1061 | [&first, &second] { WaitAndSetEvent(&first, &second); }); |
| 1062 | background_thread->PostTask( |
| 1063 | [&second, &third] { WaitAndSetEvent(&second, &third); }); |
| 1064 | background_thread->PostTask( |
| 1065 | [&third, &fourth] { WaitAndSetEvent(&third, &fourth); }); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 1066 | |
| 1067 | // All tasks have been posted before the first one is unblocked. |
| 1068 | first.Set(); |
| 1069 | // Only if the chain is invoked in posted order will the last event be set. |
| 1070 | fourth.Wait(Event::kForever); |
| 1071 | } |
| 1072 | |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 1073 | TEST(ThreadPostDelayedTaskTest, InvokesAsynchronously) { |
| 1074 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 1075 | background_thread->Start(); |
| 1076 | |
| 1077 | // The first event ensures that SendSingleMessage() is not blocking this |
| 1078 | // thread. The second event ensures that the message is processed. |
| 1079 | Event event_set_by_test_thread; |
| 1080 | Event event_set_by_background_thread; |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1081 | background_thread->PostDelayedTask( |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 1082 | [&event_set_by_test_thread, &event_set_by_background_thread] { |
| 1083 | WaitAndSetEvent(&event_set_by_test_thread, |
| 1084 | &event_set_by_background_thread); |
| 1085 | }, |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 1086 | /*milliseconds=*/10); |
| 1087 | event_set_by_test_thread.Set(); |
| 1088 | event_set_by_background_thread.Wait(Event::kForever); |
| 1089 | } |
| 1090 | |
| 1091 | TEST(ThreadPostDelayedTaskTest, InvokesInDelayOrder) { |
Steve Anton | 094396f | 2019-12-16 00:56:02 -0800 | [diff] [blame] | 1092 | ScopedFakeClock clock; |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 1093 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 1094 | background_thread->Start(); |
| 1095 | |
| 1096 | Event first; |
| 1097 | Event second; |
| 1098 | Event third; |
| 1099 | Event fourth; |
| 1100 | |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1101 | background_thread->PostDelayedTask( |
| 1102 | [&third, &fourth] { WaitAndSetEvent(&third, &fourth); }, |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 1103 | /*milliseconds=*/11); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1104 | background_thread->PostDelayedTask( |
| 1105 | [&first, &second] { WaitAndSetEvent(&first, &second); }, |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 1106 | /*milliseconds=*/9); |
Henrik Boström | 595f688 | 2022-01-24 09:57:03 +0100 | [diff] [blame] | 1107 | background_thread->PostDelayedTask( |
| 1108 | [&second, &third] { WaitAndSetEvent(&second, &third); }, |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 1109 | /*milliseconds=*/10); |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 1110 | |
| 1111 | // All tasks have been posted before the first one is unblocked. |
| 1112 | first.Set(); |
Steve Anton | 094396f | 2019-12-16 00:56:02 -0800 | [diff] [blame] | 1113 | // Only if the chain is invoked in delay order will the last event be set. |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 1114 | clock.AdvanceTime(webrtc::TimeDelta::Millis(11)); |
Steve Anton | 094396f | 2019-12-16 00:56:02 -0800 | [diff] [blame] | 1115 | EXPECT_TRUE(fourth.Wait(0)); |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Tommi | 6866dc7 | 2020-05-15 10:11:56 +0200 | [diff] [blame] | 1118 | TEST(ThreadPostDelayedTaskTest, IsCurrentTaskQueue) { |
| 1119 | auto current_tq = webrtc::TaskQueueBase::Current(); |
| 1120 | { |
| 1121 | std::unique_ptr<rtc::Thread> thread(rtc::Thread::Create()); |
| 1122 | thread->WrapCurrent(); |
| 1123 | EXPECT_EQ(webrtc::TaskQueueBase::Current(), |
| 1124 | static_cast<webrtc::TaskQueueBase*>(thread.get())); |
| 1125 | thread->UnwrapCurrent(); |
| 1126 | } |
| 1127 | EXPECT_EQ(webrtc::TaskQueueBase::Current(), current_tq); |
| 1128 | } |
| 1129 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 1130 | class ThreadFactory : public webrtc::TaskQueueFactory { |
| 1131 | public: |
| 1132 | std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> |
| 1133 | CreateTaskQueue(absl::string_view /* name */, |
| 1134 | Priority /*priority*/) const override { |
| 1135 | std::unique_ptr<Thread> thread = Thread::Create(); |
| 1136 | thread->Start(); |
| 1137 | return std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>( |
| 1138 | thread.release()); |
| 1139 | } |
| 1140 | }; |
| 1141 | |
| 1142 | using ::webrtc::TaskQueueTest; |
| 1143 | |
| 1144 | INSTANTIATE_TEST_SUITE_P(RtcThread, |
| 1145 | TaskQueueTest, |
| 1146 | ::testing::Values(std::make_unique<ThreadFactory>)); |
| 1147 | |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 1148 | } // namespace |
| 1149 | } // namespace rtc |