Add scoped class for overriding field trials.
To be used in tests that depend on specific field-trial settings without overwriting the command-line flag for overriding field trials. BUG=webrtc:4820 R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/1227653002 Cr-Commit-Position: refs/heads/master@{#9547}
This commit is contained in:
@ -603,14 +603,17 @@ TEST_F(OveruseDetectorTest, HighGaussianVarianceFastDrift2000Kbit30fps) {
|
||||
}
|
||||
|
||||
class OveruseDetectorExperimentTest : public OveruseDetectorTest {
|
||||
public:
|
||||
OveruseDetectorExperimentTest()
|
||||
: override_field_trials_(
|
||||
"WebRTC-AdaptiveBweThreshold/Enabled-0.01,0.00018/") {}
|
||||
|
||||
protected:
|
||||
void SetUp() override {
|
||||
test::InitFieldTrialsFromString(
|
||||
"WebRTC-AdaptiveBweThreshold/Enabled-0.01,0.00018/");
|
||||
overuse_detector_.reset(new OveruseDetector(options_));
|
||||
}
|
||||
|
||||
void TearDown() override { test::InitFieldTrialsFromString(""); }
|
||||
test::ScopedFieldTrials override_field_trials_;
|
||||
};
|
||||
|
||||
TEST_F(OveruseDetectorExperimentTest, ThresholdAdapts) {
|
||||
|
@ -45,12 +45,12 @@ namespace test {
|
||||
void InitFieldTrialsFromString(const std::string& trials_string) {
|
||||
static const char kPersistentStringSeparator = '/';
|
||||
|
||||
// Catch an error if this is called more than once.
|
||||
assert(field_trials_initiated_ == false);
|
||||
field_trials_initiated_ = true;
|
||||
|
||||
if (trials_string.empty()) {
|
||||
field_trials_.clear();
|
||||
if (trials_string == "")
|
||||
return;
|
||||
}
|
||||
|
||||
size_t next_item = 0;
|
||||
while (next_item < trials_string.length()) {
|
||||
@ -83,5 +83,18 @@ void InitFieldTrialsFromString(const std::string& trials_string) {
|
||||
// Using abort so it crashs both in debug and release mode.
|
||||
abort();
|
||||
}
|
||||
|
||||
ScopedFieldTrials::ScopedFieldTrials(const std::string& config)
|
||||
: previous_field_trials_(field_trials_) {
|
||||
assert(field_trials_initiated_);
|
||||
field_trials_initiated_ = false;
|
||||
field_trials_ = std::map<std::string, std::string>();
|
||||
InitFieldTrialsFromString(config);
|
||||
}
|
||||
|
||||
ScopedFieldTrials::~ScopedFieldTrials() {
|
||||
field_trials_ = previous_field_trials_;
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
|
@ -12,6 +12,7 @@
|
||||
#define WEBRTC_TEST_FIELD_TRIAL_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
@ -31,6 +32,16 @@ namespace test {
|
||||
// passed to it. That can be used to find out if a binary is parsing the flags.
|
||||
void InitFieldTrialsFromString(const std::string& config);
|
||||
|
||||
// This class is used to override field-trial configs within specific tests.
|
||||
// After this class goes out of scope previous field trials will be restored.
|
||||
class ScopedFieldTrials {
|
||||
public:
|
||||
explicit ScopedFieldTrials(const std::string& config);
|
||||
~ScopedFieldTrials();
|
||||
private:
|
||||
const std::map<std::string, std::string> previous_field_trials_;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
|
||||
|
Reference in New Issue
Block a user