blob: 80f0bcbdccfdc3bee1f0b6535ca4b3489917ef76 [file] [log] [blame]
Tommi9e46cf52020-05-04 16:43:05 +02001/*
2 * Copyright (c) 2020 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 */
10
11#include "test/run_loop.h"
12
Danil Chapovalov9c125c62022-07-07 20:29:30 +020013#include "api/units/time_delta.h"
Tommi9e46cf52020-05-04 16:43:05 +020014#include "rtc_base/task_queue.h"
Tommi9e46cf52020-05-04 16:43:05 +020015#include "test/gtest.h"
16
17namespace webrtc {
18
19TEST(RunLoopTest, TaskQueueOnThread) {
Tommi9e46cf52020-05-04 16:43:05 +020020 test::RunLoop loop;
21 EXPECT_EQ(TaskQueueBase::Current(), loop.task_queue());
22 EXPECT_TRUE(loop.task_queue()->IsCurrent());
23}
24
25TEST(RunLoopTest, Flush) {
26 test::RunLoop loop;
27 int counter = 0;
28 loop.PostTask([&counter]() { ++counter; });
29 EXPECT_EQ(counter, 0);
30 loop.Flush();
31 EXPECT_EQ(counter, 1);
32}
33
34TEST(RunLoopTest, Delayed) {
35 test::RunLoop loop;
36 bool ran = false;
Danil Chapovalov9c125c62022-07-07 20:29:30 +020037 loop.task_queue()->PostDelayedTask(
Tommi9e46cf52020-05-04 16:43:05 +020038 [&ran, &loop]() {
39 ran = true;
40 loop.Quit();
41 },
Danil Chapovalov9c125c62022-07-07 20:29:30 +020042 TimeDelta::Millis(100));
Tommi9e46cf52020-05-04 16:43:05 +020043 loop.Flush();
44 EXPECT_FALSE(ran);
45 loop.Run();
46 EXPECT_TRUE(ran);
47}
48
49TEST(RunLoopTest, PostAndQuit) {
50 test::RunLoop loop;
51 bool ran = false;
52 loop.PostTask([&ran, &loop]() {
53 ran = true;
54 loop.Quit();
55 });
56 loop.Run();
57 EXPECT_TRUE(ran);
58}
59
60} // namespace webrtc