blob: 1987a05785475cfd61bd8a845b6b9daff4f101e2 [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
Danil Chapovalov9c125c62022-07-07 20:29:30 +020013#include <utility>
14
15#include "absl/functional/any_invocable.h"
16#include "api/task_queue/task_queue_base.h"
Tommi9e46cf52020-05-04 16:43:05 +020017#include "rtc_base/thread.h"
Danail Kirov6fcf6ca2018-10-25 10:45:47 -070018
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000019namespace webrtc {
20namespace test {
21
Tommi9e46cf52020-05-04 16:43:05 +020022// This utility class allows you to run a TaskQueue supported interface on the
23// main test thread, call Run() while doing things asynchonously and break
24// the loop (from the same thread) from a callback by calling Quit().
25class RunLoop {
26 public:
27 RunLoop();
28 ~RunLoop();
29
30 TaskQueueBase* task_queue();
31
32 void Run();
33 void Quit();
34
35 void Flush();
36
Danil Chapovalov9c125c62022-07-07 20:29:30 +020037 void PostTask(absl::AnyInvocable<void() &&> task) {
38 task_queue()->PostTask(std::move(task));
Tommi9e46cf52020-05-04 16:43:05 +020039 }
40
41 private:
42 class FakeSocketServer : public rtc::SocketServer {
43 public:
44 FakeSocketServer();
45 ~FakeSocketServer();
46
47 void FailNextWait();
48
49 private:
50 bool Wait(int cms, bool process_io) override;
51 void WakeUp() override;
52
53 rtc::Socket* CreateSocket(int family, int type) override;
Tommi9e46cf52020-05-04 16:43:05 +020054
55 private:
56 bool fail_next_wait_ = false;
57 };
58
59 class WorkerThread : public rtc::Thread {
60 public:
61 explicit WorkerThread(rtc::SocketServer* ss);
62
63 private:
64 CurrentTaskQueueSetter tq_setter_;
65 };
66
67 FakeSocketServer socket_server_;
68 WorkerThread worker_thread_{&socket_server_};
69};
pbos@webrtc.orgadf23a52013-07-10 14:07:56 +000070
71} // namespace test
72} // namespace webrtc
73
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020074#endif // TEST_RUN_LOOP_H_