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