pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 +0000 | [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 06:47:31 +0200 | [diff] [blame] | 10 | #ifndef TEST_RUN_LOOP_H_ |
| 11 | #define TEST_RUN_LOOP_H_ |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 +0000 | [diff] [blame] | 12 | |
Tommi | 9e46cf5 | 2020-05-04 16:43:05 +0200 | [diff] [blame^] | 13 | #include "rtc_base/task_utils/to_queued_task.h" |
| 14 | #include "rtc_base/thread.h" |
Danail Kirov | 6fcf6ca | 2018-10-25 10:45:47 -0700 | [diff] [blame] | 15 | |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 +0000 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
Tommi | 9e46cf5 | 2020-05-04 16:43:05 +0200 | [diff] [blame^] | 19 | // This utility class allows you to run a TaskQueue supported interface on the |
| 20 | // main test thread, call Run() while doing things asynchonously and break |
| 21 | // the loop (from the same thread) from a callback by calling Quit(). |
| 22 | class RunLoop { |
| 23 | public: |
| 24 | RunLoop(); |
| 25 | ~RunLoop(); |
| 26 | |
| 27 | TaskQueueBase* task_queue(); |
| 28 | |
| 29 | void Run(); |
| 30 | void Quit(); |
| 31 | |
| 32 | void Flush(); |
| 33 | |
| 34 | // Convenience methods since TaskQueueBase doesn't support this sort of magic. |
| 35 | template <typename Closure> |
| 36 | void PostTask(Closure&& task) { |
| 37 | task_queue()->PostTask(ToQueuedTask(std::forward<Closure>(task))); |
| 38 | } |
| 39 | |
| 40 | template <typename Closure> |
| 41 | void PostDelayedTask(Closure&& task, uint32_t milliseconds) { |
| 42 | task_queue()->PostDelayedTask(ToQueuedTask(std::forward<Closure>(task)), |
| 43 | milliseconds); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | class FakeSocketServer : public rtc::SocketServer { |
| 48 | public: |
| 49 | FakeSocketServer(); |
| 50 | ~FakeSocketServer(); |
| 51 | |
| 52 | void FailNextWait(); |
| 53 | |
| 54 | private: |
| 55 | bool Wait(int cms, bool process_io) override; |
| 56 | void WakeUp() override; |
| 57 | |
| 58 | rtc::Socket* CreateSocket(int family, int type) override; |
| 59 | rtc::AsyncSocket* CreateAsyncSocket(int family, int type) override; |
| 60 | |
| 61 | private: |
| 62 | bool fail_next_wait_ = false; |
| 63 | }; |
| 64 | |
| 65 | class WorkerThread : public rtc::Thread { |
| 66 | public: |
| 67 | explicit WorkerThread(rtc::SocketServer* ss); |
| 68 | |
| 69 | private: |
| 70 | CurrentTaskQueueSetter tq_setter_; |
| 71 | }; |
| 72 | |
| 73 | FakeSocketServer socket_server_; |
| 74 | WorkerThread worker_thread_{&socket_server_}; |
| 75 | }; |
pbos@webrtc.org | adf23a5 | 2013-07-10 14:07:56 +0000 | [diff] [blame] | 76 | |
| 77 | } // namespace test |
| 78 | } // namespace webrtc |
| 79 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 80 | #endif // TEST_RUN_LOOP_H_ |