blob: 56af8a507b4a58777d998d59237f76836b27e72e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Olssona4d87372019-07-05 19:08:33 +020011#include "rtc_base/thread.h"
12
kwibergbfefb032016-05-01 14:53:46 -070013#include <memory>
14
Niels Möller5a96a0e2019-04-30 11:45:58 +020015#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/async_invoker.h"
17#include "rtc_base/async_udp_socket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/event.h"
19#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/null_socket_server.h"
21#include "rtc_base/physical_socket_server.h"
22#include "rtc_base/socket_address.h"
Artem Titove41c4332018-07-25 15:04:28 +020023#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
25#if defined(WEBRTC_WIN)
26#include <comdef.h> // NOLINT
27#endif
28
Mirko Bonadeie10b1632018-12-11 18:43:40 +010029namespace rtc {
30namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
32// Generates a sequence of numbers (collaboratively).
33class TestGenerator {
34 public:
35 TestGenerator() : last(0), count(0) {}
36
37 int Next(int prev) {
38 int result = prev + last;
39 last = result;
40 count += 1;
41 return result;
42 }
43
44 int last;
45 int count;
46};
47
48struct TestMessage : public MessageData {
49 explicit TestMessage(int v) : value(v) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 int value;
52};
53
54// Receives on a socket and sends by posting messages.
55class SocketClient : public TestGenerator, public sigslot::has_slots<> {
56 public:
Yves Gerey665174f2018-06-19 15:03:05 +020057 SocketClient(AsyncSocket* socket,
58 const SocketAddress& addr,
59 Thread* post_thread,
60 MessageHandler* phandler)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061 : socket_(AsyncUDPSocket::Create(socket, addr)),
62 post_thread_(post_thread),
63 post_handler_(phandler) {
64 socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket);
65 }
66
Steve Anton9de3aac2017-10-24 10:08:26 -070067 ~SocketClient() override { delete socket_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
69 SocketAddress address() const { return socket_->GetLocalAddress(); }
70
Yves Gerey665174f2018-06-19 15:03:05 +020071 void OnPacket(AsyncPacketSocket* socket,
72 const char* buf,
73 size_t size,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010075 const int64_t& packet_time_us) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020076 EXPECT_EQ(size, sizeof(uint32_t));
77 uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0];
78 uint32_t result = Next(prev);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070080 post_thread_->PostDelayed(RTC_FROM_HERE, 200, post_handler_, 0,
81 new TestMessage(result));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 }
83
84 private:
85 AsyncUDPSocket* socket_;
86 Thread* post_thread_;
87 MessageHandler* post_handler_;
88};
89
90// Receives messages and sends on a socket.
91class MessageClient : public MessageHandler, public TestGenerator {
92 public:
Yves Gerey665174f2018-06-19 15:03:05 +020093 MessageClient(Thread* pth, Socket* socket) : socket_(socket) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094
Steve Anton9de3aac2017-10-24 10:08:26 -070095 ~MessageClient() override { delete socket_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
Steve Anton9de3aac2017-10-24 10:08:26 -070097 void OnMessage(Message* pmsg) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata);
99 int result = Next(msg->value);
100 EXPECT_GE(socket_->Send(&result, sizeof(result)), 0);
101 delete msg;
102 }
103
104 private:
105 Socket* socket_;
106};
107
deadbeefaea92932017-05-23 12:55:03 -0700108class CustomThread : public rtc::Thread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109 public:
tommie7251592017-07-14 14:44:46 -0700110 CustomThread()
111 : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())) {}
Steve Anton9de3aac2017-10-24 10:08:26 -0700112 ~CustomThread() override { Stop(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 bool Start() { return false; }
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000114
Yves Gerey665174f2018-06-19 15:03:05 +0200115 bool WrapCurrent() { return Thread::WrapCurrent(); }
116 void UnwrapCurrent() { Thread::UnwrapCurrent(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117};
118
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119// A thread that does nothing when it runs and signals an event
120// when it is destroyed.
121class SignalWhenDestroyedThread : public Thread {
122 public:
123 SignalWhenDestroyedThread(Event* event)
tommie7251592017-07-14 14:44:46 -0700124 : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())),
125 event_(event) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
Steve Anton9de3aac2017-10-24 10:08:26 -0700127 ~SignalWhenDestroyedThread() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 Stop();
129 event_->Set();
130 }
131
Steve Anton9de3aac2017-10-24 10:08:26 -0700132 void Run() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133 // Do nothing.
134 }
135
136 private:
137 Event* event_;
138};
139
nissed9b75be2015-11-16 00:54:07 -0800140// A bool wrapped in a mutex, to avoid data races. Using a volatile
141// bool should be sufficient for correct code ("eventual consistency"
142// between caches is sufficient), but we can't tell the compiler about
143// that, and then tsan complains about a data race.
144
145// See also discussion at
146// http://stackoverflow.com/questions/7223164/is-mutex-needed-to-synchronize-a-simple-flag-between-pthreads
147
148// Using std::atomic<bool> or std::atomic_flag in C++11 is probably
149// the right thing to do, but those features are not yet allowed. Or
deadbeefaea92932017-05-23 12:55:03 -0700150// rtc::AtomicInt, if/when that is added. Since the use isn't
nissed9b75be2015-11-16 00:54:07 -0800151// performance critical, use a plain critical section for the time
152// being.
153
154class AtomicBool {
155 public:
156 explicit AtomicBool(bool value = false) : flag_(value) {}
157 AtomicBool& operator=(bool value) {
158 CritScope scoped_lock(&cs_);
159 flag_ = value;
160 return *this;
161 }
162 bool get() const {
163 CritScope scoped_lock(&cs_);
164 return flag_;
165 }
166
167 private:
pbos5ad935c2016-01-25 03:52:44 -0800168 CriticalSection cs_;
nissed9b75be2015-11-16 00:54:07 -0800169 bool flag_;
170};
171
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172// Function objects to test Thread::Invoke.
173struct FunctorA {
174 int operator()() { return 42; }
175};
176class FunctorB {
177 public:
nissed9b75be2015-11-16 00:54:07 -0800178 explicit FunctorB(AtomicBool* flag) : flag_(flag) {}
Yves Gerey665174f2018-06-19 15:03:05 +0200179 void operator()() {
180 if (flag_)
181 *flag_ = true;
182 }
183
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 private:
nissed9b75be2015-11-16 00:54:07 -0800185 AtomicBool* flag_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186};
187struct FunctorC {
188 int operator()() {
189 Thread::Current()->ProcessMessages(50);
190 return 24;
191 }
192};
Cameron Pickettd132ce12018-03-12 16:07:37 -0700193struct FunctorD {
194 public:
195 explicit FunctorD(AtomicBool* flag) : flag_(flag) {}
196 FunctorD(FunctorD&&) = default;
197 FunctorD& operator=(FunctorD&&) = default;
Yves Gerey665174f2018-06-19 15:03:05 +0200198 void operator()() {
199 if (flag_)
200 *flag_ = true;
201 }
202
Cameron Pickettd132ce12018-03-12 16:07:37 -0700203 private:
204 AtomicBool* flag_;
205 RTC_DISALLOW_COPY_AND_ASSIGN(FunctorD);
206};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000207
208// See: https://code.google.com/p/webrtc/issues/detail?id=2409
209TEST(ThreadTest, DISABLED_Main) {
210 const SocketAddress addr("127.0.0.1", 0);
211
212 // Create the messaging client on its own thread.
tommie7251592017-07-14 14:44:46 -0700213 auto th1 = Thread::CreateWithSocketServer();
214 Socket* socket =
215 th1->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
216 MessageClient msg_client(th1.get(), socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000217
218 // Create the socket client on its own thread.
tommie7251592017-07-14 14:44:46 -0700219 auto th2 = Thread::CreateWithSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 AsyncSocket* asocket =
tommie7251592017-07-14 14:44:46 -0700221 th2->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
222 SocketClient sock_client(asocket, addr, th1.get(), &msg_client);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000223
224 socket->Connect(sock_client.address());
225
tommie7251592017-07-14 14:44:46 -0700226 th1->Start();
227 th2->Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228
229 // Get the messages started.
tommie7251592017-07-14 14:44:46 -0700230 th1->PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231
232 // Give the clients a little while to run.
233 // Messages will be processed at 100, 300, 500, 700, 900.
234 Thread* th_main = Thread::Current();
235 th_main->ProcessMessages(1000);
236
237 // Stop the sending client. Give the receiver a bit longer to run, in case
238 // it is running on a machine that is under load (e.g. the build machine).
tommie7251592017-07-14 14:44:46 -0700239 th1->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 th_main->ProcessMessages(200);
tommie7251592017-07-14 14:44:46 -0700241 th2->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242
243 // Make sure the results were correct
244 EXPECT_EQ(5, msg_client.count);
245 EXPECT_EQ(34, msg_client.last);
246 EXPECT_EQ(5, sock_client.count);
247 EXPECT_EQ(55, sock_client.last);
248}
249
250// Test that setting thread names doesn't cause a malfunction.
251// There's no easy way to verify the name was set properly at this time.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000252TEST(ThreadTest, Names) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253 // Default name
tommie7251592017-07-14 14:44:46 -0700254 auto thread = Thread::CreateWithSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000255 EXPECT_TRUE(thread->Start());
256 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000257 // Name with no object parameter
tommie7251592017-07-14 14:44:46 -0700258 thread = Thread::CreateWithSocketServer();
deadbeef37f5ecf2017-02-27 14:06:41 -0800259 EXPECT_TRUE(thread->SetName("No object", nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260 EXPECT_TRUE(thread->Start());
261 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 // Really long name
tommie7251592017-07-14 14:44:46 -0700263 thread = Thread::CreateWithSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000264 EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this));
265 EXPECT_TRUE(thread->Start());
266 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000267}
268
henrike@webrtc.orge30dab72014-10-09 15:41:40 +0000269TEST(ThreadTest, Wrap) {
270 Thread* current_thread = Thread::Current();
Niels Möller5a8f8602019-06-12 11:30:59 +0200271 ThreadManager::Instance()->SetCurrentThread(nullptr);
272
273 {
274 CustomThread cthread;
275 EXPECT_TRUE(cthread.WrapCurrent());
276 EXPECT_EQ(&cthread, Thread::Current());
277 EXPECT_TRUE(cthread.RunningForTest());
278 EXPECT_FALSE(cthread.IsOwned());
279 cthread.UnwrapCurrent();
280 EXPECT_FALSE(cthread.RunningForTest());
281 }
282 ThreadManager::Instance()->SetCurrentThread(current_thread);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283}
284
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000285TEST(ThreadTest, Invoke) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286 // Create and start the thread.
tommie7251592017-07-14 14:44:46 -0700287 auto thread = Thread::CreateWithSocketServer();
288 thread->Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 // Try calling functors.
tommie7251592017-07-14 14:44:46 -0700290 EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA()));
nissed9b75be2015-11-16 00:54:07 -0800291 AtomicBool called;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000292 FunctorB f2(&called);
tommie7251592017-07-14 14:44:46 -0700293 thread->Invoke<void>(RTC_FROM_HERE, f2);
nissed9b75be2015-11-16 00:54:07 -0800294 EXPECT_TRUE(called.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 // Try calling bare functions.
296 struct LocalFuncs {
297 static int Func1() { return 999; }
298 static void Func2() {}
299 };
tommie7251592017-07-14 14:44:46 -0700300 EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1));
301 thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302}
303
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000304// Verifies that two threads calling Invoke on each other at the same time does
305// not deadlock.
306TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) {
307 AutoThread thread;
308 Thread* current_thread = Thread::Current();
deadbeef37f5ecf2017-02-27 14:06:41 -0800309 ASSERT_TRUE(current_thread != nullptr);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000310
tommie7251592017-07-14 14:44:46 -0700311 auto other_thread = Thread::CreateWithSocketServer();
312 other_thread->Start();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000313
314 struct LocalFuncs {
315 static void Set(bool* out) { *out = true; }
316 static void InvokeSet(Thread* thread, bool* out) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700317 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000318 }
319 };
320
321 bool called = false;
tommie7251592017-07-14 14:44:46 -0700322 other_thread->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700323 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000324
325 EXPECT_TRUE(called);
326}
327
328// Verifies that if thread A invokes a call on thread B and thread C is trying
329// to invoke A at the same time, thread A does not handle C's invoke while
330// invoking B.
331TEST(ThreadTest, ThreeThreadsInvoke) {
332 AutoThread thread;
333 Thread* thread_a = Thread::Current();
tommie7251592017-07-14 14:44:46 -0700334 auto thread_b = Thread::CreateWithSocketServer();
335 auto thread_c = Thread::CreateWithSocketServer();
336 thread_b->Start();
337 thread_c->Start();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000338
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000339 class LockedBool {
340 public:
341 explicit LockedBool(bool value) : value_(value) {}
342
343 void Set(bool value) {
344 CritScope lock(&crit_);
345 value_ = value;
346 }
347
348 bool Get() {
349 CritScope lock(&crit_);
350 return value_;
351 }
352
353 private:
354 CriticalSection crit_;
danilchap3c6abd22017-09-06 05:46:29 -0700355 bool value_ RTC_GUARDED_BY(crit_);
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000356 };
357
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000358 struct LocalFuncs {
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000359 static void Set(LockedBool* out) { out->Set(true); }
360 static void InvokeSet(Thread* thread, LockedBool* out) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700361 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000362 }
363
364 // Set |out| true and call InvokeSet on |thread|.
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000365 static void SetAndInvokeSet(LockedBool* out,
366 Thread* thread,
367 LockedBool* out_inner) {
368 out->Set(true);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000369 InvokeSet(thread, out_inner);
370 }
371
372 // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until
373 // |thread1| starts the call.
deadbeef162cb532017-02-23 17:10:07 -0800374 static void AsyncInvokeSetAndWait(AsyncInvoker* invoker,
375 Thread* thread1,
376 Thread* thread2,
377 LockedBool* out) {
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000378 CriticalSection crit;
379 LockedBool async_invoked(false);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000380
deadbeef162cb532017-02-23 17:10:07 -0800381 invoker->AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700382 RTC_FROM_HERE, thread1,
383 Bind(&SetAndInvokeSet, &async_invoked, thread2, out));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000384
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000385 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000386 }
387 };
388
deadbeef162cb532017-02-23 17:10:07 -0800389 AsyncInvoker invoker;
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000390 LockedBool thread_a_called(false);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000391
392 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A.
393 // Thread B returns when C receives the call and C should be blocked until A
394 // starts to process messages.
tommie7251592017-07-14 14:44:46 -0700395 thread_b->Invoke<void>(RTC_FROM_HERE,
396 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker,
397 thread_c.get(), thread_a, &thread_a_called));
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000398 EXPECT_FALSE(thread_a_called.Get());
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000399
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000400 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000401}
402
jbauch25d1f282016-02-05 00:25:02 -0800403// Set the name on a thread when the underlying QueueDestroyed signal is
404// triggered. This causes an error if the object is already partially
405// destroyed.
406class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> {
407 public:
408 SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) {
409 thread->SignalQueueDestroyed.connect(
410 this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed);
411 }
412
413 void OnQueueDestroyed() {
414 // Makes sure that if we access the Thread while it's being destroyed, that
415 // it doesn't cause a problem because the vtable has been modified.
416 thread_->SetName("foo", nullptr);
417 }
418
419 private:
420 Thread* thread_;
421};
422
423TEST(ThreadTest, SetNameOnSignalQueueDestroyed) {
tommie7251592017-07-14 14:44:46 -0700424 auto thread1 = Thread::CreateWithSocketServer();
425 SetNameOnSignalQueueDestroyedTester tester1(thread1.get());
426 thread1.reset();
jbauch25d1f282016-02-05 00:25:02 -0800427
428 Thread* thread2 = new AutoThread();
429 SetNameOnSignalQueueDestroyedTester tester2(thread2);
430 delete thread2;
jbauch25d1f282016-02-05 00:25:02 -0800431}
432
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200433class AsyncInvokeTest : public ::testing::Test {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434 public:
435 void IntCallback(int value) {
436 EXPECT_EQ(expected_thread_, Thread::Current());
437 int_value_ = value;
438 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000439 void SetExpectedThreadForIntCallback(Thread* thread) {
440 expected_thread_ = thread;
441 }
442
443 protected:
444 enum { kWaitTimeout = 1000 };
Yves Gerey665174f2018-06-19 15:03:05 +0200445 AsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000446
447 int int_value_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000448 Thread* expected_thread_;
449};
450
henrike@webrtc.orge30dab72014-10-09 15:41:40 +0000451TEST_F(AsyncInvokeTest, FireAndForget) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000452 AsyncInvoker invoker;
453 // Create and start the thread.
tommie7251592017-07-14 14:44:46 -0700454 auto thread = Thread::CreateWithSocketServer();
455 thread->Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000456 // Try calling functor.
nissed9b75be2015-11-16 00:54:07 -0800457 AtomicBool called;
tommie7251592017-07-14 14:44:46 -0700458 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called));
nissed9b75be2015-11-16 00:54:07 -0800459 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
tommie7251592017-07-14 14:44:46 -0700460 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000461}
462
Cameron Pickettd132ce12018-03-12 16:07:37 -0700463TEST_F(AsyncInvokeTest, NonCopyableFunctor) {
464 AsyncInvoker invoker;
465 // Create and start the thread.
466 auto thread = Thread::CreateWithSocketServer();
467 thread->Start();
468 // Try calling functor.
469 AtomicBool called;
470 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorD(&called));
471 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
472 thread->Stop();
473}
474
deadbeef162cb532017-02-23 17:10:07 -0800475TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) {
476 // Use these events to get in a state where the functor is in the middle of
477 // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE"
478 // is run.
Niels Möllerc572ff32018-11-07 08:43:50 +0100479 Event functor_started;
480 Event functor_continue;
481 Event functor_finished;
deadbeef162cb532017-02-23 17:10:07 -0800482
tommie7251592017-07-14 14:44:46 -0700483 auto thread = Thread::CreateWithSocketServer();
484 thread->Start();
deadbeef162cb532017-02-23 17:10:07 -0800485 volatile bool invoker_destroyed = false;
486 {
deadbeef3af63b02017-08-08 17:59:47 -0700487 auto functor = [&functor_started, &functor_continue, &functor_finished,
488 &invoker_destroyed] {
489 functor_started.Set();
490 functor_continue.Wait(Event::kForever);
491 rtc::Thread::Current()->SleepMs(kWaitTimeout);
492 EXPECT_FALSE(invoker_destroyed);
493 functor_finished.Set();
494 };
deadbeef162cb532017-02-23 17:10:07 -0800495 AsyncInvoker invoker;
deadbeef3af63b02017-08-08 17:59:47 -0700496 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), functor);
deadbeef162cb532017-02-23 17:10:07 -0800497 functor_started.Wait(Event::kForever);
deadbeefaea92932017-05-23 12:55:03 -0700498
deadbeef3af63b02017-08-08 17:59:47 -0700499 // Destroy the invoker while the functor is still executing (doing
500 // SleepMs).
deadbeefaea92932017-05-23 12:55:03 -0700501 functor_continue.Set();
deadbeef162cb532017-02-23 17:10:07 -0800502 }
503
504 // If the destructor DIDN'T wait for the functor to finish executing, it will
505 // hit the EXPECT_FALSE(invoker_destroyed) after it finishes sleeping for a
506 // second.
507 invoker_destroyed = true;
508 functor_finished.Wait(Event::kForever);
509}
510
deadbeef3af63b02017-08-08 17:59:47 -0700511// Variant of the above test where the async-invoked task calls AsyncInvoke
512// *again*, for the thread on which the AsyncInvoker is currently being
513// destroyed. This shouldn't deadlock or crash; this second invocation should
514// just be ignored.
515TEST_F(AsyncInvokeTest, KillInvokerDuringExecuteWithReentrantInvoke) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100516 Event functor_started;
deadbeef3af63b02017-08-08 17:59:47 -0700517 // Flag used to verify that the recursively invoked task never actually runs.
518 bool reentrant_functor_run = false;
519
520 Thread* main = Thread::Current();
Niels Möller5a96a0e2019-04-30 11:45:58 +0200521 Thread thread(absl::make_unique<NullSocketServer>());
deadbeef3af63b02017-08-08 17:59:47 -0700522 thread.Start();
523 {
524 AsyncInvoker invoker;
525 auto reentrant_functor = [&reentrant_functor_run] {
526 reentrant_functor_run = true;
527 };
528 auto functor = [&functor_started, &invoker, main, reentrant_functor] {
529 functor_started.Set();
530 Thread::Current()->SleepMs(kWaitTimeout);
531 invoker.AsyncInvoke<void>(RTC_FROM_HERE, main, reentrant_functor);
532 };
533 // This queues a task on |thread| to sleep for |kWaitTimeout| then queue a
534 // task on |main|. But this second queued task should never run, since the
535 // destructor will be entered before it's even invoked.
536 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, functor);
537 functor_started.Wait(Event::kForever);
538 }
539 EXPECT_FALSE(reentrant_functor_run);
540}
541
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000542TEST_F(AsyncInvokeTest, Flush) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543 AsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800544 AtomicBool flag1;
545 AtomicBool flag2;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000546 // Queue two async calls to the current thread.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700547 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1));
548 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000549 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800550 EXPECT_FALSE(flag1.get());
551 EXPECT_FALSE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000552 // Force them to run now.
553 invoker.Flush(Thread::Current());
nissed9b75be2015-11-16 00:54:07 -0800554 EXPECT_TRUE(flag1.get());
555 EXPECT_TRUE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000556}
557
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000558TEST_F(AsyncInvokeTest, FlushWithIds) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000559 AsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800560 AtomicBool flag1;
561 AtomicBool flag2;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000562 // Queue two async calls to the current thread, one with a message id.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700563 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000564 5);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700565 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000566 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800567 EXPECT_FALSE(flag1.get());
568 EXPECT_FALSE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000569 // Execute pending calls with id == 5.
570 invoker.Flush(Thread::Current(), 5);
nissed9b75be2015-11-16 00:54:07 -0800571 EXPECT_TRUE(flag1.get());
572 EXPECT_FALSE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000573 flag1 = false;
574 // Execute all pending calls. The id == 5 call should not execute again.
575 invoker.Flush(Thread::Current());
nissed9b75be2015-11-16 00:54:07 -0800576 EXPECT_FALSE(flag1.get());
577 EXPECT_TRUE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000578}
579
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200580class GuardedAsyncInvokeTest : public ::testing::Test {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200581 public:
582 void IntCallback(int value) {
583 EXPECT_EQ(expected_thread_, Thread::Current());
584 int_value_ = value;
585 }
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200586 void SetExpectedThreadForIntCallback(Thread* thread) {
587 expected_thread_ = thread;
588 }
589
590 protected:
591 const static int kWaitTimeout = 1000;
Yves Gerey665174f2018-06-19 15:03:05 +0200592 GuardedAsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {}
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200593
594 int int_value_;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200595 Thread* expected_thread_;
596};
597
598// Functor for creating an invoker.
599struct CreateInvoker {
jbauch555604a2016-04-26 03:13:22 -0700600 CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker)
601 : invoker_(invoker) {}
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200602 void operator()() { invoker_->reset(new GuardedAsyncInvoker()); }
jbauch555604a2016-04-26 03:13:22 -0700603 std::unique_ptr<GuardedAsyncInvoker>* invoker_;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200604};
605
606// Test that we can call AsyncInvoke<void>() after the thread died.
607TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) {
608 // Create and start the thread.
tommie7251592017-07-14 14:44:46 -0700609 std::unique_ptr<Thread> thread(Thread::Create());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200610 thread->Start();
jbauch555604a2016-04-26 03:13:22 -0700611 std::unique_ptr<GuardedAsyncInvoker> invoker;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200612 // Create the invoker on |thread|.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700613 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200614 // Kill |thread|.
615 thread = nullptr;
616 // Try calling functor.
nissed9b75be2015-11-16 00:54:07 -0800617 AtomicBool called;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700618 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called)));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200619 // With thread gone, nothing should happen.
nissed9b75be2015-11-16 00:54:07 -0800620 WAIT(called.get(), kWaitTimeout);
621 EXPECT_FALSE(called.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200622}
623
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200624// The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker
625// when Thread is still alive.
626TEST_F(GuardedAsyncInvokeTest, FireAndForget) {
627 GuardedAsyncInvoker invoker;
628 // Try calling functor.
nissed9b75be2015-11-16 00:54:07 -0800629 AtomicBool called;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700630 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called)));
nissed9b75be2015-11-16 00:54:07 -0800631 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200632}
633
Cameron Pickettd132ce12018-03-12 16:07:37 -0700634TEST_F(GuardedAsyncInvokeTest, NonCopyableFunctor) {
635 GuardedAsyncInvoker invoker;
636 // Try calling functor.
637 AtomicBool called;
638 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorD(&called)));
639 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
640}
641
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200642TEST_F(GuardedAsyncInvokeTest, Flush) {
643 GuardedAsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800644 AtomicBool flag1;
645 AtomicBool flag2;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200646 // Queue two async calls to the current thread.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700647 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1)));
648 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2)));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200649 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800650 EXPECT_FALSE(flag1.get());
651 EXPECT_FALSE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200652 // Force them to run now.
653 EXPECT_TRUE(invoker.Flush());
nissed9b75be2015-11-16 00:54:07 -0800654 EXPECT_TRUE(flag1.get());
655 EXPECT_TRUE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200656}
657
658TEST_F(GuardedAsyncInvokeTest, FlushWithIds) {
659 GuardedAsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800660 AtomicBool flag1;
661 AtomicBool flag2;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200662 // Queue two async calls to the current thread, one with a message id.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700663 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1), 5));
664 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2)));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200665 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800666 EXPECT_FALSE(flag1.get());
667 EXPECT_FALSE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200668 // Execute pending calls with id == 5.
669 EXPECT_TRUE(invoker.Flush(5));
nissed9b75be2015-11-16 00:54:07 -0800670 EXPECT_TRUE(flag1.get());
671 EXPECT_FALSE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200672 flag1 = false;
673 // Execute all pending calls. The id == 5 call should not execute again.
674 EXPECT_TRUE(invoker.Flush());
nissed9b75be2015-11-16 00:54:07 -0800675 EXPECT_FALSE(flag1.get());
676 EXPECT_TRUE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200677}
Mirko Bonadeie10b1632018-12-11 18:43:40 +0100678
Henrik Boströmba4dcc32019-02-28 09:34:06 +0100679void ThreadIsCurrent(Thread* thread, bool* result, Event* event) {
680 *result = thread->IsCurrent();
681 event->Set();
682}
683
684void WaitAndSetEvent(Event* wait_event, Event* set_event) {
685 wait_event->Wait(Event::kForever);
686 set_event->Set();
687}
688
689// A functor that keeps track of the number of copies and moves.
690class LifeCycleFunctor {
691 public:
692 struct Stats {
693 size_t copy_count = 0;
694 size_t move_count = 0;
695 };
696
697 LifeCycleFunctor(Stats* stats, Event* event) : stats_(stats), event_(event) {}
698 LifeCycleFunctor(const LifeCycleFunctor& other) { *this = other; }
699 LifeCycleFunctor(LifeCycleFunctor&& other) { *this = std::move(other); }
700
701 LifeCycleFunctor& operator=(const LifeCycleFunctor& other) {
702 stats_ = other.stats_;
703 event_ = other.event_;
704 ++stats_->copy_count;
705 return *this;
706 }
707
708 LifeCycleFunctor& operator=(LifeCycleFunctor&& other) {
709 stats_ = other.stats_;
710 event_ = other.event_;
711 ++stats_->move_count;
712 return *this;
713 }
714
715 void operator()() { event_->Set(); }
716
717 private:
718 Stats* stats_;
719 Event* event_;
720};
721
722// A functor that verifies the thread it was destroyed on.
723class DestructionFunctor {
724 public:
725 DestructionFunctor(Thread* thread, bool* thread_was_current, Event* event)
726 : thread_(thread),
727 thread_was_current_(thread_was_current),
728 event_(event) {}
729 ~DestructionFunctor() {
730 // Only signal the event if this was the functor that was invoked to avoid
731 // the event being signaled due to the destruction of temporary/moved
732 // versions of this object.
733 if (was_invoked_) {
734 *thread_was_current_ = thread_->IsCurrent();
735 event_->Set();
736 }
737 }
738
739 void operator()() { was_invoked_ = true; }
740
741 private:
742 Thread* thread_;
743 bool* thread_was_current_;
744 Event* event_;
745 bool was_invoked_ = false;
746};
747
748TEST(ThreadPostTaskTest, InvokesWithBind) {
749 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
750 background_thread->Start();
751
752 Event event;
753 background_thread->PostTask(RTC_FROM_HERE, Bind(&Event::Set, &event));
754 event.Wait(Event::kForever);
755}
756
757TEST(ThreadPostTaskTest, InvokesWithLambda) {
758 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
759 background_thread->Start();
760
761 Event event;
762 background_thread->PostTask(RTC_FROM_HERE, [&event] { event.Set(); });
763 event.Wait(Event::kForever);
764}
765
766TEST(ThreadPostTaskTest, InvokesWithCopiedFunctor) {
767 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
768 background_thread->Start();
769
770 LifeCycleFunctor::Stats stats;
771 Event event;
772 LifeCycleFunctor functor(&stats, &event);
773 background_thread->PostTask(RTC_FROM_HERE, functor);
774 event.Wait(Event::kForever);
775
776 EXPECT_EQ(1u, stats.copy_count);
777 EXPECT_EQ(0u, stats.move_count);
778}
779
780TEST(ThreadPostTaskTest, InvokesWithMovedFunctor) {
781 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
782 background_thread->Start();
783
784 LifeCycleFunctor::Stats stats;
785 Event event;
786 LifeCycleFunctor functor(&stats, &event);
787 background_thread->PostTask(RTC_FROM_HERE, std::move(functor));
788 event.Wait(Event::kForever);
789
790 EXPECT_EQ(0u, stats.copy_count);
791 EXPECT_EQ(1u, stats.move_count);
792}
793
794TEST(ThreadPostTaskTest, InvokesWithReferencedFunctorShouldCopy) {
795 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
796 background_thread->Start();
797
798 LifeCycleFunctor::Stats stats;
799 Event event;
800 LifeCycleFunctor functor(&stats, &event);
801 LifeCycleFunctor& functor_ref = functor;
802 background_thread->PostTask(RTC_FROM_HERE, functor_ref);
803 event.Wait(Event::kForever);
804
805 EXPECT_EQ(1u, stats.copy_count);
806 EXPECT_EQ(0u, stats.move_count);
807}
808
809TEST(ThreadPostTaskTest, InvokesWithCopiedFunctorDestroyedOnTargetThread) {
810 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
811 background_thread->Start();
812
813 Event event;
814 bool was_invoked_on_background_thread = false;
815 DestructionFunctor functor(background_thread.get(),
816 &was_invoked_on_background_thread, &event);
817 background_thread->PostTask(RTC_FROM_HERE, functor);
818 event.Wait(Event::kForever);
819
820 EXPECT_TRUE(was_invoked_on_background_thread);
821}
822
823TEST(ThreadPostTaskTest, InvokesWithMovedFunctorDestroyedOnTargetThread) {
824 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
825 background_thread->Start();
826
827 Event event;
828 bool was_invoked_on_background_thread = false;
829 DestructionFunctor functor(background_thread.get(),
830 &was_invoked_on_background_thread, &event);
831 background_thread->PostTask(RTC_FROM_HERE, std::move(functor));
832 event.Wait(Event::kForever);
833
834 EXPECT_TRUE(was_invoked_on_background_thread);
835}
836
837TEST(ThreadPostTaskTest,
838 InvokesWithReferencedFunctorShouldCopyAndDestroyedOnTargetThread) {
839 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
840 background_thread->Start();
841
842 Event event;
843 bool was_invoked_on_background_thread = false;
844 DestructionFunctor functor(background_thread.get(),
845 &was_invoked_on_background_thread, &event);
846 DestructionFunctor& functor_ref = functor;
847 background_thread->PostTask(RTC_FROM_HERE, functor_ref);
848 event.Wait(Event::kForever);
849
850 EXPECT_TRUE(was_invoked_on_background_thread);
851}
852
853TEST(ThreadPostTaskTest, InvokesOnBackgroundThread) {
854 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
855 background_thread->Start();
856
857 Event event;
858 bool was_invoked_on_background_thread = false;
859 background_thread->PostTask(RTC_FROM_HERE,
860 Bind(&ThreadIsCurrent, background_thread.get(),
861 &was_invoked_on_background_thread, &event));
862 event.Wait(Event::kForever);
863
864 EXPECT_TRUE(was_invoked_on_background_thread);
865}
866
867TEST(ThreadPostTaskTest, InvokesAsynchronously) {
868 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
869 background_thread->Start();
870
871 // The first event ensures that SendSingleMessage() is not blocking this
872 // thread. The second event ensures that the message is processed.
873 Event event_set_by_test_thread;
874 Event event_set_by_background_thread;
875 background_thread->PostTask(RTC_FROM_HERE,
876 Bind(&WaitAndSetEvent, &event_set_by_test_thread,
877 &event_set_by_background_thread));
878 event_set_by_test_thread.Set();
879 event_set_by_background_thread.Wait(Event::kForever);
880}
881
882TEST(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
891 background_thread->PostTask(RTC_FROM_HERE,
892 Bind(&WaitAndSetEvent, &first, &second));
893 background_thread->PostTask(RTC_FROM_HERE,
894 Bind(&WaitAndSetEvent, &second, &third));
895 background_thread->PostTask(RTC_FROM_HERE,
896 Bind(&WaitAndSetEvent, &third, &fourth));
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
Mirko Bonadeie10b1632018-12-11 18:43:40 +0100904} // namespace
905} // namespace rtc