From 07122bc87ea6046f47d96f5b13d337172b4a260c Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Tue, 26 Mar 2019 14:37:01 +0100 Subject: [PATCH] Use TaskQueueForTest instead or TaskQueue in unittests To avoid hidden dependency on GlobalTaskQueueFactory used to construct TaskQueue Bug: webrtc:10284 Change-Id: Iaa08be2827198e16aeb5538ea188d54cab60c1d9 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/128879 Reviewed-by: Karl Wiberg Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/master@{#27291} --- modules/audio_processing/BUILD.gn | 3 ++- modules/audio_processing/aec_dump/BUILD.gn | 2 +- .../aec_dump/aec_dump_unittest.cc | 6 ++--- .../audio_processing_unittest.cc | 8 +++--- .../test/audio_processing_simulator.h | 4 +-- .../audio_processing/test/debug_dump_test.cc | 4 +-- pc/BUILD.gn | 1 + pc/test/fake_periodic_video_source.h | 8 +++--- rtc_base/BUILD.gn | 3 ++- rtc_base/task_queue_unittest.cc | 4 +-- rtc_base/task_utils/BUILD.gn | 1 + .../task_utils/repeating_task_unittest.cc | 20 +++++++-------- rtc_base/weak_ptr_unittest.cc | 18 ++++--------- test/pc/e2e/BUILD.gn | 1 + test/pc/e2e/peer_connection_quality_test.cc | 2 +- test/pc/e2e/peer_connection_quality_test.h | 4 +-- video/overuse_frame_detector_unittest.cc | 25 ++++++++----------- 17 files changed, 54 insertions(+), 60 deletions(-) diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn index d02406fccd..5b64ef37bb 100644 --- a/modules/audio_processing/BUILD.gn +++ b/modules/audio_processing/BUILD.gn @@ -453,6 +453,7 @@ if (rtc_include_tests) { "../../rtc_base:rtc_base_approved", "../../rtc_base:rtc_base_tests_utils", "../../rtc_base:safe_minmax", + "../../rtc_base:task_queue_for_test", "../../rtc_base/system:arch", "../../rtc_base/system:file_wrapper", "../../system_wrappers", @@ -618,7 +619,7 @@ if (rtc_include_tests) { "../../rtc_base:protobuf_utils", "../../rtc_base:rtc_base_approved", "../../rtc_base:rtc_json", - "../../rtc_base:rtc_task_queue", + "../../rtc_base:task_queue_for_test", "../../system_wrappers", "../../test:test_support", "aec_dump", diff --git a/modules/audio_processing/aec_dump/BUILD.gn b/modules/audio_processing/aec_dump/BUILD.gn index 39f09fcfae..e0990af90a 100644 --- a/modules/audio_processing/aec_dump/BUILD.gn +++ b/modules/audio_processing/aec_dump/BUILD.gn @@ -87,7 +87,7 @@ if (rtc_enable_protobuf) { ":aec_dump_impl", "..:audioproc_debug_proto", "../", - "../../../rtc_base:rtc_task_queue", + "../../../rtc_base:task_queue_for_test", "../../../test:fileutils", "../../../test:test_support", "//testing/gtest", diff --git a/modules/audio_processing/aec_dump/aec_dump_unittest.cc b/modules/audio_processing/aec_dump/aec_dump_unittest.cc index 1f9ca0aec3..561fa62ef4 100644 --- a/modules/audio_processing/aec_dump/aec_dump_unittest.cc +++ b/modules/audio_processing/aec_dump/aec_dump_unittest.cc @@ -12,14 +12,14 @@ #include "modules/audio_processing/aec_dump/aec_dump_factory.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "test/gtest.h" #include "test/testsupport/file_utils.h" TEST(AecDumper, APICallsDoNotCrash) { // Note order of initialization: Task queue has to be initialized // before AecDump. - rtc::TaskQueue file_writer_queue("file_writer_queue"); + webrtc::TaskQueueForTest file_writer_queue("file_writer_queue"); const std::string filename = webrtc::test::TempFilename(webrtc::test::OutputPath(), "aec_dump"); @@ -48,7 +48,7 @@ TEST(AecDumper, APICallsDoNotCrash) { } TEST(AecDumper, WriteToFile) { - rtc::TaskQueue file_writer_queue("file_writer_queue"); + webrtc::TaskQueueForTest file_writer_queue("file_writer_queue"); const std::string filename = webrtc::test::TempFilename(webrtc::test::OutputPath(), "aec_dump"); diff --git a/modules/audio_processing/audio_processing_unittest.cc b/modules/audio_processing/audio_processing_unittest.cc index 59feb9be53..d1e5801ca5 100644 --- a/modules/audio_processing/audio_processing_unittest.cc +++ b/modules/audio_processing/audio_processing_unittest.cc @@ -38,7 +38,7 @@ #include "rtc_base/strings/string_builder.h" #include "rtc_base/swap_queue.h" #include "rtc_base/system/arch.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/thread.h" #include "test/gtest.h" #include "test/testsupport/file_utils.h" @@ -1448,7 +1448,7 @@ void ApmTest::ProcessDebugDump(const std::string& in_filename, const std::string& out_filename, Format format, int max_size_bytes) { - rtc::TaskQueue worker_queue("ApmTest_worker_queue"); + TaskQueueForTest worker_queue("ApmTest_worker_queue"); FILE* in_file = fopen(in_filename.c_str(), "rb"); ASSERT_TRUE(in_file != NULL); audioproc::Event event_msg; @@ -1616,7 +1616,7 @@ TEST_F(ApmTest, VerifyDebugDumpFloat) { // TODO(andrew): expand test to verify output. TEST_F(ApmTest, DebugDump) { - rtc::TaskQueue worker_queue("ApmTest_worker_queue"); + TaskQueueForTest worker_queue("ApmTest_worker_queue"); const std::string filename = test::TempFilename(test::OutputPath(), "debug_aec"); { @@ -1650,7 +1650,7 @@ TEST_F(ApmTest, DebugDump) { // TODO(andrew): expand test to verify output. TEST_F(ApmTest, DebugDumpFromFileHandle) { - rtc::TaskQueue worker_queue("ApmTest_worker_queue"); + TaskQueueForTest worker_queue("ApmTest_worker_queue"); const std::string filename = test::TempFilename(test::OutputPath(), "debug_aec"); diff --git a/modules/audio_processing/test/audio_processing_simulator.h b/modules/audio_processing/test/audio_processing_simulator.h index 97b3db523a..5bf276027c 100644 --- a/modules/audio_processing/test/audio_processing_simulator.h +++ b/modules/audio_processing/test/audio_processing_simulator.h @@ -23,7 +23,7 @@ #include "modules/audio_processing/test/fake_recording_device.h" #include "modules/audio_processing/test/test_utils.h" #include "rtc_base/constructor_magic.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/time_utils.h" namespace webrtc { @@ -199,7 +199,7 @@ class AudioProcessingSimulator { int analog_mic_level_; FakeRecordingDevice fake_recording_device_; - rtc::TaskQueue worker_queue_; + TaskQueueForTest worker_queue_; RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioProcessingSimulator); }; diff --git a/modules/audio_processing/test/debug_dump_test.cc b/modules/audio_processing/test/debug_dump_test.cc index be0af9b947..368449553f 100644 --- a/modules/audio_processing/test/debug_dump_test.cc +++ b/modules/audio_processing/test/debug_dump_test.cc @@ -19,7 +19,7 @@ #include "modules/audio_processing/aec_dump/aec_dump_factory.h" #include "modules/audio_processing/test/debug_dump_replayer.h" #include "modules/audio_processing/test/test_utils.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "test/gtest.h" #include "test/testsupport/file_utils.h" @@ -110,7 +110,7 @@ class DebugDumpGenerator { bool enable_pre_amplifier_; - rtc::TaskQueue worker_queue_; + TaskQueueForTest worker_queue_; std::unique_ptr apm_; const std::string dump_file_name_; diff --git a/pc/BUILD.gn b/pc/BUILD.gn index 2b4d111e89..5e2460415a 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -426,6 +426,7 @@ if (rtc_include_tests) { "../rtc_base:gunit_helpers", "../rtc_base:rtc_base_approved", "../rtc_base:rtc_task_queue", + "../rtc_base:task_queue_for_test", "../rtc_base/task_utils:repeating_task", "../rtc_base/third_party/sigslot", "../test:test_support", diff --git a/pc/test/fake_periodic_video_source.h b/pc/test/fake_periodic_video_source.h index 00b2e21050..1fcb0f6d1b 100644 --- a/pc/test/fake_periodic_video_source.h +++ b/pc/test/fake_periodic_video_source.h @@ -17,7 +17,7 @@ #include "api/video/video_source_interface.h" #include "media/base/fake_frame_source.h" #include "media/base/video_broadcaster.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/task_utils/repeating_task.h" namespace webrtc { @@ -44,8 +44,8 @@ class FakePeriodicVideoSource final config.height, config.frame_interval_ms * rtc::kNumMicrosecsPerMillisec, config.timestamp_offset_ms * rtc::kNumMicrosecsPerMillisec), - task_queue_( - absl::make_unique("FakePeriodicVideoTrackSource")) { + task_queue_(absl::make_unique( + "FakePeriodicVideoTrackSource")) { thread_checker_.DetachFromThread(); frame_source_.SetRotation(config.rotation); @@ -82,7 +82,7 @@ class FakePeriodicVideoSource final rtc::VideoBroadcaster broadcaster_; cricket::FakeFrameSource frame_source_; - std::unique_ptr task_queue_; + std::unique_ptr task_queue_; }; } // namespace webrtc diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index d362c98db7..46c7138892 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -1284,6 +1284,7 @@ if (rtc_include_tests) { ":rtc_base_tests_main", ":rtc_base_tests_utils", ":rtc_task_queue", + ":task_queue_for_test", "../test:test_support", "//third_party/abseil-cpp/absl/memory", ] @@ -1319,7 +1320,7 @@ if (rtc_include_tests) { ":rtc_base_tests_main", ":rtc_base_tests_utils", ":rtc_event", - ":rtc_task_queue", + ":task_queue_for_test", ":weak_ptr", "../test:test_support", "//third_party/abseil-cpp/absl/memory", diff --git a/rtc_base/task_queue_unittest.cc b/rtc_base/task_queue_unittest.cc index 5c80c4fd54..a7148dcdd1 100644 --- a/rtc_base/task_queue_unittest.cc +++ b/rtc_base/task_queue_unittest.cc @@ -23,7 +23,7 @@ #include "absl/memory/memory.h" #include "rtc_base/bind.h" #include "rtc_base/event.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/time_utils.h" #include "test/gtest.h" @@ -64,7 +64,7 @@ TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) { static const char kQueueName[] = "PostDelayedHighRes"; Event event; - TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH); + webrtc::TaskQueueForTest queue(kQueueName, TaskQueue::Priority::HIGH); uint32_t start = Time(); queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 3); diff --git a/rtc_base/task_utils/BUILD.gn b/rtc_base/task_utils/BUILD.gn index 16204d159c..e562779c68 100644 --- a/rtc_base/task_utils/BUILD.gn +++ b/rtc_base/task_utils/BUILD.gn @@ -46,6 +46,7 @@ if (rtc_include_tests) { ":repeating_task", "..:rtc_base_approved", "..:rtc_task_queue", + "..:task_queue_for_test", "../../test:test_support", "//third_party/abseil-cpp/absl/memory", ] diff --git a/rtc_base/task_utils/repeating_task_unittest.cc b/rtc_base/task_utils/repeating_task_unittest.cc index 244bb8e403..6f8e7faebc 100644 --- a/rtc_base/task_utils/repeating_task_unittest.cc +++ b/rtc_base/task_utils/repeating_task_unittest.cc @@ -15,7 +15,7 @@ #include "absl/memory/memory.h" #include "rtc_base/event.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/task_utils/repeating_task.h" #include "test/gmock.h" #include "test/gtest.h" @@ -68,7 +68,7 @@ TEST(RepeatingTaskTest, TaskIsStoppedOnStop) { const int kShortIntervalCount = 4; const int kMargin = 1; - rtc::TaskQueue task_queue("TestQueue"); + TaskQueueForTest task_queue("TestQueue"); std::atomic_int counter(0); auto handle = RepeatingTaskHandle::Start(task_queue.Get(), [&] { if (++counter >= kShortIntervalCount) @@ -96,7 +96,7 @@ TEST(RepeatingTaskTest, CompensatesForLongRunTime) { const int kSleepAtCount = 3; std::atomic_int counter(0); - rtc::TaskQueue task_queue("TestQueue"); + TaskQueueForTest task_queue("TestQueue"); RepeatingTaskHandle::Start(task_queue.Get(), [&] { if (++counter == kSleepAtCount) Sleep(kSleepDuration); @@ -110,7 +110,7 @@ TEST(RepeatingTaskTest, CompensatesForLongRunTime) { TEST(RepeatingTaskTest, CompensatesForShortRunTime) { std::atomic_int counter(0); - rtc::TaskQueue task_queue("TestQueue"); + TaskQueueForTest task_queue("TestQueue"); RepeatingTaskHandle::Start(task_queue.Get(), [&] { ++counter; // Sleeping for the 10 ms should be compensated. @@ -129,7 +129,7 @@ TEST(RepeatingTaskTest, CancelDelayedTaskBeforeItRuns) { MockClosure mock; EXPECT_CALL(mock, Call).Times(0); EXPECT_CALL(mock, Delete).WillOnce(Invoke([&done] { done.Set(); })); - rtc::TaskQueue task_queue("queue"); + TaskQueueForTest task_queue("queue"); auto handle = RepeatingTaskHandle::DelayedStart( task_queue.Get(), TimeDelta::ms(100), MoveOnlyClosure(&mock)); handle.PostStop(); @@ -141,7 +141,7 @@ TEST(RepeatingTaskTest, CancelTaskAfterItRuns) { MockClosure mock; EXPECT_CALL(mock, Call).WillOnce(Return(TimeDelta::ms(100))); EXPECT_CALL(mock, Delete).WillOnce(Invoke([&done] { done.Set(); })); - rtc::TaskQueue task_queue("queue"); + TaskQueueForTest task_queue("queue"); auto handle = RepeatingTaskHandle::Start(task_queue.Get(), MoveOnlyClosure(&mock)); handle.PostStop(); @@ -150,7 +150,7 @@ TEST(RepeatingTaskTest, CancelTaskAfterItRuns) { TEST(RepeatingTaskTest, TaskCanStopItself) { std::atomic_int counter(0); - rtc::TaskQueue task_queue("TestQueue"); + TaskQueueForTest task_queue("TestQueue"); RepeatingTaskHandle handle; task_queue.PostTask([&] { handle = RepeatingTaskHandle::Start(task_queue.Get(), [&] { @@ -172,7 +172,7 @@ TEST(RepeatingTaskTest, ZeroReturnValueRepostsTheTask) { done.Set(); return kTimeout; })); - rtc::TaskQueue task_queue("queue"); + TaskQueueForTest task_queue("queue"); RepeatingTaskHandle::Start(task_queue.Get(), MoveOnlyClosure(&closure)); EXPECT_TRUE(done.Wait(kTimeout.ms())); } @@ -187,7 +187,7 @@ TEST(RepeatingTaskTest, StartPeriodicTask) { done.Set(); return kTimeout; })); - rtc::TaskQueue task_queue("queue"); + TaskQueueForTest task_queue("queue"); RepeatingTaskHandle::Start(task_queue.Get(), closure.AsStdFunction()); EXPECT_TRUE(done.Wait(kTimeout.ms())); } @@ -205,7 +205,7 @@ TEST(RepeatingTaskTest, Example) { }); } }; - rtc::TaskQueue task_queue("queue"); + TaskQueueForTest task_queue("queue"); auto object = absl::make_unique(); // Create and start the periodic task. RepeatingTaskHandle handle; diff --git a/rtc_base/weak_ptr_unittest.cc b/rtc_base/weak_ptr_unittest.cc index 8f09a33db4..1fdd9feab4 100644 --- a/rtc_base/weak_ptr_unittest.cc +++ b/rtc_base/weak_ptr_unittest.cc @@ -13,7 +13,7 @@ #include "absl/memory/memory.h" #include "rtc_base/event.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/weak_ptr.h" #include "test/gtest.h" @@ -203,13 +203,8 @@ TEST(WeakPtrTest, HasWeakPtrs) { template std::unique_ptr NewObjectCreatedOnTaskQueue() { std::unique_ptr obj; - TaskQueue queue("NewObjectCreatedOnTaskQueue"); - Event event; - queue.PostTask([&event, &obj] { - obj.reset(new T()); - event.Set(); - }); - EXPECT_TRUE(event.Wait(1000)); + webrtc::TaskQueueForTest queue("NewObjectCreatedOnTaskQueue"); + queue.SendTask([&] { obj = absl::make_unique(); }); return obj; } @@ -230,15 +225,12 @@ TEST(WeakPtrTest, WeakPtrInitiateAndUseOnDifferentThreads) { auto target = absl::make_unique(); // Create weak ptr on main thread WeakPtr weak_ptr = target->factory.GetWeakPtr(); - rtc::TaskQueue queue("queue"); - rtc::Event done; - queue.PostTask([&] { + webrtc::TaskQueueForTest queue("queue"); + queue.SendTask([&] { // Dereference and invalide weak_ptr on another thread. EXPECT_EQ(weak_ptr.get(), target.get()); target.reset(); - done.Set(); }); - EXPECT_TRUE(done.Wait(1000)); } } // namespace rtc diff --git a/test/pc/e2e/BUILD.gn b/test/pc/e2e/BUILD.gn index 72a54d4212..65ddb7f8a5 100644 --- a/test/pc/e2e/BUILD.gn +++ b/test/pc/e2e/BUILD.gn @@ -250,6 +250,7 @@ if (rtc_include_tests) { "../../../rtc_base:rtc_base_approved", "../../../rtc_base:rtc_task_queue", "../../../rtc_base:safe_conversions", + "../../../rtc_base:task_queue_for_test", "../../../rtc_base/task_utils:repeating_task", "../../../system_wrappers", "../../../test:fileutils", diff --git a/test/pc/e2e/peer_connection_quality_test.cc b/test/pc/e2e/peer_connection_quality_test.cc index f743f47cee..008842977d 100644 --- a/test/pc/e2e/peer_connection_quality_test.cc +++ b/test/pc/e2e/peer_connection_quality_test.cc @@ -301,7 +301,7 @@ void PeerConnectionE2EQualityTest::Run( } // Create a |task_queue_|. - task_queue_ = absl::make_unique("pc_e2e_quality_test"); + task_queue_ = absl::make_unique("pc_e2e_quality_test"); // Setup call. signaling_thread->Invoke( RTC_FROM_HERE, diff --git a/test/pc/e2e/peer_connection_quality_test.h b/test/pc/e2e/peer_connection_quality_test.h index 9c2c952e73..a8846705c9 100644 --- a/test/pc/e2e/peer_connection_quality_test.h +++ b/test/pc/e2e/peer_connection_quality_test.h @@ -21,7 +21,7 @@ #include "api/units/time_delta.h" #include "api/units/timestamp.h" #include "pc/test/frame_generator_capturer_video_track_source.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/task_utils/repeating_task.h" #include "rtc_base/thread.h" #include "rtc_base/thread_annotations.h" @@ -252,7 +252,7 @@ class PeerConnectionE2EQualityTest // Task queue, that is used for running activities during test call. // This task queue will be created before call set up and will be destroyed // immediately before call tear down. - std::unique_ptr task_queue_; + std::unique_ptr task_queue_; }; } // namespace webrtc_pc_e2e diff --git a/video/overuse_frame_detector_unittest.cc b/video/overuse_frame_detector_unittest.cc index b356d1d200..c9ba11ce8c 100644 --- a/video/overuse_frame_detector_unittest.cc +++ b/video/overuse_frame_detector_unittest.cc @@ -17,6 +17,7 @@ #include "rtc_base/event.h" #include "rtc_base/fake_clock.h" #include "rtc_base/random.h" +#include "rtc_base/task_queue_for_test.h" #include "test/gmock.h" #include "test/gtest.h" #include "video/overuse_frame_detector.h" @@ -425,15 +426,13 @@ TEST_F(OveruseFrameDetectorTest, UpdatesExistingSamples) { } TEST_F(OveruseFrameDetectorTest, RunOnTqNormalUsage) { - rtc::TaskQueue queue("OveruseFrameDetectorTestQueue"); + TaskQueueForTest queue("OveruseFrameDetectorTestQueue"); + + queue.SendTask([&] { + overuse_detector_->StartCheckForOveruse(&queue, options_, observer_); + }); rtc::Event event; - queue.PostTask([this, &event, &queue] { - overuse_detector_->StartCheckForOveruse(&queue, options_, observer_); - event.Set(); - }); - event.Wait(rtc::Event::kForever); - // Expect NormalUsage(). When called, stop the |overuse_detector_| and then // set |event| to end the test. EXPECT_CALL(mock_observer_, AdaptUp(reason_)) @@ -905,15 +904,13 @@ TEST_F(OveruseFrameDetectorTest2, UpdatesExistingSamples) { } TEST_F(OveruseFrameDetectorTest2, RunOnTqNormalUsage) { - rtc::TaskQueue queue("OveruseFrameDetectorTestQueue"); + TaskQueueForTest queue("OveruseFrameDetectorTestQueue"); + + queue.SendTask([&] { + overuse_detector_->StartCheckForOveruse(&queue, options_, observer_); + }); rtc::Event event; - queue.PostTask([this, &event, &queue] { - overuse_detector_->StartCheckForOveruse(&queue, options_, observer_); - event.Set(); - }); - event.Wait(rtc::Event::kForever); - // Expect NormalUsage(). When called, stop the |overuse_detector_| and then // set |event| to end the test. EXPECT_CALL(mock_observer_, AdaptUp(reason_))