blob: d54b4a41371f792b82864667b67dcaafdd71c775 [file] [log] [blame]
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +00001/*
2 * Copyright (c) 2013 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "test/run_loop.h"
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000011
Evan Shrubsole9a999052021-12-12 15:27:00 +010012#include "rtc_base/time_utils.h"
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000013
14namespace webrtc {
15namespace test {
16
Tommi9e46cf52020-05-04 16:43:05 +020017RunLoop::RunLoop() {
18 worker_thread_.WrapCurrent();
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000019}
Tommi9e46cf52020-05-04 16:43:05 +020020
21RunLoop::~RunLoop() {
22 worker_thread_.UnwrapCurrent();
23}
24
25TaskQueueBase* RunLoop::task_queue() {
26 return &worker_thread_;
27}
28
29void RunLoop::Run() {
30 worker_thread_.ProcessMessages(WorkerThread::kForever);
31}
32
33void RunLoop::Quit() {
34 socket_server_.FailNextWait();
35}
36
37void RunLoop::Flush() {
Danil Chapovalov9c125c62022-07-07 20:29:30 +020038 worker_thread_.PostTask([this]() { socket_server_.FailNextWait(); });
Evan Shrubsole9a999052021-12-12 15:27:00 +010039 // If a test clock is used, like with GlobalSimulatedTimeController then the
40 // thread will loop forever since time never increases. Since the clock is
41 // simulated, 0ms can be used as the loop delay, which will process all
42 // messages ready for execution.
43 int cms = rtc::GetClockForTesting() ? 0 : 1000;
44 worker_thread_.ProcessMessages(cms);
Tommi9e46cf52020-05-04 16:43:05 +020045}
46
47RunLoop::FakeSocketServer::FakeSocketServer() = default;
48RunLoop::FakeSocketServer::~FakeSocketServer() = default;
49
50void RunLoop::FakeSocketServer::FailNextWait() {
51 fail_next_wait_ = true;
52}
53
54bool RunLoop::FakeSocketServer::Wait(int cms, bool process_io) {
55 if (fail_next_wait_) {
56 fail_next_wait_ = false;
57 return false;
58 }
59 return true;
60}
61
62void RunLoop::FakeSocketServer::WakeUp() {}
63
64rtc::Socket* RunLoop::FakeSocketServer::CreateSocket(int family, int type) {
65 return nullptr;
66}
67
Tommi9e46cf52020-05-04 16:43:05 +020068RunLoop::WorkerThread::WorkerThread(rtc::SocketServer* ss)
69 : rtc::Thread(ss), tq_setter_(this) {}
70
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000071} // namespace test
72} // namespace webrtc