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:
pbos
2015-07-07 08:22:27 -07:00
committed by Commit bot
parent a7d70546ad
commit 19492f1c4c
3 changed files with 33 additions and 6 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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