blob: 9a91d44db5c9f1edb2b6a017a2342689a61b315e [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#ifndef TEST_RUN_LOOP_H_
11#define TEST_RUN_LOOP_H_
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000012
Tommi9e46cf52020-05-04 16:43:05 +020013#include "rtc_base/task_utils/to_queued_task.h"
14#include "rtc_base/thread.h"
Danail Kirov6fcf6ca2018-10-25 10:45:47 -070015
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000016namespace webrtc {
17namespace test {
18
Tommi9e46cf52020-05-04 16:43:05 +020019// 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().
22class 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;
Tommi9e46cf52020-05-04 16:43:05 +020059
60 private:
61 bool fail_next_wait_ = false;
62 };
63
64 class WorkerThread : public rtc::Thread {
65 public:
66 explicit WorkerThread(rtc::SocketServer* ss);
67
68 private:
69 CurrentTaskQueueSetter tq_setter_;
70 };
71
72 FakeSocketServer socket_server_;
73 WorkerThread worker_thread_{&socket_server_};
74};
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000075
76} // namespace test
77} // namespace webrtc
78
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020079#endif // TEST_RUN_LOOP_H_