Introduce a RunLoop class that supports the TaskQueue interface
on the current thread.
This simplifies writing async tests that use TaskQueue and doesn't
require spinning up a new thread for simple things. The implementation
is currently based on rtc::Thread, which could also be useful in
some circumstances while migrating code over to TQ.
Remove PressEnterToContinue from the test_common files since
it's very specific and only used from one file.
Bug: none
Change-Id: I8b2c6c40809271a109ec17cf7e1120847645d58a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/174260
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31160}
diff --git a/test/run_loop_unittest.cc b/test/run_loop_unittest.cc
new file mode 100644
index 0000000..a356cc2
--- /dev/null
+++ b/test/run_loop_unittest.cc
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "test/run_loop.h"
+
+#include "rtc_base/task_queue.h"
+#include "rtc_base/task_utils/to_queued_task.h"
+#include "test/gtest.h"
+
+namespace webrtc {
+
+TEST(RunLoopTest, TaskQueueOnThread) {
+ EXPECT_EQ(TaskQueueBase::Current(), nullptr);
+ test::RunLoop loop;
+ EXPECT_EQ(TaskQueueBase::Current(), loop.task_queue());
+ EXPECT_TRUE(loop.task_queue()->IsCurrent());
+}
+
+TEST(RunLoopTest, Flush) {
+ test::RunLoop loop;
+ int counter = 0;
+ loop.PostTask([&counter]() { ++counter; });
+ EXPECT_EQ(counter, 0);
+ loop.Flush();
+ EXPECT_EQ(counter, 1);
+}
+
+TEST(RunLoopTest, Delayed) {
+ test::RunLoop loop;
+ bool ran = false;
+ loop.PostDelayedTask(
+ [&ran, &loop]() {
+ ran = true;
+ loop.Quit();
+ },
+ 100);
+ loop.Flush();
+ EXPECT_FALSE(ran);
+ loop.Run();
+ EXPECT_TRUE(ran);
+}
+
+TEST(RunLoopTest, PostAndQuit) {
+ test::RunLoop loop;
+ bool ran = false;
+ loop.PostTask([&ran, &loop]() {
+ ran = true;
+ loop.Quit();
+ });
+ loop.Run();
+ EXPECT_TRUE(ran);
+}
+
+} // namespace webrtc