1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/*
* Copyright 2018 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 "rtc_base/experiments/field_trial_parser.h"
#include "absl/strings/string_view.h"
#include "rtc_base/experiments/field_trial_list.h"
#include "rtc_base/gunit.h"
#include "system_wrappers/include/field_trial.h"
#include "test/field_trial.h"
#include "test/gmock.h"
namespace webrtc {
namespace {
constexpr char kDummyExperiment[] = "WebRTC-DummyExperiment";
struct DummyExperiment {
FieldTrialFlag enabled = FieldTrialFlag("Enabled");
FieldTrialParameter<double> factor = FieldTrialParameter<double>("f", 0.5);
FieldTrialParameter<int> retries = FieldTrialParameter<int>("r", 5);
FieldTrialParameter<unsigned> size = FieldTrialParameter<unsigned>("s", 3);
FieldTrialParameter<bool> ping = FieldTrialParameter<bool>("p", 0);
FieldTrialParameter<std::string> hash =
FieldTrialParameter<std::string>("h", "a80");
DummyExperiment()
: DummyExperiment([] {
field_trial::FieldTrialsAllowedInScopeForTesting k{
{kDummyExperiment}};
return field_trial::FindFullName(kDummyExperiment);
}()) {}
explicit DummyExperiment(absl::string_view field_trial) {
ParseFieldTrial({&enabled, &factor, &retries, &size, &ping, &hash},
field_trial);
}
};
enum class CustomEnum {
kDefault = 0,
kRed = 1,
kBlue = 2,
};
} // namespace
TEST(FieldTrialParserTest, ParsesValidParameters) {
DummyExperiment exp("Enabled,f:-1.7,r:2,s:10,p:1,h:x7c");
EXPECT_TRUE(exp.enabled.Get());
EXPECT_EQ(exp.factor.Get(), -1.7);
EXPECT_EQ(exp.retries.Get(), 2);
EXPECT_EQ(exp.size.Get(), 10u);
EXPECT_EQ(exp.ping.Get(), true);
EXPECT_EQ(exp.hash.Get(), "x7c");
}
TEST(FieldTrialParserTest, InitializesFromFieldTrial) {
test::ScopedFieldTrials field_trials(
"WebRTC-OtherExperiment/Disabled/"
"WebRTC-DummyExperiment/Enabled,f:-1.7,r:2,s:10,p:1,h:x7c/"
"WebRTC-AnotherExperiment/Enabled,f:-3.1,otherstuff:beef/");
DummyExperiment exp;
EXPECT_TRUE(exp.enabled.Get());
EXPECT_EQ(exp.factor.Get(), -1.7);
EXPECT_EQ(exp.retries.Get(), 2);
EXPECT_EQ(exp.size.Get(), 10u);
EXPECT_EQ(exp.ping.Get(), true);
EXPECT_EQ(exp.hash.Get(), "x7c");
}
TEST(FieldTrialParserTest, UsesDefaults) {
DummyExperiment exp("");
EXPECT_FALSE(exp.enabled.Get());
EXPECT_EQ(exp.factor.Get(), 0.5);
EXPECT_EQ(exp.retries.Get(), 5);
EXPECT_EQ(exp.size.Get(), 3u);
EXPECT_EQ(exp.ping.Get(), false);
EXPECT_EQ(exp.hash.Get(), "a80");
}
TEST(FieldTrialParserTest, CanHandleMixedInput) {
DummyExperiment exp("p:true,h:,Enabled");
EXPECT_TRUE(exp.enabled.Get());
EXPECT_EQ(exp.factor.Get(), 0.5);
EXPECT_EQ(exp.retries.Get(), 5);
EXPECT_EQ(exp.size.Get(), 3u);
EXPECT_EQ(exp.ping.Get(), true);
EXPECT_EQ(exp.hash.Get(), "");
}
TEST(FieldTrialParserTest, ParsesDoubleParameter) {
FieldTrialParameter<double> double_param("f", 0.0);
ParseFieldTrial({&double_param}, "f:45%");
EXPECT_EQ(double_param.Get(), 0.45);
ParseFieldTrial({&double_param}, "f:34 %");
EXPECT_EQ(double_param.Get(), 0.34);
ParseFieldTrial({&double_param}, "f:0.67");
EXPECT_EQ(double_param.Get(), 0.67);
}
TEST(FieldTrialParserTest, IgnoresNewKey) {
DummyExperiment exp("Disabled,r:-11,foo");
EXPECT_FALSE(exp.enabled.Get());
EXPECT_EQ(exp.factor.Get(), 0.5);
EXPECT_EQ(exp.retries.Get(), -11);
}
TEST(FieldTrialParserTest, IgnoresInvalid) {
DummyExperiment exp("Enabled,f,p:,r:%,,s:-1,:foo,h");
EXPECT_TRUE(exp.enabled.Get());
EXPECT_EQ(exp.factor.Get(), 0.5);
EXPECT_EQ(exp.retries.Get(), 5);
EXPECT_EQ(exp.size.Get(), 3u);
EXPECT_EQ(exp.ping.Get(), false);
EXPECT_EQ(exp.hash.Get(), "a80");
}
TEST(FieldTrialParserTest, IgnoresOutOfRange) {
FieldTrialConstrained<double> low("low", 10, absl::nullopt, 100);
FieldTrialConstrained<double> high("high", 10, 5, absl::nullopt);
ParseFieldTrial({&low, &high}, "low:1000,high:0");
EXPECT_EQ(low.Get(), 10);
EXPECT_EQ(high.Get(), 10);
ParseFieldTrial({&low, &high}, "low:inf,high:nan");
EXPECT_EQ(low.Get(), 10);
EXPECT_EQ(high.Get(), 10);
ParseFieldTrial({&low, &high}, "low:20,high:20");
EXPECT_EQ(low.Get(), 20);
EXPECT_EQ(high.Get(), 20);
FieldTrialConstrained<unsigned> size("size", 5, 1, 10);
ParseFieldTrial({&size}, "size:0");
EXPECT_EQ(size.Get(), 5u);
}
TEST(FieldTrialParserTest, ReadsValuesFromFieldWithoutKey) {
FieldTrialFlag enabled("Enabled");
FieldTrialParameter<int> req("", 10);
ParseFieldTrial({&enabled, &req}, "Enabled,20");
EXPECT_EQ(req.Get(), 20);
ParseFieldTrial({&req}, "30");
EXPECT_EQ(req.Get(), 30);
}
TEST(FieldTrialParserTest, ParsesOptionalParameters) {
FieldTrialOptional<int> max_count("c", absl::nullopt);
ParseFieldTrial({&max_count}, "");
EXPECT_FALSE(max_count.GetOptional().has_value());
ParseFieldTrial({&max_count}, "c:10");
EXPECT_EQ(max_count.GetOptional().value(), 10);
ParseFieldTrial({&max_count}, "c");
EXPECT_FALSE(max_count.GetOptional().has_value());
ParseFieldTrial({&max_count}, "c:20");
EXPECT_EQ(max_count.GetOptional().value(), 20);
ParseFieldTrial({&max_count}, "c:");
EXPECT_EQ(max_count.GetOptional().value(), 20);
FieldTrialOptional<unsigned> max_size("c", absl::nullopt);
ParseFieldTrial({&max_size}, "");
EXPECT_FALSE(max_size.GetOptional().has_value());
ParseFieldTrial({&max_size}, "c:10");
EXPECT_EQ(max_size.GetOptional().value(), 10u);
ParseFieldTrial({&max_size}, "c");
EXPECT_FALSE(max_size.GetOptional().has_value());
ParseFieldTrial({&max_size}, "c:20");
EXPECT_EQ(max_size.GetOptional().value(), 20u);
FieldTrialOptional<std::string> optional_string("s", std::string("ab"));
ParseFieldTrial({&optional_string}, "s:");
EXPECT_EQ(optional_string.GetOptional().value(), "");
ParseFieldTrial({&optional_string}, "s");
EXPECT_FALSE(optional_string.GetOptional().has_value());
}
TEST(FieldTrialParserTest, ParsesCustomEnumParameter) {
FieldTrialEnum<CustomEnum> my_enum("e", CustomEnum::kDefault,
{{"default", CustomEnum::kDefault},
{"red", CustomEnum::kRed},
{"blue", CustomEnum::kBlue}});
ParseFieldTrial({&my_enum}, "");
EXPECT_EQ(my_enum.Get(), CustomEnum::kDefault);
ParseFieldTrial({&my_enum}, "e:red");
EXPECT_EQ(my_enum.Get(), CustomEnum::kRed);
ParseFieldTrial({&my_enum}, "e:2");
EXPECT_EQ(my_enum.Get(), CustomEnum::kBlue);
ParseFieldTrial({&my_enum}, "e:5");
EXPECT_EQ(my_enum.Get(), CustomEnum::kBlue);
}
} // namespace webrtc
|