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