From 123f3453e2df51e797f75652f6ed4c3c052704c2 Mon Sep 17 00:00:00 2001 From: Sebastian Jansson Date: Wed, 13 Mar 2019 11:22:52 +0100 Subject: [PATCH] Cleanup of scenario test framework. * Removing unused return values. * Using TaskQueueForTest to do blocking calls. * Improving naming. This prepares for future work to run scenario tests in simulated time. Bug: webrtc:10365 Change-Id: I2c100e9c20f4b85e85d7b455ea01944f6a14e08f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/127561 Reviewed-by: Artem Titov Commit-Queue: Sebastian Jansson Cr-Commit-Position: refs/heads/master@{#27105} --- test/scenario/BUILD.gn | 2 +- test/scenario/quality_stats.cc | 4 +--- test/scenario/quality_stats.h | 4 ++-- test/scenario/scenario.cc | 31 ++++++++++++++----------------- test/scenario/scenario.h | 17 +++++++++-------- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/test/scenario/BUILD.gn b/test/scenario/BUILD.gn index 849dddcc29..bdedf04fa4 100644 --- a/test/scenario/BUILD.gn +++ b/test/scenario/BUILD.gn @@ -55,7 +55,6 @@ if (rtc_include_tests) { "audio_stream.h", "call_client.cc", "call_client.h", - "call_client.h", "hardware_codecs.cc", "hardware_codecs.h", "network_node.cc", @@ -128,6 +127,7 @@ if (rtc_include_tests) { "../../rtc_base:rtc_base_approved", "../../rtc_base:rtc_base_tests_utils", "../../rtc_base:rtc_task_queue", + "../../rtc_base:rtc_task_queue_for_test", "../../rtc_base:safe_minmax", "../../rtc_base:sequenced_task_checker", "../../system_wrappers", diff --git a/test/scenario/quality_stats.cc b/test/scenario/quality_stats.cc index 0391fe0e06..02399896ea 100644 --- a/test/scenario/quality_stats.cc +++ b/test/scenario/quality_stats.cc @@ -32,9 +32,7 @@ VideoQualityAnalyzer::VideoQualityAnalyzer( } VideoQualityAnalyzer::~VideoQualityAnalyzer() { - rtc::Event event; - task_queue_.PostTask([&event] { event.Set(); }); - event.Wait(rtc::Event::kForever); + task_queue_.SendTask([] {}); } void VideoQualityAnalyzer::OnCapturedFrame(const VideoFrame& frame) { diff --git a/test/scenario/quality_stats.h b/test/scenario/quality_stats.h index 0bfd96e9bc..8afe30f145 100644 --- a/test/scenario/quality_stats.h +++ b/test/scenario/quality_stats.h @@ -20,7 +20,7 @@ #include "api/video/video_frame.h" #include "api/video/video_sink_interface.h" #include "api/video/video_source_interface.h" -#include "rtc_base/task_queue.h" +#include "rtc_base/task_queue_for_test.h" #include "rtc_base/time_utils.h" #include "system_wrappers/include/clock.h" #include "test/logging/log_writer.h" @@ -50,7 +50,7 @@ class VideoQualityAnalyzer { std::vector> frame_info_handlers_; std::deque captured_frames_; - rtc::TaskQueue task_queue_; + rtc::test::TaskQueueForTest task_queue_; }; struct VideoQualityStats { diff --git a/test/scenario/scenario.cc b/test/scenario/scenario.cc index 9b8a1bf214..47b1d912d2 100644 --- a/test/scenario/scenario.cc +++ b/test/scenario/scenario.cc @@ -224,12 +224,11 @@ SimulationNode* Scenario::CreateSimulationNode(NetworkNodeConfig config) { } EmulatedNetworkNode* Scenario::CreateNetworkNode( - NetworkNodeConfig config, std::unique_ptr behavior) { - RTC_DCHECK(config.mode == NetworkNodeConfig::TrafficMode::kCustom); network_nodes_.emplace_back(new EmulatedNetworkNode(std::move(behavior))); EmulatedNetworkNode* network_node = network_nodes_.back().get(); - Every(config.update_frequency, + // TODO(srte): Use the update interval as provided by |behavior|. + Every(TimeDelta::ms(5), [this, network_node] { network_node->Process(Now()); }); return network_node; } @@ -315,24 +314,21 @@ AudioStreamPair* Scenario::CreateAudioStream( return audio_streams_.back().get(); } -RepeatedActivity* Scenario::Every(TimeDelta interval, - std::function function) { +void Scenario::Every(TimeDelta interval, + std::function function) { repeated_activities_.emplace_back(new RepeatedActivity(interval, function)); if (start_time_.IsFinite()) { repeated_activities_.back()->SetStartTime(Now()); } - return repeated_activities_.back().get(); } -RepeatedActivity* Scenario::Every(TimeDelta interval, - std::function function) { +void Scenario::Every(TimeDelta interval, std::function function) { auto function_with_argument = [function](TimeDelta) { function(); }; repeated_activities_.emplace_back( new RepeatedActivity(interval, function_with_argument)); if (start_time_.IsFinite()) { repeated_activities_.back()->SetStartTime(Now()); } - return repeated_activities_.back().get(); } void Scenario::At(TimeDelta offset, std::function function) { @@ -340,24 +336,25 @@ void Scenario::At(TimeDelta offset, std::function function) { } void Scenario::RunFor(TimeDelta duration) { - RunUntil(Duration() + duration); + RunUntil(TimeSinceStart() + duration); } -void Scenario::RunUntil(TimeDelta max_duration) { - RunUntil(max_duration, TimeDelta::PlusInfinity(), []() { return false; }); +void Scenario::RunUntil(TimeDelta target_time_since_start) { + RunUntil(target_time_since_start, TimeDelta::PlusInfinity(), + []() { return false; }); } -void Scenario::RunUntil(TimeDelta max_duration, - TimeDelta poll_interval, +void Scenario::RunUntil(TimeDelta target_time_since_start, + TimeDelta check_interval, std::function exit_function) { if (start_time_.IsInfinite()) Start(); rtc::Event done_; - while (!exit_function() && Duration() < max_duration) { + while (!exit_function() && TimeSinceStart() < target_time_since_start) { Timestamp current_time = Now(); TimeDelta duration = current_time - start_time_; - Timestamp next_time = current_time + poll_interval; + Timestamp next_time = current_time + check_interval; for (auto& activity : repeated_activities_) { activity->Poll(current_time); next_time = std::min(next_time, activity->NextTime()); @@ -423,7 +420,7 @@ Timestamp Scenario::Now() { return Timestamp::us(clock_->TimeInMicroseconds()); } -TimeDelta Scenario::Duration() { +TimeDelta Scenario::TimeSinceStart() { if (start_time_.IsInfinite()) return TimeDelta::Zero(); return Now() - start_time_; diff --git a/test/scenario/scenario.h b/test/scenario/scenario.h index c3fc877289..6de1d78f7d 100644 --- a/test/scenario/scenario.h +++ b/test/scenario/scenario.h @@ -74,7 +74,6 @@ class Scenario { SimulationNode* CreateSimulationNode( std::function config_modifier); EmulatedNetworkNode* CreateNetworkNode( - NetworkNodeConfig config, std::unique_ptr behavior); CallClient* CreateClient(std::string name, CallClientConfig config); @@ -130,9 +129,8 @@ class Scenario { CrossTrafficConfig config); // Runs the provided function with a fixed interval. - RepeatedActivity* Every(TimeDelta interval, - std::function function); - RepeatedActivity* Every(TimeDelta interval, std::function function); + void Every(TimeDelta interval, std::function function); + void Every(TimeDelta interval, std::function function); // Runs the provided function after given duration has passed in a session. void At(TimeDelta offset, std::function function); @@ -145,9 +143,12 @@ class Scenario { // Runs the scenario for the given time or until the exit function returns // true. void RunFor(TimeDelta duration); - void RunUntil(TimeDelta max_duration); - void RunUntil(TimeDelta max_duration, - TimeDelta probe_interval, + void RunUntil(TimeDelta target_time_since_start); + // Will check |exit_function| every |check_interval|. It stops after a check + // if either |target_time_since_start| has passed or if |exit_function| + // returns true. + void RunUntil(TimeDelta target_time_since_start, + TimeDelta check_interval, std::function exit_function); void Start(); void Stop(); @@ -165,7 +166,7 @@ class Scenario { // Returns the current time. Timestamp Now(); // Return the duration of the current session so far. - TimeDelta Duration(); + TimeDelta TimeSinceStart(); std::unique_ptr GetLogWriter(std::string name) { if (!log_writer_factory_ || name.empty())