Add rtc::Thread::PostDelayedTask

Earlier, rtc::Thread::PostTask was added as a convenient
alternative to MessageHandlers. This CL additionally adds support
for posting delayed tasks in a similar manner.

Bug: webrtc:10294
Change-Id: I0957b59ca2133a882c980bd2ad109fa03d701a16
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161740
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30069}
This commit is contained in:
Steve Anton
2019-12-11 11:21:53 -08:00
committed by Commit Bot
parent 26fe811623
commit bcc1a765fb
3 changed files with 82 additions and 19 deletions

View File

@ -902,6 +902,48 @@ TEST(ThreadPostTaskTest, InvokesInPostedOrder) {
fourth.Wait(Event::kForever);
}
TEST(ThreadPostDelayedTaskTest, InvokesAsynchronously) {
std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
background_thread->Start();
// The first event ensures that SendSingleMessage() is not blocking this
// thread. The second event ensures that the message is processed.
Event event_set_by_test_thread;
Event event_set_by_background_thread;
background_thread->PostDelayedTask(
RTC_FROM_HERE,
Bind(&WaitAndSetEvent, &event_set_by_test_thread,
&event_set_by_background_thread),
/*milliseconds=*/10);
event_set_by_test_thread.Set();
event_set_by_background_thread.Wait(Event::kForever);
}
TEST(ThreadPostDelayedTaskTest, InvokesInDelayOrder) {
std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create());
background_thread->Start();
Event first;
Event second;
Event third;
Event fourth;
background_thread->PostDelayedTask(RTC_FROM_HERE,
Bind(&WaitAndSetEvent, &third, &fourth),
/*milliseconds=*/11);
background_thread->PostDelayedTask(RTC_FROM_HERE,
Bind(&WaitAndSetEvent, &first, &second),
/*milliseconds=*/9);
background_thread->PostDelayedTask(RTC_FROM_HERE,
Bind(&WaitAndSetEvent, &second, &third),
/*milliseconds=*/10);
// All tasks have been posted before the first one is unblocked.
first.Set();
// Only if the chain is invoked in posted order will the last event be set.
fourth.Wait(Event::kForever);
}
class ThreadFactory : public webrtc::TaskQueueFactory {
public:
std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>