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
3 // found in the LICENSE file.
5 #include "net/base/network_quality_estimator.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/test/histogram_tester.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "net/base/network_change_notifier.h"
17 #include "net/base/network_quality.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
25 // http://crbug.com/492410
26 TEST(NetworkQualityEstimatorTest
, DISABLED_TestPeakKbpsFastestRTTUpdates
) {
27 net::test_server::EmbeddedTestServer embedded_test_server
;
28 embedded_test_server
.ServeFilesFromDirectory(
29 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
30 ASSERT_TRUE(embedded_test_server
.InitializeAndWaitUntilReady());
32 // Enable requests to local host to be used for network quality estimation.
33 NetworkQualityEstimator
estimator(true, true);
35 NetworkQuality network_quality
= estimator
.GetPeakEstimate();
36 EXPECT_EQ(network_quality
.rtt(), base::TimeDelta::Max());
37 EXPECT_EQ(network_quality
.downstream_throughput_kbps(), 0);
40 TestDelegate test_delegate
;
41 TestURLRequestContext
context(false);
43 scoped_ptr
<URLRequest
> request(
44 context
.CreateRequest(embedded_test_server
.GetURL("/echo.html"),
45 DEFAULT_PRIORITY
, &test_delegate
));
48 base::RunLoop().Run();
50 // Both RTT and downstream throughput should be updated.
51 estimator
.NotifyDataReceived(*request
, 1000, 1000);
53 NetworkQuality network_quality
= estimator
.GetPeakEstimate();
54 EXPECT_GT(network_quality
.rtt(), base::TimeDelta());
55 EXPECT_LT(network_quality
.rtt(), base::TimeDelta::Max());
56 EXPECT_GE(network_quality
.downstream_throughput_kbps(), 1);
59 // Check UMA histograms.
60 base::HistogramTester histogram_tester
;
61 histogram_tester
.ExpectTotalCount("NQE.PeakKbps.Unknown", 0);
62 histogram_tester
.ExpectTotalCount("NQE.FastestRTT.Unknown", 0);
64 estimator
.OnConnectionTypeChanged(
65 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI
);
66 histogram_tester
.ExpectTotalCount("NQE.PeakKbps.Unknown", 1);
67 histogram_tester
.ExpectTotalCount("NQE.FastestRTT.Unknown", 1);
69 NetworkQuality network_quality
= estimator
.GetPeakEstimate();
70 EXPECT_EQ(estimator
.current_connection_type_
,
71 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI
);
72 EXPECT_EQ(network_quality
.rtt(), base::TimeDelta::Max());
73 EXPECT_EQ(network_quality
.downstream_throughput_kbps(), 0);
77 TEST(NetworkQualityEstimatorTest
, StoreObservations
) {
78 net::test_server::EmbeddedTestServer embedded_test_server
;
79 embedded_test_server
.ServeFilesFromDirectory(
80 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
81 ASSERT_TRUE(embedded_test_server
.InitializeAndWaitUntilReady());
83 NetworkQualityEstimator
estimator(true, true);
84 TestDelegate test_delegate
;
85 TestURLRequestContext
context(false);
87 // Push 10 more observations than the maximum buffer size.
89 i
< estimator
.GetMaximumObservationBufferSizeForTests() + 10U; ++i
) {
90 scoped_ptr
<URLRequest
> request(
91 context
.CreateRequest(embedded_test_server
.GetURL("/echo.html"),
92 DEFAULT_PRIORITY
, &test_delegate
));
94 base::RunLoop().Run();
96 estimator
.NotifyDataReceived(*request
, 1000, 1000);
99 EXPECT_TRUE(estimator
.VerifyBufferSizeForTests(
100 estimator
.GetMaximumObservationBufferSizeForTests()));
102 // Verify that the stored observations are cleared on network change.
103 estimator
.OnConnectionTypeChanged(
104 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI
);
105 EXPECT_TRUE(estimator
.VerifyBufferSizeForTests(0U));
107 scoped_ptr
<URLRequest
> request(
108 context
.CreateRequest(embedded_test_server
.GetURL("/echo.html"),
109 DEFAULT_PRIORITY
, &test_delegate
));
111 // Verify that overflow protection works.
113 base::RunLoop().Run();
114 estimator
.NotifyDataReceived(*request
, std::numeric_limits
<int64_t>::max(),
115 std::numeric_limits
<int64_t>::max());
117 NetworkQuality network_quality
= estimator
.GetPeakEstimate();
118 EXPECT_EQ(std::numeric_limits
<int32_t>::max() - 1,
119 network_quality
.downstream_throughput_kbps());