pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 10 | #include "test/run_loop.h" |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 | [diff] [blame] | 11 | |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 12 | #include "rtc_base/time_utils.h" |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | namespace test { |
| 16 | |
Tommi | 9e46cf5 | 2020-05-04 14:43:05 | [diff] [blame] | 17 | RunLoop::RunLoop() { |
| 18 | worker_thread_.WrapCurrent(); |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 | [diff] [blame] | 19 | } |
Tommi | 9e46cf5 | 2020-05-04 14:43:05 | [diff] [blame] | 20 | |
| 21 | RunLoop::~RunLoop() { |
| 22 | worker_thread_.UnwrapCurrent(); |
| 23 | } |
| 24 | |
| 25 | TaskQueueBase* RunLoop::task_queue() { |
| 26 | return &worker_thread_; |
| 27 | } |
| 28 | |
| 29 | void RunLoop::Run() { |
| 30 | worker_thread_.ProcessMessages(WorkerThread::kForever); |
| 31 | } |
| 32 | |
| 33 | void RunLoop::Quit() { |
| 34 | socket_server_.FailNextWait(); |
| 35 | } |
| 36 | |
| 37 | void RunLoop::Flush() { |
Danil Chapovalov | 9c125c6 | 2022-07-07 18:29:30 | [diff] [blame] | 38 | worker_thread_.PostTask([this]() { socket_server_.FailNextWait(); }); |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 39 | // 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); |
Tommi | 9e46cf5 | 2020-05-04 14:43:05 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | RunLoop::FakeSocketServer::FakeSocketServer() = default; |
| 48 | RunLoop::FakeSocketServer::~FakeSocketServer() = default; |
| 49 | |
| 50 | void RunLoop::FakeSocketServer::FailNextWait() { |
| 51 | fail_next_wait_ = true; |
| 52 | } |
| 53 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 54 | bool RunLoop::FakeSocketServer::Wait(webrtc::TimeDelta max_wait_duration, |
| 55 | bool process_io) { |
Tommi | 9e46cf5 | 2020-05-04 14:43:05 | [diff] [blame] | 56 | if (fail_next_wait_) { |
| 57 | fail_next_wait_ = false; |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | void RunLoop::FakeSocketServer::WakeUp() {} |
| 64 | |
| 65 | rtc::Socket* RunLoop::FakeSocketServer::CreateSocket(int family, int type) { |
| 66 | return nullptr; |
| 67 | } |
| 68 | |
Tommi | 9e46cf5 | 2020-05-04 14:43:05 | [diff] [blame] | 69 | RunLoop::WorkerThread::WorkerThread(rtc::SocketServer* ss) |
| 70 | : rtc::Thread(ss), tq_setter_(this) {} |
| 71 | |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 | [diff] [blame] | 72 | } // namespace test |
| 73 | } // namespace webrtc |