1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "rtc_base/experiments/field_trial_units.h"
11 
12 #include <string>
13 
14 #include "absl/types/optional.h"
15 #include "rtc_base/experiments/field_trial_parser.h"
16 #include "test/gtest.h"
17 
18 namespace webrtc {
19 namespace {
20 struct DummyExperiment {
21   FieldTrialParameter<DataRate> target_rate =
22       FieldTrialParameter<DataRate>("t", DataRate::KilobitsPerSec(100));
23   FieldTrialParameter<TimeDelta> period =
24       FieldTrialParameter<TimeDelta>("p", TimeDelta::Millis(100));
25   FieldTrialOptional<DataSize> max_buffer =
26       FieldTrialOptional<DataSize>("b", absl::nullopt);
27 
DummyExperimentwebrtc::__anonb30e65e00111::DummyExperiment28   explicit DummyExperiment(std::string field_trial) {
29     ParseFieldTrial({&target_rate, &max_buffer, &period}, field_trial);
30   }
31 };
32 }  // namespace
33 
TEST(FieldTrialParserUnitsTest,FallsBackToDefaults)34 TEST(FieldTrialParserUnitsTest, FallsBackToDefaults) {
35   DummyExperiment exp("");
36   EXPECT_EQ(exp.target_rate.Get(), DataRate::KilobitsPerSec(100));
37   EXPECT_FALSE(exp.max_buffer.GetOptional().has_value());
38   EXPECT_EQ(exp.period.Get(), TimeDelta::Millis(100));
39 }
TEST(FieldTrialParserUnitsTest,ParsesUnitParameters)40 TEST(FieldTrialParserUnitsTest, ParsesUnitParameters) {
41   DummyExperiment exp("t:300kbps,b:5bytes,p:300ms");
42   EXPECT_EQ(exp.target_rate.Get(), DataRate::KilobitsPerSec(300));
43   EXPECT_EQ(*exp.max_buffer.GetOptional(), DataSize::Bytes(5));
44   EXPECT_EQ(exp.period.Get(), TimeDelta::Millis(300));
45 }
TEST(FieldTrialParserUnitsTest,ParsesDefaultUnitParameters)46 TEST(FieldTrialParserUnitsTest, ParsesDefaultUnitParameters) {
47   DummyExperiment exp("t:300,b:5,p:300");
48   EXPECT_EQ(exp.target_rate.Get(), DataRate::KilobitsPerSec(300));
49   EXPECT_EQ(*exp.max_buffer.GetOptional(), DataSize::Bytes(5));
50   EXPECT_EQ(exp.period.Get(), TimeDelta::Millis(300));
51 }
TEST(FieldTrialParserUnitsTest,ParsesInfinityParameter)52 TEST(FieldTrialParserUnitsTest, ParsesInfinityParameter) {
53   DummyExperiment exp("t:inf,p:inf");
54   EXPECT_EQ(exp.target_rate.Get(), DataRate::Infinity());
55   EXPECT_EQ(exp.period.Get(), TimeDelta::PlusInfinity());
56 }
TEST(FieldTrialParserUnitsTest,ParsesOtherUnitParameters)57 TEST(FieldTrialParserUnitsTest, ParsesOtherUnitParameters) {
58   DummyExperiment exp("t:300bps,p:0.3 seconds,b:8 bytes");
59   EXPECT_EQ(exp.target_rate.Get(), DataRate::BitsPerSec(300));
60   EXPECT_EQ(*exp.max_buffer.GetOptional(), DataSize::Bytes(8));
61   EXPECT_EQ(exp.period.Get(), TimeDelta::Millis(300));
62 }
TEST(FieldTrialParserUnitsTest,IgnoresOutOfRange)63 TEST(FieldTrialParserUnitsTest, IgnoresOutOfRange) {
64   FieldTrialConstrained<DataRate> rate("r", DataRate::KilobitsPerSec(30),
65                                        DataRate::KilobitsPerSec(10),
66                                        DataRate::KilobitsPerSec(100));
67   FieldTrialConstrained<TimeDelta> delta("d", TimeDelta::Millis(30),
68                                          TimeDelta::Millis(10),
69                                          TimeDelta::Millis(100));
70   FieldTrialConstrained<DataSize> size(
71       "s", DataSize::Bytes(30), DataSize::Bytes(10), DataSize::Bytes(100));
72   ParseFieldTrial({&rate, &delta, &size}, "r:0,d:0,s:0");
73   EXPECT_EQ(rate->kbps(), 30);
74   EXPECT_EQ(delta->ms(), 30);
75   EXPECT_EQ(size->bytes(), 30);
76   ParseFieldTrial({&rate, &delta, &size}, "r:300,d:300,s:300");
77   EXPECT_EQ(rate->kbps(), 30);
78   EXPECT_EQ(delta->ms(), 30);
79   EXPECT_EQ(size->bytes(), 30);
80   ParseFieldTrial({&rate, &delta, &size}, "r:50,d:50,s:50");
81   EXPECT_EQ(rate->kbps(), 50);
82   EXPECT_EQ(delta->ms(), 50);
83   EXPECT_EQ(size->bytes(), 50);
84 }
85 
86 }  // namespace webrtc
87