blob: 8a2bf544024908f052da96d7e680b7cbe759f6c9 [file] [log] [blame]
pbos@webrtc.orgadf23a52013-07-10 14:07:561/*
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 04:47:3110#ifndef TEST_RUN_LOOP_H_
11#define TEST_RUN_LOOP_H_
pbos@webrtc.orgadf23a52013-07-10 14:07:5612
Danil Chapovalov9c125c62022-07-07 18:29:3013#include <utility>
14
15#include "absl/functional/any_invocable.h"
16#include "api/task_queue/task_queue_base.h"
Tommi9e46cf52020-05-04 14:43:0517#include "rtc_base/thread.h"
Danail Kirov6fcf6ca2018-10-25 17:45:4718
pbos@webrtc.orgadf23a52013-07-10 14:07:5619namespace webrtc {
20namespace test {
21
Tommi9e46cf52020-05-04 14:43:0522// 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 18:29:3037 void PostTask(absl::AnyInvocable<void() &&> task) {
38 task_queue()->PostTask(std::move(task));
Tommi9e46cf52020-05-04 14:43:0539 }
40
41 private:
42 class FakeSocketServer : public rtc::SocketServer {
43 public:
44 FakeSocketServer();
45 ~FakeSocketServer();
46
47 void FailNextWait();
48
49 private:
Markus Handell9a21c492022-08-25 11:40:1350 bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override;
Tommi9e46cf52020-05-04 14:43:0551 void WakeUp() override;
52
53 rtc::Socket* CreateSocket(int family, int type) override;
Tommi9e46cf52020-05-04 14:43:0554
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:5670
71} // namespace test
72} // namespace webrtc
73
Mirko Bonadei92ea95e2017-09-15 04:47:3174#endif // TEST_RUN_LOOP_H_