Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: net/nqe/network_quality_estimator_unittest.cc

Issue 2491703003: NQE: Notify observer as soon as it is added (Closed)
Patch Set: Moar tests Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/nqe/network_quality_estimator.h" 5 #include "net/nqe/network_quality_estimator.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 estimator.AddThroughputObserver(&throughput_observer); 344 estimator.AddThroughputObserver(&throughput_observer);
345 345
346 estimator.SimulateNetworkChange( 346 estimator.SimulateNetworkChange(
347 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); 347 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test");
348 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, 348 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true,
349 1); 349 1);
350 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 2); 350 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 2);
351 base::RunLoop().RunUntilIdle(); 351 base::RunLoop().RunUntilIdle();
352 352
353 // Verify that the cached network quality was read, and observers were 353 // Verify that the cached network quality was read, and observers were
354 // notified. 354 // notified. |observer| must be notified once right after it was added, and
355 EXPECT_EQ(1U, observer.effective_connection_types().size()); 355 // once again after the cached network quality was read.
356 EXPECT_EQ(2U, observer.effective_connection_types().size());
356 EXPECT_EQ(1U, rtt_observer.observations().size()); 357 EXPECT_EQ(1U, rtt_observer.observations().size());
357 EXPECT_EQ(1U, throughput_observer.observations().size()); 358 EXPECT_EQ(1U, throughput_observer.observations().size());
358 } 359 }
359 360
360 TEST(NetworkQualityEstimatorTest, StoreObservations) { 361 TEST(NetworkQualityEstimatorTest, StoreObservations) {
361 std::map<std::string, std::string> variation_params; 362 std::map<std::string, std::string> variation_params;
362 TestNetworkQualityEstimator estimator(variation_params); 363 TestNetworkQualityEstimator estimator(variation_params);
363 364
364 base::TimeDelta rtt; 365 base::TimeDelta rtt;
365 int32_t kbps; 366 int32_t kbps;
(...skipping 1061 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, 1428 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI,
1428 "test"); 1429 "test");
1429 EXPECT_EQ(2U, observer.effective_connection_types().size()); 1430 EXPECT_EQ(2U, observer.effective_connection_types().size());
1430 1431
1431 // A change in effective connection type does not trigger notification to the 1432 // A change in effective connection type does not trigger notification to the
1432 // observers, since it is not accompanied by any new observation or a network 1433 // observers, since it is not accompanied by any new observation or a network
1433 // change event. 1434 // change event.
1434 estimator.set_start_time_null_http_rtt( 1435 estimator.set_start_time_null_http_rtt(
1435 base::TimeDelta::FromMilliseconds(100)); 1436 base::TimeDelta::FromMilliseconds(100));
1436 EXPECT_EQ(2U, observer.effective_connection_types().size()); 1437 EXPECT_EQ(2U, observer.effective_connection_types().size());
1438
1439 TestEffectiveConnectionTypeObserver observer_2;
1440 estimator.AddEffectiveConnectionTypeObserver(&observer_2);
1441 EXPECT_EQ(0U, observer_2.effective_connection_types().size());
1442 base::RunLoop().RunUntilIdle();
1443 // |observer_2| must be notified as soon as it is added.
1444 EXPECT_EQ(1U, observer_2.effective_connection_types().size());
1445
1446 // |observer_3| should not be notified since it unregisters before the
1447 // message loop is run.
1448 TestEffectiveConnectionTypeObserver observer_3;
1449 estimator.AddEffectiveConnectionTypeObserver(&observer_3);
1450 EXPECT_EQ(0U, observer_3.effective_connection_types().size());
1451 estimator.RemoveEffectiveConnectionTypeObserver(&observer_3);
1452 base::RunLoop().RunUntilIdle();
1453 EXPECT_EQ(0U, observer_3.effective_connection_types().size());
1437 } 1454 }
1438 1455
1439 // Tests that the network quality is computed at the specified interval, and 1456 // Tests that the network quality is computed at the specified interval, and
1440 // that the network quality observers are notified of any change. 1457 // that the network quality observers are notified of any change.
1441 TEST(NetworkQualityEstimatorTest, TestRTTAndThroughputEstimatesObserver) { 1458 TEST(NetworkQualityEstimatorTest, TestRTTAndThroughputEstimatesObserver) {
1442 base::HistogramTester histogram_tester; 1459 base::HistogramTester histogram_tester;
1443 std::unique_ptr<base::SimpleTestTickClock> tick_clock( 1460 std::unique_ptr<base::SimpleTestTickClock> tick_clock(
1444 new base::SimpleTestTickClock()); 1461 new base::SimpleTestTickClock());
1445 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); 1462 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get();
1446 1463
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1500 EXPECT_LE(1, observer.notifications_received() - notifications_received); 1517 EXPECT_LE(1, observer.notifications_received() - notifications_received);
1501 notifications_received = observer.notifications_received(); 1518 notifications_received = observer.notifications_received();
1502 1519
1503 // A change in effective connection type does not trigger notification to the 1520 // A change in effective connection type does not trigger notification to the
1504 // observers, since it is not accompanied by any new observation or a network 1521 // observers, since it is not accompanied by any new observation or a network
1505 // change event. 1522 // change event.
1506 estimator.set_start_time_null_http_rtt( 1523 estimator.set_start_time_null_http_rtt(
1507 base::TimeDelta::FromMilliseconds(10000)); 1524 base::TimeDelta::FromMilliseconds(10000));
1508 estimator.set_start_time_null_http_rtt(base::TimeDelta::FromMilliseconds(1)); 1525 estimator.set_start_time_null_http_rtt(base::TimeDelta::FromMilliseconds(1));
1509 EXPECT_EQ(0, observer.notifications_received() - notifications_received); 1526 EXPECT_EQ(0, observer.notifications_received() - notifications_received);
1527
1528 TestRTTAndThroughputEstimatesObserver observer_2;
1529 estimator.AddRTTAndThroughputEstimatesObserver(&observer_2);
1530 EXPECT_EQ(nqe::internal::InvalidRTT(), observer_2.http_rtt());
1531 EXPECT_EQ(nqe::internal::InvalidRTT(), observer_2.transport_rtt());
1532 EXPECT_EQ(nqe::internal::kInvalidThroughput,
1533 observer_2.downstream_throughput_kbps());
1534 base::RunLoop().RunUntilIdle();
1535 EXPECT_NE(nqe::internal::InvalidRTT(), observer_2.http_rtt());
1536 EXPECT_NE(nqe::internal::InvalidRTT(), observer_2.transport_rtt());
1537 EXPECT_NE(nqe::internal::kInvalidThroughput,
1538 observer_2.downstream_throughput_kbps());
1539
1540 // |observer_3| should not be notified because it is unregisters before the
1541 // message loop is run.
1542 TestRTTAndThroughputEstimatesObserver observer_3;
1543 estimator.AddRTTAndThroughputEstimatesObserver(&observer_3);
1544 EXPECT_EQ(nqe::internal::InvalidRTT(), observer_3.http_rtt());
1545 EXPECT_EQ(nqe::internal::InvalidRTT(), observer_3.transport_rtt());
1546 EXPECT_EQ(nqe::internal::kInvalidThroughput,
1547 observer_3.downstream_throughput_kbps());
1548 estimator.RemoveRTTAndThroughputEstimatesObserver(&observer_3);
1549 base::RunLoop().RunUntilIdle();
1550 EXPECT_EQ(nqe::internal::InvalidRTT(), observer_3.http_rtt());
1551 EXPECT_EQ(nqe::internal::InvalidRTT(), observer_3.transport_rtt());
1552 EXPECT_EQ(nqe::internal::kInvalidThroughput,
1553 observer_3.downstream_throughput_kbps());
1510 } 1554 }
1511 1555
1512 // Tests that the effective connection type is computed on every RTT 1556 // Tests that the effective connection type is computed on every RTT
1513 // observation if the last computed effective connection type was unknown. 1557 // observation if the last computed effective connection type was unknown.
1514 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) { 1558 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) {
1515 std::unique_ptr<base::SimpleTestTickClock> tick_clock( 1559 std::unique_ptr<base::SimpleTestTickClock> tick_clock(
1516 new base::SimpleTestTickClock()); 1560 new base::SimpleTestTickClock());
1517 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); 1561 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get();
1518 1562
1519 TestEffectiveConnectionTypeObserver observer; 1563 TestEffectiveConnectionTypeObserver observer;
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after
2320 if (expected_count == 1) { 2364 if (expected_count == 1) {
2321 EffectiveConnectionType last_notified_type = 2365 EffectiveConnectionType last_notified_type =
2322 observer.effective_connection_types().at( 2366 observer.effective_connection_types().at(
2323 observer.effective_connection_types().size() - 1); 2367 observer.effective_connection_types().size() - 1);
2324 EXPECT_EQ(i, last_notified_type); 2368 EXPECT_EQ(i, last_notified_type);
2325 } 2369 }
2326 } 2370 }
2327 } 2371 }
2328 2372
2329 } // namespace net 2373 } // namespace net
OLDNEW
« net/nqe/network_quality_estimator.cc ('K') | « net/nqe/network_quality_estimator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698