1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "services/network/resource_scheduler/resource_scheduler_params_manager.h"
6 
7 #include <map>
8 #include <string>
9 
10 #include "base/strings/string_number_conversions.h"
11 #include "base/test/scoped_feature_list.h"
12 #include "net/nqe/network_quality_estimator_params.h"
13 #include "services/network/public/cpp/features.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace network {
17 
18 namespace {
19 
20 // Should remain synchronized with the values in
21 // resouce_scheduler_params_manager.cc.
22 constexpr base::TimeDelta kLowerBoundQueuingDuration =
23     base::TimeDelta::FromSeconds(15);
24 constexpr base::TimeDelta kUpperBoundQueuingDuration =
25     base::TimeDelta::FromSeconds(120);
26 constexpr int kHttpRttMultiplierForQueuingDuration = 30;
27 
28 class ResourceSchedulerParamsManagerTest : public testing::Test {
29  public:
ResourceSchedulerParamsManagerTest()30   ResourceSchedulerParamsManagerTest() {}
31 
~ResourceSchedulerParamsManagerTest()32   ~ResourceSchedulerParamsManagerTest() override {}
33 
ReadConfigTestHelper(size_t num_ranges)34   void ReadConfigTestHelper(size_t num_ranges) {
35     base::test::ScopedFeatureList scoped_feature_list;
36     base::FieldTrialParams params;
37     int index = 1;
38     net::EffectiveConnectionType effective_connection_type =
39         net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
40     while (effective_connection_type <
41            net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G + num_ranges) {
42       std::string index_str = base::NumberToString(index);
43       params["EffectiveConnectionType" + index_str] =
44           net::GetNameForEffectiveConnectionType(effective_connection_type);
45       params["MaxDelayableRequests" + index_str] = index_str + "0";
46       params["NonDelayableWeight" + index_str] = "0";
47       effective_connection_type = static_cast<net::EffectiveConnectionType>(
48           static_cast<int>(effective_connection_type) + 1);
49       ++index;
50     }
51 
52     scoped_feature_list.InitAndEnableFeatureWithParameters(
53         features::kThrottleDelayable, params);
54 
55     ResourceSchedulerParamsManager resource_scheduler_params_manager;
56 
57     effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
58     while (effective_connection_type < net::EFFECTIVE_CONNECTION_TYPE_LAST) {
59       if (effective_connection_type <
60           net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G + num_ranges) {
61         int index = static_cast<int>(effective_connection_type) - 1;
62         EXPECT_EQ(index * 10u, resource_scheduler_params_manager
63                                    .GetParamsForEffectiveConnectionType(
64                                        effective_connection_type)
65                                    .max_delayable_requests);
66         EXPECT_EQ(0, resource_scheduler_params_manager
67                          .GetParamsForEffectiveConnectionType(
68                              effective_connection_type)
69                          .non_delayable_weight);
70       } else {
71         VerifyDefaultParams(resource_scheduler_params_manager,
72                             effective_connection_type);
73       }
74       effective_connection_type = static_cast<net::EffectiveConnectionType>(
75           static_cast<int>(effective_connection_type) + 1);
76     }
77   }
78 
VerifyDefaultParams(const ResourceSchedulerParamsManager & resource_scheduler_params_manager,net::EffectiveConnectionType effective_connection_type) const79   void VerifyDefaultParams(
80       const ResourceSchedulerParamsManager& resource_scheduler_params_manager,
81       net::EffectiveConnectionType effective_connection_type) const {
82     switch (effective_connection_type) {
83       case net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN:
84       case net::EFFECTIVE_CONNECTION_TYPE_OFFLINE:
85       case net::EFFECTIVE_CONNECTION_TYPE_4G:
86         EXPECT_EQ(10u, resource_scheduler_params_manager
87                            .GetParamsForEffectiveConnectionType(
88                                effective_connection_type)
89                            .max_delayable_requests);
90         EXPECT_EQ(0.0, resource_scheduler_params_manager
91                            .GetParamsForEffectiveConnectionType(
92                                effective_connection_type)
93                            .non_delayable_weight);
94         EXPECT_FALSE(
95             resource_scheduler_params_manager
96                 .GetParamsForEffectiveConnectionType(effective_connection_type)
97                 .delay_requests_on_multiplexed_connections);
98         EXPECT_TRUE(
99             resource_scheduler_params_manager
100                 .GetParamsForEffectiveConnectionType(effective_connection_type)
101                 .max_queuing_time.has_value());
102         EXPECT_FALSE(
103             resource_scheduler_params_manager
104                 .GetParamsForEffectiveConnectionType(effective_connection_type)
105                 .http_rtt_multiplier_for_proactive_throttling);
106         return;
107 
108       case net::EFFECTIVE_CONNECTION_TYPE_3G:
109         EXPECT_EQ(8u, resource_scheduler_params_manager
110                           .GetParamsForEffectiveConnectionType(
111                               effective_connection_type)
112                           .max_delayable_requests);
113         EXPECT_EQ(3.0, resource_scheduler_params_manager
114                            .GetParamsForEffectiveConnectionType(
115                                effective_connection_type)
116                            .non_delayable_weight);
117         EXPECT_TRUE(
118             resource_scheduler_params_manager
119                 .GetParamsForEffectiveConnectionType(effective_connection_type)
120                 .delay_requests_on_multiplexed_connections);
121         EXPECT_TRUE(
122             resource_scheduler_params_manager
123                 .GetParamsForEffectiveConnectionType(effective_connection_type)
124                 .max_queuing_time.has_value());
125         EXPECT_FALSE(
126             resource_scheduler_params_manager
127                 .GetParamsForEffectiveConnectionType(effective_connection_type)
128                 .http_rtt_multiplier_for_proactive_throttling);
129         return;
130 
131       case net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G:
132       case net::EFFECTIVE_CONNECTION_TYPE_2G:
133         EXPECT_EQ(8u, resource_scheduler_params_manager
134                           .GetParamsForEffectiveConnectionType(
135                               effective_connection_type)
136                           .max_delayable_requests);
137         EXPECT_EQ(3.0, resource_scheduler_params_manager
138                            .GetParamsForEffectiveConnectionType(
139                                effective_connection_type)
140                            .non_delayable_weight);
141         EXPECT_TRUE(
142             resource_scheduler_params_manager
143                 .GetParamsForEffectiveConnectionType(effective_connection_type)
144                 .delay_requests_on_multiplexed_connections);
145         EXPECT_TRUE(
146             resource_scheduler_params_manager
147                 .GetParamsForEffectiveConnectionType(effective_connection_type)
148                 .max_queuing_time.has_value());
149         EXPECT_FALSE(
150             resource_scheduler_params_manager
151                 .GetParamsForEffectiveConnectionType(effective_connection_type)
152                 .http_rtt_multiplier_for_proactive_throttling);
153         return;
154 
155       case net::EFFECTIVE_CONNECTION_TYPE_LAST:
156         NOTREACHED();
157         return;
158     }
159   }
160 
161  private:
162   DISALLOW_COPY_AND_ASSIGN(ResourceSchedulerParamsManagerTest);
163 };
164 
TEST_F(ResourceSchedulerParamsManagerTest,VerifyAllDefaultParams)165 TEST_F(ResourceSchedulerParamsManagerTest, VerifyAllDefaultParams) {
166   ResourceSchedulerParamsManager resource_scheduler_params_manager;
167 
168   for (int effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
169        effective_connection_type < net::EFFECTIVE_CONNECTION_TYPE_LAST;
170        ++effective_connection_type) {
171     VerifyDefaultParams(
172         resource_scheduler_params_manager,
173         static_cast<net::EffectiveConnectionType>(effective_connection_type));
174   }
175 }
176 
177 // Verify that the params are parsed correctly when
178 // kDelayRequestsOnMultiplexedConnections is enabled.
TEST_F(ResourceSchedulerParamsManagerTest,DelayRequestsOnMultiplexedConnections)179 TEST_F(ResourceSchedulerParamsManagerTest,
180        DelayRequestsOnMultiplexedConnections) {
181   ResourceSchedulerParamsManager resource_scheduler_params_manager;
182 
183   for (int effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
184        effective_connection_type < net::EFFECTIVE_CONNECTION_TYPE_LAST;
185        ++effective_connection_type) {
186     net::EffectiveConnectionType ect =
187         static_cast<net::EffectiveConnectionType>(effective_connection_type);
188     if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G ||
189         effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_2G ||
190         effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_3G) {
191       EXPECT_EQ(8u, resource_scheduler_params_manager
192                         .GetParamsForEffectiveConnectionType(ect)
193                         .max_delayable_requests);
194       EXPECT_EQ(3.0, resource_scheduler_params_manager
195                          .GetParamsForEffectiveConnectionType(ect)
196                          .non_delayable_weight);
197       EXPECT_TRUE(resource_scheduler_params_manager
198                       .GetParamsForEffectiveConnectionType(ect)
199                       .delay_requests_on_multiplexed_connections);
200       EXPECT_TRUE(resource_scheduler_params_manager
201                       .GetParamsForEffectiveConnectionType(ect)
202                       .max_queuing_time.has_value());
203 
204     } else {
205       VerifyDefaultParams(
206           resource_scheduler_params_manager,
207           static_cast<net::EffectiveConnectionType>(effective_connection_type));
208     }
209   }
210 }
211 
212 // Verify that the params are parsed correctly when
213 // kDelayRequestsOnMultiplexedConnections is disabled.
TEST_F(ResourceSchedulerParamsManagerTest,DisableDelayRequestsOnMultiplexedConnections)214 TEST_F(ResourceSchedulerParamsManagerTest,
215        DisableDelayRequestsOnMultiplexedConnections) {
216   base::test::ScopedFeatureList scoped_feature_list;
217   scoped_feature_list.InitAndDisableFeature(
218       features::kDelayRequestsOnMultiplexedConnections);
219 
220   ResourceSchedulerParamsManager resource_scheduler_params_manager;
221 
222   for (int effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
223        effective_connection_type < net::EFFECTIVE_CONNECTION_TYPE_LAST;
224        ++effective_connection_type) {
225     net::EffectiveConnectionType ect =
226         static_cast<net::EffectiveConnectionType>(effective_connection_type);
227     if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G ||
228         effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_2G ||
229         effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_3G) {
230       EXPECT_EQ(8u, resource_scheduler_params_manager
231                         .GetParamsForEffectiveConnectionType(ect)
232                         .max_delayable_requests);
233       EXPECT_EQ(3.0, resource_scheduler_params_manager
234                          .GetParamsForEffectiveConnectionType(ect)
235                          .non_delayable_weight);
236       EXPECT_FALSE(resource_scheduler_params_manager
237                        .GetParamsForEffectiveConnectionType(ect)
238                        .delay_requests_on_multiplexed_connections);
239       EXPECT_TRUE(resource_scheduler_params_manager
240                       .GetParamsForEffectiveConnectionType(ect)
241                       .max_queuing_time.has_value());
242 
243     } else {
244       VerifyDefaultParams(
245           resource_scheduler_params_manager,
246           static_cast<net::EffectiveConnectionType>(effective_connection_type));
247     }
248   }
249 }
250 
TEST_F(ResourceSchedulerParamsManagerTest,MaxQueuingTime)251 TEST_F(ResourceSchedulerParamsManagerTest, MaxQueuingTime) {
252   ResourceSchedulerParamsManager resource_scheduler_params_manager;
253 
254   for (int effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
255        effective_connection_type < net::EFFECTIVE_CONNECTION_TYPE_LAST;
256        ++effective_connection_type) {
257     net::EffectiveConnectionType ect =
258         static_cast<net::EffectiveConnectionType>(effective_connection_type);
259     base::TimeDelta typical_http_rtt =
260         net::NetworkQualityEstimatorParams::GetDefaultTypicalHttpRtt(ect);
261 
262     if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G ||
263         effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_2G) {
264       EXPECT_EQ(8u, resource_scheduler_params_manager
265                         .GetParamsForEffectiveConnectionType(ect)
266                         .max_delayable_requests);
267       EXPECT_EQ(3.0, resource_scheduler_params_manager
268                          .GetParamsForEffectiveConnectionType(ect)
269                          .non_delayable_weight);
270       EXPECT_TRUE(resource_scheduler_params_manager
271                       .GetParamsForEffectiveConnectionType(ect)
272                       .delay_requests_on_multiplexed_connections);
273       EXPECT_EQ(typical_http_rtt * kHttpRttMultiplierForQueuingDuration,
274                 resource_scheduler_params_manager
275                     .GetParamsForEffectiveConnectionType(ect)
276                     .max_queuing_time);
277 
278     } else if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_3G) {
279       EXPECT_EQ(8u, resource_scheduler_params_manager
280                         .GetParamsForEffectiveConnectionType(ect)
281                         .max_delayable_requests);
282       EXPECT_EQ(3.0, resource_scheduler_params_manager
283                          .GetParamsForEffectiveConnectionType(ect)
284                          .non_delayable_weight);
285       EXPECT_TRUE(resource_scheduler_params_manager
286                       .GetParamsForEffectiveConnectionType(ect)
287                       .delay_requests_on_multiplexed_connections);
288       EXPECT_EQ(kLowerBoundQueuingDuration,
289                 resource_scheduler_params_manager
290                     .GetParamsForEffectiveConnectionType(ect)
291                     .max_queuing_time);
292 
293     } else if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_4G) {
294       EXPECT_EQ(10u, resource_scheduler_params_manager
295                          .GetParamsForEffectiveConnectionType(ect)
296                          .max_delayable_requests);
297       EXPECT_EQ(0.0, resource_scheduler_params_manager
298                          .GetParamsForEffectiveConnectionType(ect)
299                          .non_delayable_weight);
300       EXPECT_FALSE(resource_scheduler_params_manager
301                        .GetParamsForEffectiveConnectionType(ect)
302                        .delay_requests_on_multiplexed_connections);
303       EXPECT_EQ(kLowerBoundQueuingDuration,
304                 resource_scheduler_params_manager
305                     .GetParamsForEffectiveConnectionType(ect)
306                     .max_queuing_time);
307     } else {
308       EXPECT_EQ(10u, resource_scheduler_params_manager
309                          .GetParamsForEffectiveConnectionType(ect)
310                          .max_delayable_requests);
311       EXPECT_EQ(0.0, resource_scheduler_params_manager
312                          .GetParamsForEffectiveConnectionType(ect)
313                          .non_delayable_weight);
314       EXPECT_FALSE(resource_scheduler_params_manager
315                        .GetParamsForEffectiveConnectionType(ect)
316                        .delay_requests_on_multiplexed_connections);
317       EXPECT_EQ(kUpperBoundQueuingDuration,
318                 resource_scheduler_params_manager
319                     .GetParamsForEffectiveConnectionType(ect)
320                     .max_queuing_time);
321     }
322   }
323 }
324 
325 // Verify that the params are parsed correctly when
326 // kDelayRequestsOnMultiplexedConnections and kThrottleDelayable are enabled.
TEST_F(ResourceSchedulerParamsManagerTest,MultipleFieldTrialsEnabled)327 TEST_F(ResourceSchedulerParamsManagerTest, MultipleFieldTrialsEnabled) {
328   base::test::ScopedFeatureList scoped_feature_list;
329 
330   // Configure kDelayRequestsOnMultiplexedConnections experiment params.
331   base::FieldTrialParams params_multiplex;
332   params_multiplex["MaxEffectiveConnectionType"] = "3G";
333 
334   // Configure kThrottleDelayable experiment params.
335   base::FieldTrialParams params_throttle_delayable;
336   params_throttle_delayable["EffectiveConnectionType1"] = "3G";
337   params_throttle_delayable["MaxDelayableRequests1"] = "12";
338   params_throttle_delayable["NonDelayableWeight1"] = "3.0";
339   params_throttle_delayable["EffectiveConnectionType2"] = "4G";
340   params_throttle_delayable["MaxDelayableRequests2"] = "14";
341   params_throttle_delayable["NonDelayableWeight2"] = "4.0";
342   scoped_feature_list.InitWithFeaturesAndParameters(
343       {{features::kDelayRequestsOnMultiplexedConnections, params_multiplex},
344        {features::kThrottleDelayable, params_throttle_delayable}},
345       {});
346 
347   ResourceSchedulerParamsManager resource_scheduler_params_manager;
348 
349   // Verify the parsed params.
350   for (int effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
351        effective_connection_type < net::EFFECTIVE_CONNECTION_TYPE_LAST;
352        ++effective_connection_type) {
353     net::EffectiveConnectionType ect =
354         static_cast<net::EffectiveConnectionType>(effective_connection_type);
355     if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G ||
356         effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_2G) {
357       EXPECT_EQ(8u, resource_scheduler_params_manager
358                         .GetParamsForEffectiveConnectionType(ect)
359                         .max_delayable_requests);
360       EXPECT_EQ(3.0, resource_scheduler_params_manager
361                          .GetParamsForEffectiveConnectionType(ect)
362                          .non_delayable_weight);
363       EXPECT_TRUE(resource_scheduler_params_manager
364                       .GetParamsForEffectiveConnectionType(ect)
365                       .delay_requests_on_multiplexed_connections);
366       EXPECT_TRUE(resource_scheduler_params_manager
367                       .GetParamsForEffectiveConnectionType(ect)
368                       .max_queuing_time.has_value());
369 
370     } else if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_3G) {
371       EXPECT_EQ(12u, resource_scheduler_params_manager
372                          .GetParamsForEffectiveConnectionType(ect)
373                          .max_delayable_requests);
374       EXPECT_EQ(3.0, resource_scheduler_params_manager
375                          .GetParamsForEffectiveConnectionType(ect)
376                          .non_delayable_weight);
377       EXPECT_TRUE(resource_scheduler_params_manager
378                       .GetParamsForEffectiveConnectionType(ect)
379                       .delay_requests_on_multiplexed_connections);
380       EXPECT_TRUE(resource_scheduler_params_manager
381                       .GetParamsForEffectiveConnectionType(ect)
382                       .max_queuing_time.has_value());
383 
384     } else if (effective_connection_type == net::EFFECTIVE_CONNECTION_TYPE_4G) {
385       EXPECT_EQ(14u, resource_scheduler_params_manager
386                          .GetParamsForEffectiveConnectionType(ect)
387                          .max_delayable_requests);
388       EXPECT_EQ(4.0, resource_scheduler_params_manager
389                          .GetParamsForEffectiveConnectionType(ect)
390                          .non_delayable_weight);
391       EXPECT_FALSE(resource_scheduler_params_manager
392                        .GetParamsForEffectiveConnectionType(ect)
393                        .delay_requests_on_multiplexed_connections);
394       EXPECT_TRUE(resource_scheduler_params_manager
395                       .GetParamsForEffectiveConnectionType(ect)
396                       .max_queuing_time.has_value());
397 
398     } else {
399       VerifyDefaultParams(resource_scheduler_params_manager, ect);
400     }
401   }
402 }
403 
404 // Test that a configuration with bad strings does not break the parser, and
405 // the parser stops reading the configuration after it encounters the first
406 // missing index.
TEST_F(ResourceSchedulerParamsManagerTest,ReadInvalidConfigTest)407 TEST_F(ResourceSchedulerParamsManagerTest, ReadInvalidConfigTest) {
408   base::test::ScopedFeatureList scoped_feature_list;
409   base::FieldTrialParams params;
410   // Skip configuration parameters for index 2 to test that the parser stops
411   // when it cannot find the parameters for an index.
412   for (int range_index : {1, 3, 4}) {
413     std::string index_str = base::NumberToString(range_index);
414     params["EffectiveConnectionType" + index_str] = "Slow-2G";
415     params["MaxDelayableRequests" + index_str] = index_str + "0";
416     params["NonDelayableWeight" + index_str] = "0";
417   }
418   // Add some bad configuration strigs to ensure that the parser does not break.
419   params["BadConfigParam1"] = "100";
420   params["BadConfigParam2"] = "100";
421 
422   scoped_feature_list.InitAndEnableFeatureWithParameters(
423       features::kThrottleDelayable, params);
424 
425   ResourceSchedulerParamsManager resource_scheduler_params_manager;
426 
427   // Only the first configuration parameter must be read because a match was not
428   // found for index 2. The configuration parameters with index 3 and 4 must be
429   // ignored, even though they are valid configuration parameters.
430   EXPECT_EQ(10u, resource_scheduler_params_manager
431                      .GetParamsForEffectiveConnectionType(
432                          net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)
433                      .max_delayable_requests);
434   EXPECT_EQ(0.0, resource_scheduler_params_manager
435                      .GetParamsForEffectiveConnectionType(
436                          net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)
437                      .non_delayable_weight);
438 
439   VerifyDefaultParams(resource_scheduler_params_manager,
440                       net::EFFECTIVE_CONNECTION_TYPE_2G);
441   VerifyDefaultParams(resource_scheduler_params_manager,
442                       net::EFFECTIVE_CONNECTION_TYPE_3G);
443   VerifyDefaultParams(resource_scheduler_params_manager,
444                       net::EFFECTIVE_CONNECTION_TYPE_4G);
445 }
446 
447 // Test that a configuration with 2 ranges is read correctly.
TEST_F(ResourceSchedulerParamsManagerTest,ReadValidConfigTest2)448 TEST_F(ResourceSchedulerParamsManagerTest, ReadValidConfigTest2) {
449   ReadConfigTestHelper(2);
450 }
451 
452 // Test that a configuration with 3 ranges is read correctly.
TEST_F(ResourceSchedulerParamsManagerTest,ReadValidConfigTest3)453 TEST_F(ResourceSchedulerParamsManagerTest, ReadValidConfigTest3) {
454   ReadConfigTestHelper(3);
455 }
456 
TEST_F(ResourceSchedulerParamsManagerTest,ThrottleDelayableDisabled)457 TEST_F(ResourceSchedulerParamsManagerTest, ThrottleDelayableDisabled) {
458   base::test::ScopedFeatureList scoped_feature_list;
459   scoped_feature_list.InitAndDisableFeature(features::kThrottleDelayable);
460 
461   ResourceSchedulerParamsManager resource_scheduler_params_manager;
462 
463   VerifyDefaultParams(resource_scheduler_params_manager,
464                       net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
465   VerifyDefaultParams(resource_scheduler_params_manager,
466                       net::EFFECTIVE_CONNECTION_TYPE_2G);
467   VerifyDefaultParams(resource_scheduler_params_manager,
468                       net::EFFECTIVE_CONNECTION_TYPE_3G);
469   VerifyDefaultParams(resource_scheduler_params_manager,
470                       net::EFFECTIVE_CONNECTION_TYPE_4G);
471 }
472 
TEST_F(ResourceSchedulerParamsManagerTest,MaxDelayableRequestsAndNonDelayableWeightSet)473 TEST_F(ResourceSchedulerParamsManagerTest,
474        MaxDelayableRequestsAndNonDelayableWeightSet) {
475   base::test::ScopedFeatureList scoped_feature_list;
476 
477   base::FieldTrialParams params;
478 
479   params["EffectiveConnectionType1"] = "Slow-2G";
480   size_t max_delayable_requests_slow_2g = 2u;
481   double non_delayable_weight_slow_2g = 2.0;
482   params["MaxDelayableRequests1"] =
483       base::NumberToString(max_delayable_requests_slow_2g);
484   params["NonDelayableWeight1"] =
485       base::NumberToString(non_delayable_weight_slow_2g);
486 
487   params["EffectiveConnectionType2"] = "3G";
488   size_t max_delayable_requests_3g = 4u;
489   double non_delayable_weight_3g = 0.0;
490   params["MaxDelayableRequests2"] =
491       base::NumberToString(max_delayable_requests_3g);
492   params["NonDelayableWeight2"] = base::NumberToString(non_delayable_weight_3g);
493 
494   scoped_feature_list.InitAndEnableFeatureWithParameters(
495       features::kThrottleDelayable, params);
496 
497   ResourceSchedulerParamsManager resource_scheduler_params_manager;
498 
499   EXPECT_EQ(max_delayable_requests_slow_2g,
500             resource_scheduler_params_manager
501                 .GetParamsForEffectiveConnectionType(
502                     net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)
503                 .max_delayable_requests);
504   EXPECT_EQ(non_delayable_weight_slow_2g,
505             resource_scheduler_params_manager
506                 .GetParamsForEffectiveConnectionType(
507                     net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)
508                 .non_delayable_weight);
509   EXPECT_TRUE(resource_scheduler_params_manager
510                   .GetParamsForEffectiveConnectionType(
511                       net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)
512                   .max_queuing_time.has_value());
513 
514   VerifyDefaultParams(resource_scheduler_params_manager,
515                       net::EFFECTIVE_CONNECTION_TYPE_2G);
516 
517   EXPECT_EQ(max_delayable_requests_3g,
518             resource_scheduler_params_manager
519                 .GetParamsForEffectiveConnectionType(
520                     net::EFFECTIVE_CONNECTION_TYPE_3G)
521                 .max_delayable_requests);
522   EXPECT_EQ(non_delayable_weight_3g, resource_scheduler_params_manager
523                                          .GetParamsForEffectiveConnectionType(
524                                              net::EFFECTIVE_CONNECTION_TYPE_3G)
525                                          .non_delayable_weight);
526   EXPECT_TRUE(resource_scheduler_params_manager
527                   .GetParamsForEffectiveConnectionType(
528                       net::EFFECTIVE_CONNECTION_TYPE_3G)
529                   .max_queuing_time.has_value());
530 
531   VerifyDefaultParams(resource_scheduler_params_manager,
532                       net::EFFECTIVE_CONNECTION_TYPE_4G);
533 }
534 
TEST_F(ResourceSchedulerParamsManagerTest,ProactivelyThrottleLowPriorityRequests)535 TEST_F(ResourceSchedulerParamsManagerTest,
536        ProactivelyThrottleLowPriorityRequests) {
537   const double kDelaySlow2G = 1.5;
538   const double kDelay2G = 1.6;
539   const double kDelay4G = 0.5;
540 
541   base::test::ScopedFeatureList scoped_feature_list;
542 
543   base::FieldTrialParams params;
544   params["http_rtt_multiplier_for_proactive_throttling_Slow-2G"] =
545       base::NumberToString(kDelaySlow2G);
546   params["http_rtt_multiplier_for_proactive_throttling_2G"] =
547       base::NumberToString(kDelay2G);
548   params["http_rtt_multiplier_for_proactive_throttling_4G"] =
549       base::NumberToString(kDelay4G);
550 
551   scoped_feature_list.InitAndEnableFeatureWithParameters(
552       features::kProactivelyThrottleLowPriorityRequests, params);
553 
554   ResourceSchedulerParamsManager resource_scheduler_params_manager;
555 
556   EXPECT_EQ(kDelaySlow2G, resource_scheduler_params_manager
557                               .GetParamsForEffectiveConnectionType(
558                                   net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)
559                               .http_rtt_multiplier_for_proactive_throttling);
560   EXPECT_EQ(kDelay2G, resource_scheduler_params_manager
561                           .GetParamsForEffectiveConnectionType(
562                               net::EFFECTIVE_CONNECTION_TYPE_2G)
563                           .http_rtt_multiplier_for_proactive_throttling);
564   EXPECT_FALSE(resource_scheduler_params_manager
565                    .GetParamsForEffectiveConnectionType(
566                        net::EFFECTIVE_CONNECTION_TYPE_3G)
567                    .http_rtt_multiplier_for_proactive_throttling.has_value());
568   EXPECT_EQ(kDelay4G,
569             resource_scheduler_params_manager
570                 .GetParamsForEffectiveConnectionType(
571                     net::EFFECTIVE_CONNECTION_TYPE_4G)
572                 .http_rtt_multiplier_for_proactive_throttling.value());
573 }
574 
575 }  // unnamed namespace
576 
577 }  // namespace network
578