blob: 55a0e2d553e35560990488c33774a72afe47cf97 [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
kwibergbfefb032016-05-01 14:53:46 -070011#include <memory>
12
Niels Möller5a96a0e2019-04-30 11:45:58 +020013#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/async_invoker.h"
15#include "rtc_base/async_udp_socket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/event.h"
17#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/null_socket_server.h"
19#include "rtc_base/physical_socket_server.h"
20#include "rtc_base/socket_address.h"
Artem Titove41c4332018-07-25 15:04:28 +020021#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24#if defined(WEBRTC_WIN)
25#include <comdef.h> // NOLINT
26#endif
27
Mirko Bonadeie10b1632018-12-11 18:43:40 +010028namespace rtc {
29namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030
31// Generates a sequence of numbers (collaboratively).
32class TestGenerator {
33 public:
34 TestGenerator() : last(0), count(0) {}
35
36 int Next(int prev) {
37 int result = prev + last;
38 last = result;
39 count += 1;
40 return result;
41 }
42
43 int last;
44 int count;
45};
46
47struct TestMessage : public MessageData {
48 explicit TestMessage(int v) : value(v) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
50 int value;
51};
52
53// Receives on a socket and sends by posting messages.
54class SocketClient : public TestGenerator, public sigslot::has_slots<> {
55 public:
Yves Gerey665174f2018-06-19 15:03:05 +020056 SocketClient(AsyncSocket* socket,
57 const SocketAddress& addr,
58 Thread* post_thread,
59 MessageHandler* phandler)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 : socket_(AsyncUDPSocket::Create(socket, addr)),
61 post_thread_(post_thread),
62 post_handler_(phandler) {
63 socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket);
64 }
65
Steve Anton9de3aac2017-10-24 10:08:26 -070066 ~SocketClient() override { delete socket_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
68 SocketAddress address() const { return socket_->GetLocalAddress(); }
69
Yves Gerey665174f2018-06-19 15:03:05 +020070 void OnPacket(AsyncPacketSocket* socket,
71 const char* buf,
72 size_t size,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010074 const int64_t& packet_time_us) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020075 EXPECT_EQ(size, sizeof(uint32_t));
76 uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0];
77 uint32_t result = Next(prev);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070079 post_thread_->PostDelayed(RTC_FROM_HERE, 200, post_handler_, 0,
80 new TestMessage(result));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 }
82
83 private:
84 AsyncUDPSocket* socket_;
85 Thread* post_thread_;
86 MessageHandler* post_handler_;
87};
88
89// Receives messages and sends on a socket.
90class MessageClient : public MessageHandler, public TestGenerator {
91 public:
Yves Gerey665174f2018-06-19 15:03:05 +020092 MessageClient(Thread* pth, Socket* socket) : socket_(socket) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093
Steve Anton9de3aac2017-10-24 10:08:26 -070094 ~MessageClient() override { delete socket_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095
Steve Anton9de3aac2017-10-24 10:08:26 -070096 void OnMessage(Message* pmsg) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata);
98 int result = Next(msg->value);
99 EXPECT_GE(socket_->Send(&result, sizeof(result)), 0);
100 delete msg;
101 }
102
103 private:
104 Socket* socket_;
105};
106
deadbeefaea92932017-05-23 12:55:03 -0700107class CustomThread : public rtc::Thread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 public:
tommie7251592017-07-14 14:44:46 -0700109 CustomThread()
110 : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())) {}
Steve Anton9de3aac2017-10-24 10:08:26 -0700111 ~CustomThread() override { Stop(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 bool Start() { return false; }
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000113
Yves Gerey665174f2018-06-19 15:03:05 +0200114 bool WrapCurrent() { return Thread::WrapCurrent(); }
115 void UnwrapCurrent() { Thread::UnwrapCurrent(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116};
117
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118// A thread that does nothing when it runs and signals an event
119// when it is destroyed.
120class SignalWhenDestroyedThread : public Thread {
121 public:
122 SignalWhenDestroyedThread(Event* event)
tommie7251592017-07-14 14:44:46 -0700123 : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())),
124 event_(event) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
Steve Anton9de3aac2017-10-24 10:08:26 -0700126 ~SignalWhenDestroyedThread() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 Stop();
128 event_->Set();
129 }
130
Steve Anton9de3aac2017-10-24 10:08:26 -0700131 void Run() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132 // Do nothing.
133 }
134
135 private:
136 Event* event_;
137};
138
nissed9b75be2015-11-16 00:54:07 -0800139// A bool wrapped in a mutex, to avoid data races. Using a volatile
140// bool should be sufficient for correct code ("eventual consistency"
141// between caches is sufficient), but we can't tell the compiler about
142// that, and then tsan complains about a data race.
143
144// See also discussion at
145// http://stackoverflow.com/questions/7223164/is-mutex-needed-to-synchronize-a-simple-flag-between-pthreads
146
147// Using std::atomic<bool> or std::atomic_flag in C++11 is probably
148// the right thing to do, but those features are not yet allowed. Or
deadbeefaea92932017-05-23 12:55:03 -0700149// rtc::AtomicInt, if/when that is added. Since the use isn't
nissed9b75be2015-11-16 00:54:07 -0800150// performance critical, use a plain critical section for the time
151// being.
152
153class AtomicBool {
154 public:
155 explicit AtomicBool(bool value = false) : flag_(value) {}
156 AtomicBool& operator=(bool value) {
157 CritScope scoped_lock(&cs_);
158 flag_ = value;
159 return *this;
160 }
161 bool get() const {
162 CritScope scoped_lock(&cs_);
163 return flag_;
164 }
165
166 private:
pbos5ad935c2016-01-25 03:52:44 -0800167 CriticalSection cs_;
nissed9b75be2015-11-16 00:54:07 -0800168 bool flag_;
169};
170
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171// Function objects to test Thread::Invoke.
172struct FunctorA {
173 int operator()() { return 42; }
174};
175class FunctorB {
176 public:
nissed9b75be2015-11-16 00:54:07 -0800177 explicit FunctorB(AtomicBool* flag) : flag_(flag) {}
Yves Gerey665174f2018-06-19 15:03:05 +0200178 void operator()() {
179 if (flag_)
180 *flag_ = true;
181 }
182
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183 private:
nissed9b75be2015-11-16 00:54:07 -0800184 AtomicBool* flag_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185};
186struct FunctorC {
187 int operator()() {
188 Thread::Current()->ProcessMessages(50);
189 return 24;
190 }
191};
Cameron Pickettd132ce12018-03-12 16:07:37 -0700192struct FunctorD {
193 public:
194 explicit FunctorD(AtomicBool* flag) : flag_(flag) {}
195 FunctorD(FunctorD&&) = default;
196 FunctorD& operator=(FunctorD&&) = default;
Yves Gerey665174f2018-06-19 15:03:05 +0200197 void operator()() {
198 if (flag_)
199 *flag_ = true;
200 }
201
Cameron Pickettd132ce12018-03-12 16:07:37 -0700202 private:
203 AtomicBool* flag_;
204 RTC_DISALLOW_COPY_AND_ASSIGN(FunctorD);
205};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206
207// See: https://code.google.com/p/webrtc/issues/detail?id=2409
208TEST(ThreadTest, DISABLED_Main) {
209 const SocketAddress addr("127.0.0.1", 0);
210
211 // Create the messaging client on its own thread.
tommie7251592017-07-14 14:44:46 -0700212 auto th1 = Thread::CreateWithSocketServer();
213 Socket* socket =
214 th1->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
215 MessageClient msg_client(th1.get(), socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216
217 // Create the socket client on its own thread.
tommie7251592017-07-14 14:44:46 -0700218 auto th2 = Thread::CreateWithSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219 AsyncSocket* asocket =
tommie7251592017-07-14 14:44:46 -0700220 th2->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
221 SocketClient sock_client(asocket, addr, th1.get(), &msg_client);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222
223 socket->Connect(sock_client.address());
224
tommie7251592017-07-14 14:44:46 -0700225 th1->Start();
226 th2->Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227
228 // Get the messages started.
tommie7251592017-07-14 14:44:46 -0700229 th1->PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230
231 // Give the clients a little while to run.
232 // Messages will be processed at 100, 300, 500, 700, 900.
233 Thread* th_main = Thread::Current();
234 th_main->ProcessMessages(1000);
235
236 // Stop the sending client. Give the receiver a bit longer to run, in case
237 // it is running on a machine that is under load (e.g. the build machine).
tommie7251592017-07-14 14:44:46 -0700238 th1->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 th_main->ProcessMessages(200);
tommie7251592017-07-14 14:44:46 -0700240 th2->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241
242 // Make sure the results were correct
243 EXPECT_EQ(5, msg_client.count);
244 EXPECT_EQ(34, msg_client.last);
245 EXPECT_EQ(5, sock_client.count);
246 EXPECT_EQ(55, sock_client.last);
247}
248
249// Test that setting thread names doesn't cause a malfunction.
250// There's no easy way to verify the name was set properly at this time.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000251TEST(ThreadTest, Names) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000252 // Default name
tommie7251592017-07-14 14:44:46 -0700253 auto thread = Thread::CreateWithSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 EXPECT_TRUE(thread->Start());
255 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256 // Name with no object parameter
tommie7251592017-07-14 14:44:46 -0700257 thread = Thread::CreateWithSocketServer();
deadbeef37f5ecf2017-02-27 14:06:41 -0800258 EXPECT_TRUE(thread->SetName("No object", nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 EXPECT_TRUE(thread->Start());
260 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261 // Really long name
tommie7251592017-07-14 14:44:46 -0700262 thread = Thread::CreateWithSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this));
264 EXPECT_TRUE(thread->Start());
265 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266}
267
henrike@webrtc.orge30dab72014-10-09 15:41:40 +0000268TEST(ThreadTest, Wrap) {
269 Thread* current_thread = Thread::Current();
Niels Möller5a8f8602019-06-12 11:30:59 +0200270 ThreadManager::Instance()->SetCurrentThread(nullptr);
271
272 {
273 CustomThread cthread;
274 EXPECT_TRUE(cthread.WrapCurrent());
275 EXPECT_EQ(&cthread, Thread::Current());
276 EXPECT_TRUE(cthread.RunningForTest());
277 EXPECT_FALSE(cthread.IsOwned());
278 cthread.UnwrapCurrent();
279 EXPECT_FALSE(cthread.RunningForTest());
280 }
281 ThreadManager::Instance()->SetCurrentThread(current_thread);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282}
283
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000284TEST(ThreadTest, Invoke) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 // Create and start the thread.
tommie7251592017-07-14 14:44:46 -0700286 auto thread = Thread::CreateWithSocketServer();
287 thread->Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 // Try calling functors.
tommie7251592017-07-14 14:44:46 -0700289 EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA()));
nissed9b75be2015-11-16 00:54:07 -0800290 AtomicBool called;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291 FunctorB f2(&called);
tommie7251592017-07-14 14:44:46 -0700292 thread->Invoke<void>(RTC_FROM_HERE, f2);
nissed9b75be2015-11-16 00:54:07 -0800293 EXPECT_TRUE(called.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000294 // Try calling bare functions.
295 struct LocalFuncs {
296 static int Func1() { return 999; }
297 static void Func2() {}
298 };
tommie7251592017-07-14 14:44:46 -0700299 EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1));
300 thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301}
302
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000303// Verifies that two threads calling Invoke on each other at the same time does
304// not deadlock.
305TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) {
306 AutoThread thread;
307 Thread* current_thread = Thread::Current();
deadbeef37f5ecf2017-02-27 14:06:41 -0800308 ASSERT_TRUE(current_thread != nullptr);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000309
tommie7251592017-07-14 14:44:46 -0700310 auto other_thread = Thread::CreateWithSocketServer();
311 other_thread->Start();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000312
313 struct LocalFuncs {
314 static void Set(bool* out) { *out = true; }
315 static void InvokeSet(Thread* thread, bool* out) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700316 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000317 }
318 };
319
320 bool called = false;
tommie7251592017-07-14 14:44:46 -0700321 other_thread->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700322 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000323
324 EXPECT_TRUE(called);
325}
326
327// Verifies that if thread A invokes a call on thread B and thread C is trying
328// to invoke A at the same time, thread A does not handle C's invoke while
329// invoking B.
330TEST(ThreadTest, ThreeThreadsInvoke) {
331 AutoThread thread;
332 Thread* thread_a = Thread::Current();
tommie7251592017-07-14 14:44:46 -0700333 auto thread_b = Thread::CreateWithSocketServer();
334 auto thread_c = Thread::CreateWithSocketServer();
335 thread_b->Start();
336 thread_c->Start();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000337
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000338 class LockedBool {
339 public:
340 explicit LockedBool(bool value) : value_(value) {}
341
342 void Set(bool value) {
343 CritScope lock(&crit_);
344 value_ = value;
345 }
346
347 bool Get() {
348 CritScope lock(&crit_);
349 return value_;
350 }
351
352 private:
353 CriticalSection crit_;
danilchap3c6abd22017-09-06 05:46:29 -0700354 bool value_ RTC_GUARDED_BY(crit_);
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000355 };
356
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000357 struct LocalFuncs {
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000358 static void Set(LockedBool* out) { out->Set(true); }
359 static void InvokeSet(Thread* thread, LockedBool* out) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700360 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000361 }
362
363 // Set |out| true and call InvokeSet on |thread|.
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000364 static void SetAndInvokeSet(LockedBool* out,
365 Thread* thread,
366 LockedBool* out_inner) {
367 out->Set(true);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000368 InvokeSet(thread, out_inner);
369 }
370
371 // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until
372 // |thread1| starts the call.
deadbeef162cb532017-02-23 17:10:07 -0800373 static void AsyncInvokeSetAndWait(AsyncInvoker* invoker,
374 Thread* thread1,
375 Thread* thread2,
376 LockedBool* out) {
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000377 CriticalSection crit;
378 LockedBool async_invoked(false);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000379
deadbeef162cb532017-02-23 17:10:07 -0800380 invoker->AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700381 RTC_FROM_HERE, thread1,
382 Bind(&SetAndInvokeSet, &async_invoked, thread2, out));
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000383
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000384 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000385 }
386 };
387
deadbeef162cb532017-02-23 17:10:07 -0800388 AsyncInvoker invoker;
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000389 LockedBool thread_a_called(false);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000390
391 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A.
392 // Thread B returns when C receives the call and C should be blocked until A
393 // starts to process messages.
tommie7251592017-07-14 14:44:46 -0700394 thread_b->Invoke<void>(RTC_FROM_HERE,
395 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker,
396 thread_c.get(), thread_a, &thread_a_called));
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000397 EXPECT_FALSE(thread_a_called.Get());
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000398
pbos@webrtc.orge93cbd12014-10-15 14:54:56 +0000399 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000400}
401
jbauch25d1f282016-02-05 00:25:02 -0800402// Set the name on a thread when the underlying QueueDestroyed signal is
403// triggered. This causes an error if the object is already partially
404// destroyed.
405class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> {
406 public:
407 SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) {
408 thread->SignalQueueDestroyed.connect(
409 this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed);
410 }
411
412 void OnQueueDestroyed() {
413 // Makes sure that if we access the Thread while it's being destroyed, that
414 // it doesn't cause a problem because the vtable has been modified.
415 thread_->SetName("foo", nullptr);
416 }
417
418 private:
419 Thread* thread_;
420};
421
422TEST(ThreadTest, SetNameOnSignalQueueDestroyed) {
tommie7251592017-07-14 14:44:46 -0700423 auto thread1 = Thread::CreateWithSocketServer();
424 SetNameOnSignalQueueDestroyedTester tester1(thread1.get());
425 thread1.reset();
jbauch25d1f282016-02-05 00:25:02 -0800426
427 Thread* thread2 = new AutoThread();
428 SetNameOnSignalQueueDestroyedTester tester2(thread2);
429 delete thread2;
jbauch25d1f282016-02-05 00:25:02 -0800430}
431
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200432class AsyncInvokeTest : public ::testing::Test {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000433 public:
434 void IntCallback(int value) {
435 EXPECT_EQ(expected_thread_, Thread::Current());
436 int_value_ = value;
437 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000438 void SetExpectedThreadForIntCallback(Thread* thread) {
439 expected_thread_ = thread;
440 }
441
442 protected:
443 enum { kWaitTimeout = 1000 };
Yves Gerey665174f2018-06-19 15:03:05 +0200444 AsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000445
446 int int_value_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000447 Thread* expected_thread_;
448};
449
henrike@webrtc.orge30dab72014-10-09 15:41:40 +0000450TEST_F(AsyncInvokeTest, FireAndForget) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000451 AsyncInvoker invoker;
452 // Create and start the thread.
tommie7251592017-07-14 14:44:46 -0700453 auto thread = Thread::CreateWithSocketServer();
454 thread->Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000455 // Try calling functor.
nissed9b75be2015-11-16 00:54:07 -0800456 AtomicBool called;
tommie7251592017-07-14 14:44:46 -0700457 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called));
nissed9b75be2015-11-16 00:54:07 -0800458 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
tommie7251592017-07-14 14:44:46 -0700459 thread->Stop();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000460}
461
Cameron Pickettd132ce12018-03-12 16:07:37 -0700462TEST_F(AsyncInvokeTest, NonCopyableFunctor) {
463 AsyncInvoker invoker;
464 // Create and start the thread.
465 auto thread = Thread::CreateWithSocketServer();
466 thread->Start();
467 // Try calling functor.
468 AtomicBool called;
469 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorD(&called));
470 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
471 thread->Stop();
472}
473
deadbeef162cb532017-02-23 17:10:07 -0800474TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) {
475 // Use these events to get in a state where the functor is in the middle of
476 // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE"
477 // is run.
Niels Möllerc572ff32018-11-07 08:43:50 +0100478 Event functor_started;
479 Event functor_continue;
480 Event functor_finished;
deadbeef162cb532017-02-23 17:10:07 -0800481
tommie7251592017-07-14 14:44:46 -0700482 auto thread = Thread::CreateWithSocketServer();
483 thread->Start();
deadbeef162cb532017-02-23 17:10:07 -0800484 volatile bool invoker_destroyed = false;
485 {
deadbeef3af63b02017-08-08 17:59:47 -0700486 auto functor = [&functor_started, &functor_continue, &functor_finished,
487 &invoker_destroyed] {
488 functor_started.Set();
489 functor_continue.Wait(Event::kForever);
490 rtc::Thread::Current()->SleepMs(kWaitTimeout);
491 EXPECT_FALSE(invoker_destroyed);
492 functor_finished.Set();
493 };
deadbeef162cb532017-02-23 17:10:07 -0800494 AsyncInvoker invoker;
deadbeef3af63b02017-08-08 17:59:47 -0700495 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), functor);
deadbeef162cb532017-02-23 17:10:07 -0800496 functor_started.Wait(Event::kForever);
deadbeefaea92932017-05-23 12:55:03 -0700497
deadbeef3af63b02017-08-08 17:59:47 -0700498 // Destroy the invoker while the functor is still executing (doing
499 // SleepMs).
deadbeefaea92932017-05-23 12:55:03 -0700500 functor_continue.Set();
deadbeef162cb532017-02-23 17:10:07 -0800501 }
502
503 // If the destructor DIDN'T wait for the functor to finish executing, it will
504 // hit the EXPECT_FALSE(invoker_destroyed) after it finishes sleeping for a
505 // second.
506 invoker_destroyed = true;
507 functor_finished.Wait(Event::kForever);
508}
509
deadbeef3af63b02017-08-08 17:59:47 -0700510// Variant of the above test where the async-invoked task calls AsyncInvoke
511// *again*, for the thread on which the AsyncInvoker is currently being
512// destroyed. This shouldn't deadlock or crash; this second invocation should
513// just be ignored.
514TEST_F(AsyncInvokeTest, KillInvokerDuringExecuteWithReentrantInvoke) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100515 Event functor_started;
deadbeef3af63b02017-08-08 17:59:47 -0700516 // Flag used to verify that the recursively invoked task never actually runs.
517 bool reentrant_functor_run = false;
518
519 Thread* main = Thread::Current();
Niels Möller5a96a0e2019-04-30 11:45:58 +0200520 Thread thread(absl::make_unique<NullSocketServer>());
deadbeef3af63b02017-08-08 17:59:47 -0700521 thread.Start();
522 {
523 AsyncInvoker invoker;
524 auto reentrant_functor = [&reentrant_functor_run] {
525 reentrant_functor_run = true;
526 };
527 auto functor = [&functor_started, &invoker, main, reentrant_functor] {
528 functor_started.Set();
529 Thread::Current()->SleepMs(kWaitTimeout);
530 invoker.AsyncInvoke<void>(RTC_FROM_HERE, main, reentrant_functor);
531 };
532 // This queues a task on |thread| to sleep for |kWaitTimeout| then queue a
533 // task on |main|. But this second queued task should never run, since the
534 // destructor will be entered before it's even invoked.
535 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, functor);
536 functor_started.Wait(Event::kForever);
537 }
538 EXPECT_FALSE(reentrant_functor_run);
539}
540
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000541TEST_F(AsyncInvokeTest, Flush) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000542 AsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800543 AtomicBool flag1;
544 AtomicBool flag2;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000545 // Queue two async calls to the current thread.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700546 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1));
547 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000548 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800549 EXPECT_FALSE(flag1.get());
550 EXPECT_FALSE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000551 // Force them to run now.
552 invoker.Flush(Thread::Current());
nissed9b75be2015-11-16 00:54:07 -0800553 EXPECT_TRUE(flag1.get());
554 EXPECT_TRUE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000555}
556
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000557TEST_F(AsyncInvokeTest, FlushWithIds) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558 AsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800559 AtomicBool flag1;
560 AtomicBool flag2;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000561 // Queue two async calls to the current thread, one with a message id.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700562 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000563 5);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700564 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000565 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800566 EXPECT_FALSE(flag1.get());
567 EXPECT_FALSE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000568 // Execute pending calls with id == 5.
569 invoker.Flush(Thread::Current(), 5);
nissed9b75be2015-11-16 00:54:07 -0800570 EXPECT_TRUE(flag1.get());
571 EXPECT_FALSE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000572 flag1 = false;
573 // Execute all pending calls. The id == 5 call should not execute again.
574 invoker.Flush(Thread::Current());
nissed9b75be2015-11-16 00:54:07 -0800575 EXPECT_FALSE(flag1.get());
576 EXPECT_TRUE(flag2.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000577}
578
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200579class GuardedAsyncInvokeTest : public ::testing::Test {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200580 public:
581 void IntCallback(int value) {
582 EXPECT_EQ(expected_thread_, Thread::Current());
583 int_value_ = value;
584 }
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200585 void SetExpectedThreadForIntCallback(Thread* thread) {
586 expected_thread_ = thread;
587 }
588
589 protected:
590 const static int kWaitTimeout = 1000;
Yves Gerey665174f2018-06-19 15:03:05 +0200591 GuardedAsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {}
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200592
593 int int_value_;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200594 Thread* expected_thread_;
595};
596
597// Functor for creating an invoker.
598struct CreateInvoker {
jbauch555604a2016-04-26 03:13:22 -0700599 CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker)
600 : invoker_(invoker) {}
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200601 void operator()() { invoker_->reset(new GuardedAsyncInvoker()); }
jbauch555604a2016-04-26 03:13:22 -0700602 std::unique_ptr<GuardedAsyncInvoker>* invoker_;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200603};
604
605// Test that we can call AsyncInvoke<void>() after the thread died.
606TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) {
607 // Create and start the thread.
tommie7251592017-07-14 14:44:46 -0700608 std::unique_ptr<Thread> thread(Thread::Create());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200609 thread->Start();
jbauch555604a2016-04-26 03:13:22 -0700610 std::unique_ptr<GuardedAsyncInvoker> invoker;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200611 // Create the invoker on |thread|.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700612 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200613 // Kill |thread|.
614 thread = nullptr;
615 // Try calling functor.
nissed9b75be2015-11-16 00:54:07 -0800616 AtomicBool called;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700617 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called)));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200618 // With thread gone, nothing should happen.
nissed9b75be2015-11-16 00:54:07 -0800619 WAIT(called.get(), kWaitTimeout);
620 EXPECT_FALSE(called.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200621}
622
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200623// The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker
624// when Thread is still alive.
625TEST_F(GuardedAsyncInvokeTest, FireAndForget) {
626 GuardedAsyncInvoker invoker;
627 // Try calling functor.
nissed9b75be2015-11-16 00:54:07 -0800628 AtomicBool called;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700629 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called)));
nissed9b75be2015-11-16 00:54:07 -0800630 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200631}
632
Cameron Pickettd132ce12018-03-12 16:07:37 -0700633TEST_F(GuardedAsyncInvokeTest, NonCopyableFunctor) {
634 GuardedAsyncInvoker invoker;
635 // Try calling functor.
636 AtomicBool called;
637 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorD(&called)));
638 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
639}
640
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200641TEST_F(GuardedAsyncInvokeTest, Flush) {
642 GuardedAsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800643 AtomicBool flag1;
644 AtomicBool flag2;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200645 // Queue two async calls to the current thread.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700646 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1)));
647 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2)));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200648 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800649 EXPECT_FALSE(flag1.get());
650 EXPECT_FALSE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200651 // Force them to run now.
652 EXPECT_TRUE(invoker.Flush());
nissed9b75be2015-11-16 00:54:07 -0800653 EXPECT_TRUE(flag1.get());
654 EXPECT_TRUE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200655}
656
657TEST_F(GuardedAsyncInvokeTest, FlushWithIds) {
658 GuardedAsyncInvoker invoker;
nissed9b75be2015-11-16 00:54:07 -0800659 AtomicBool flag1;
660 AtomicBool flag2;
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200661 // Queue two async calls to the current thread, one with a message id.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700662 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1), 5));
663 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2)));
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200664 // Because we haven't pumped messages, these should not have run yet.
nissed9b75be2015-11-16 00:54:07 -0800665 EXPECT_FALSE(flag1.get());
666 EXPECT_FALSE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200667 // Execute pending calls with id == 5.
668 EXPECT_TRUE(invoker.Flush(5));
nissed9b75be2015-11-16 00:54:07 -0800669 EXPECT_TRUE(flag1.get());
670 EXPECT_FALSE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200671 flag1 = false;
672 // Execute all pending calls. The id == 5 call should not execute again.
673 EXPECT_TRUE(invoker.Flush());
nissed9b75be2015-11-16 00:54:07 -0800674 EXPECT_FALSE(flag1.get());
675 EXPECT_TRUE(flag2.get());
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200676}
Mirko Bonadeie10b1632018-12-11 18:43:40 +0100677
Henrik Boströmba4dcc32019-02-28 09:34:06 +0100678void ThreadIsCurrent(Thread* thread, bool* result, Event* event) {
679 *result = thread->IsCurrent();
680 event->Set();
681}
682
683void WaitAndSetEvent(Event* wait_event, Event* set_event) {
684 wait_event->Wait(Event::kForever);
685 set_event->Set();
686}
687
688// A functor that keeps track of the number of copies and moves.
689class LifeCycleFunctor {
690 public:
691 struct Stats {
692 size_t copy_count = 0;
693 size_t move_count = 0;
694 };
695
696 LifeCycleFunctor(Stats* stats, Event* event) : stats_(stats), event_(event) {}
697 LifeCycleFunctor(const LifeCycleFunctor& other) { *this = other; }
698 LifeCycleFunctor(LifeCycleFunctor&& other) { *this = std::move(other); }
699
700 LifeCycleFunctor& operator=(const LifeCycleFunctor& other) {
701 stats_ = other.stats_;
702 event_ = other.event_;
703 ++stats_->copy_count;
704 return *this;
705 }
706
707 LifeCycleFunctor& operator=(LifeCycleFunctor&& other) {
708 stats_ = other.stats_;
709 event_ = other.event_;
710 ++stats_->move_count;
711 return *this;
712 }
713
714 void operator()() { event_->Set(); }
715
716 private:
717 Stats* stats_;
718 Event* event_;
719};
720
721// A functor that verifies the thread it was destroyed on.
722class DestructionFunctor {
723 public:
724 DestructionFunctor(Thread* thread, bool* thread_was_current, Event* event)
725 : thread_(thread),
726 thread_was_current_(thread_was_current),
727 event_(event) {}
728 ~DestructionFunctor() {
729 // Only signal the event if this was the functor that was invoked to avoid
730 // the event being signaled due to the destruction of temporary/moved
731 // versions of this object.
732 if (was_invoked_) {
733 *thread_was_current_ = thread_->IsCurrent();
734 event_->Set();
735 }
736 }
737
738 void operator()() { was_invoked_ = true; }
739
740 private:
741 Thread* thread_;
742 bool* thread_was_current_;
743 Event* event_;
744 bool was_invoked_ = false;
745};
746
747TEST(ThreadPostTaskTest, InvokesWithBind) {
748 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
749 background_thread->Start();
750
751 Event event;
752 background_thread->PostTask(RTC_FROM_HERE, Bind(&Event::Set, &event));
753 event.Wait(Event::kForever);
754}
755
756TEST(ThreadPostTaskTest, InvokesWithLambda) {
757 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
758 background_thread->Start();
759
760 Event event;
761 background_thread->PostTask(RTC_FROM_HERE, [&event] { event.Set(); });
762 event.Wait(Event::kForever);
763}
764
765TEST(ThreadPostTaskTest, InvokesWithCopiedFunctor) {
766 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
767 background_thread->Start();
768
769 LifeCycleFunctor::Stats stats;
770 Event event;
771 LifeCycleFunctor functor(&stats, &event);
772 background_thread->PostTask(RTC_FROM_HERE, functor);
773 event.Wait(Event::kForever);
774
775 EXPECT_EQ(1u, stats.copy_count);
776 EXPECT_EQ(0u, stats.move_count);
777}
778
779TEST(ThreadPostTaskTest, InvokesWithMovedFunctor) {
780 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
781 background_thread->Start();
782
783 LifeCycleFunctor::Stats stats;
784 Event event;
785 LifeCycleFunctor functor(&stats, &event);
786 background_thread->PostTask(RTC_FROM_HERE, std::move(functor));
787 event.Wait(Event::kForever);
788
789 EXPECT_EQ(0u, stats.copy_count);
790 EXPECT_EQ(1u, stats.move_count);
791}
792
793TEST(ThreadPostTaskTest, InvokesWithReferencedFunctorShouldCopy) {
794 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
795 background_thread->Start();
796
797 LifeCycleFunctor::Stats stats;
798 Event event;
799 LifeCycleFunctor functor(&stats, &event);
800 LifeCycleFunctor& functor_ref = functor;
801 background_thread->PostTask(RTC_FROM_HERE, functor_ref);
802 event.Wait(Event::kForever);
803
804 EXPECT_EQ(1u, stats.copy_count);
805 EXPECT_EQ(0u, stats.move_count);
806}
807
808TEST(ThreadPostTaskTest, InvokesWithCopiedFunctorDestroyedOnTargetThread) {
809 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
810 background_thread->Start();
811
812 Event event;
813 bool was_invoked_on_background_thread = false;
814 DestructionFunctor functor(background_thread.get(),
815 &was_invoked_on_background_thread, &event);
816 background_thread->PostTask(RTC_FROM_HERE, functor);
817 event.Wait(Event::kForever);
818
819 EXPECT_TRUE(was_invoked_on_background_thread);
820}
821
822TEST(ThreadPostTaskTest, InvokesWithMovedFunctorDestroyedOnTargetThread) {
823 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
824 background_thread->Start();
825
826 Event event;
827 bool was_invoked_on_background_thread = false;
828 DestructionFunctor functor(background_thread.get(),
829 &was_invoked_on_background_thread, &event);
830 background_thread->PostTask(RTC_FROM_HERE, std::move(functor));
831 event.Wait(Event::kForever);
832
833 EXPECT_TRUE(was_invoked_on_background_thread);
834}
835
836TEST(ThreadPostTaskTest,
837 InvokesWithReferencedFunctorShouldCopyAndDestroyedOnTargetThread) {
838 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
839 background_thread->Start();
840
841 Event event;
842 bool was_invoked_on_background_thread = false;
843 DestructionFunctor functor(background_thread.get(),
844 &was_invoked_on_background_thread, &event);
845 DestructionFunctor& functor_ref = functor;
846 background_thread->PostTask(RTC_FROM_HERE, functor_ref);
847 event.Wait(Event::kForever);
848
849 EXPECT_TRUE(was_invoked_on_background_thread);
850}
851
852TEST(ThreadPostTaskTest, InvokesOnBackgroundThread) {
853 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
854 background_thread->Start();
855
856 Event event;
857 bool was_invoked_on_background_thread = false;
858 background_thread->PostTask(RTC_FROM_HERE,
859 Bind(&ThreadIsCurrent, background_thread.get(),
860 &was_invoked_on_background_thread, &event));
861 event.Wait(Event::kForever);
862
863 EXPECT_TRUE(was_invoked_on_background_thread);
864}
865
866TEST(ThreadPostTaskTest, InvokesAsynchronously) {
867 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
868 background_thread->Start();
869
870 // The first event ensures that SendSingleMessage() is not blocking this
871 // thread. The second event ensures that the message is processed.
872 Event event_set_by_test_thread;
873 Event event_set_by_background_thread;
874 background_thread->PostTask(RTC_FROM_HERE,
875 Bind(&WaitAndSetEvent, &event_set_by_test_thread,
876 &event_set_by_background_thread));
877 event_set_by_test_thread.Set();
878 event_set_by_background_thread.Wait(Event::kForever);
879}
880
881TEST(ThreadPostTaskTest, InvokesInPostedOrder) {
882 std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
883 background_thread->Start();
884
885 Event first;
886 Event second;
887 Event third;
888 Event fourth;
889
890 background_thread->PostTask(RTC_FROM_HERE,
891 Bind(&WaitAndSetEvent, &first, &second));
892 background_thread->PostTask(RTC_FROM_HERE,
893 Bind(&WaitAndSetEvent, &second, &third));
894 background_thread->PostTask(RTC_FROM_HERE,
895 Bind(&WaitAndSetEvent, &third, &fourth));
896
897 // All tasks have been posted before the first one is unblocked.
898 first.Set();
899 // Only if the chain is invoked in posted order will the last event be set.
900 fourth.Wait(Event::kForever);
901}
902
Mirko Bonadeie10b1632018-12-11 18:43:40 +0100903} // namespace
904} // namespace rtc