| Index: net/base/network_quality_estimator_unittest.cc
|
| diff --git a/net/base/network_quality_estimator_unittest.cc b/net/base/network_quality_estimator_unittest.cc
|
| index 25e386db44db573184f628053115c75be6ea87e5..e6fabcdbb42934802d915d9297f7a98d567705a5 100644
|
| --- a/net/base/network_quality_estimator_unittest.cc
|
| +++ b/net/base/network_quality_estimator_unittest.cc
|
| @@ -5,8 +5,10 @@
|
| #include "net/base/network_quality_estimator.h"
|
|
|
| #include <stdint.h>
|
| +
|
| #include <limits>
|
| #include <map>
|
| +#include <string>
|
| #include <utility>
|
| #include <vector>
|
|
|
| @@ -23,14 +25,20 @@
|
| #include "net/base/external_estimate_provider.h"
|
| #include "net/base/load_flags.h"
|
| #include "net/base/network_change_notifier.h"
|
| +#include "net/http/http_network_session.h"
|
| #include "net/http/http_status_code.h"
|
| #include "net/test/embedded_test_server/embedded_test_server.h"
|
| #include "net/test/embedded_test_server/http_request.h"
|
| #include "net/test/embedded_test_server/http_response.h"
|
| +#include "net/url_request/url_request.h"
|
| #include "net/url_request/url_request_test_util.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "url/gurl.h"
|
|
|
| +#if defined(TCP_INFO)
|
| +#include <netinet/tcp.h>
|
| +#endif
|
| +
|
| namespace {
|
|
|
| // Helps in setting the current network type and id.
|
| @@ -42,7 +50,8 @@ class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
|
| : NetworkQualityEstimator(std::move(external_estimate_provider),
|
| variation_params,
|
| true,
|
| - true) {
|
| + true),
|
| + watcher_reset_notification_received_count_(0) {
|
| // Set up embedded test server.
|
| embedded_test_server_.ServeFilesFromDirectory(
|
| base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
|
| @@ -84,6 +93,10 @@ class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
|
| return embedded_test_server_.GetURL("/echo.html");
|
| }
|
|
|
| + size_t watcher_reset_notification_received_count() const {
|
| + return watcher_reset_notification_received_count_;
|
| + }
|
| +
|
| using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
|
| using NetworkQualityEstimator::OnConnectionTypeChanged;
|
|
|
| @@ -101,6 +114,8 @@ class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
|
| // Embedded server used for testing.
|
| net::EmbeddedTestServer embedded_test_server_;
|
|
|
| + size_t watcher_reset_notification_received_count_;
|
| +
|
| DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
|
| };
|
|
|
| @@ -1061,12 +1076,12 @@ TEST(NetworkQualityEstimatorTest, TestObservers) {
|
|
|
| EXPECT_EQ(2U, rtt_observer.observations().size());
|
| EXPECT_EQ(2U, throughput_observer.observations().size());
|
| - for (auto observation : rtt_observer.observations()) {
|
| + for (const auto& observation : rtt_observer.observations()) {
|
| EXPECT_LE(0, observation.rtt_ms);
|
| EXPECT_LE(0, (observation.timestamp - then).InMilliseconds());
|
| EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source);
|
| }
|
| - for (auto observation : throughput_observer.observations()) {
|
| + for (const auto& observation : throughput_observer.observations()) {
|
| EXPECT_LE(0, observation.throughput_kbps);
|
| EXPECT_LE(0, (observation.timestamp - then).InMilliseconds());
|
| EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source);
|
| @@ -1077,14 +1092,18 @@ TEST(NetworkQualityEstimatorTest, TestObservers) {
|
| base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2));
|
|
|
| scoped_ptr<SocketPerformanceWatcher> tcp_watcher =
|
| - estimator.CreateSocketPerformanceWatcher(
|
| - SocketPerformanceWatcherFactory::PROTOCOL_TCP);
|
| + estimator.GetSocketPerformanceWatcherFactory()
|
| + ->CreateSocketPerformanceWatcher(
|
| + SocketPerformanceWatcherFactory::PROTOCOL_TCP);
|
| scoped_ptr<SocketPerformanceWatcher> quic_watcher =
|
| - estimator.CreateSocketPerformanceWatcher(
|
| - SocketPerformanceWatcherFactory::PROTOCOL_QUIC);
|
| + estimator.GetSocketPerformanceWatcherFactory()
|
| + ->CreateSocketPerformanceWatcher(
|
| + SocketPerformanceWatcherFactory::PROTOCOL_QUIC);
|
| tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt);
|
| quic_watcher->OnUpdatedRTTAvailable(quic_rtt);
|
|
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| EXPECT_EQ(4U, rtt_observer.observations().size());
|
| EXPECT_EQ(2U, throughput_observer.observations().size());
|
|
|
| @@ -1093,4 +1112,66 @@ TEST(NetworkQualityEstimatorTest, TestObservers) {
|
| rtt_observer.observations().at(3).rtt_ms);
|
| }
|
|
|
| +// TestTCPSocketRTT requires kernel support for tcp_info struct, and so it is
|
| +// enabled only on the POSIX platforms.
|
| +#if defined(TCP_INFO)
|
| +#define MAYBE_TestTCPSocketRTT TestTCPSocketRTT
|
| +#else
|
| +#define MAYBE_TestTCPSocketRTT DISABLED_TestTCPSocketRTT
|
| +#endif
|
| +// Tests that the TCP socket notifies the Network Quality Estimator of TCP RTTs,
|
| +// which in turn notifies registered RTT observers.
|
| +TEST(NetworkQualityEstimatorTest, MAYBE_TestTCPSocketRTT) {
|
| + TestRTTObserver rtt_observer;
|
| + std::map<std::string, std::string> variation_params;
|
| + TestNetworkQualityEstimator estimator(variation_params);
|
| + estimator.AddRTTObserver(&rtt_observer);
|
| +
|
| + TestDelegate test_delegate;
|
| + TestURLRequestContext context(true);
|
| + context.set_network_quality_estimator(&estimator);
|
| +
|
| + scoped_ptr<HttpNetworkSession::Params> params(new HttpNetworkSession::Params);
|
| + // |estimator| should be notified of TCP RTT observations.
|
| + params->socket_performance_watcher_factory =
|
| + estimator.GetSocketPerformanceWatcherFactory();
|
| + context.set_http_network_session_params(std::move(params));
|
| + context.Init();
|
| +
|
| + EXPECT_EQ(0U, rtt_observer.observations().size());
|
| + EXPECT_EQ(0U, estimator.watcher_reset_notification_received_count());
|
| +
|
| + const size_t kNumRequests = 2;
|
| +
|
| + // Send two requests. Verify that the completion of each request generates at
|
| + // least one TCP RTT observation.
|
| + for (size_t i = 0; i < kNumRequests; ++i) {
|
| + size_t before_count_tcp_rtt_observations = 0;
|
| + for (const auto& observation : rtt_observer.observations()) {
|
| + if (observation.source == NetworkQualityEstimator::TCP)
|
| + ++before_count_tcp_rtt_observations;
|
| + }
|
| +
|
| + scoped_ptr<URLRequest> request(context.CreateRequest(
|
| + estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
|
| + request->Start();
|
| + base::RunLoop().Run();
|
| +
|
| + size_t after_count_tcp_rtt_observations = 0;
|
| + for (const auto& observation : rtt_observer.observations()) {
|
| + if (observation.source == NetworkQualityEstimator::TCP)
|
| + ++after_count_tcp_rtt_observations;
|
| + }
|
| + // Exactly one notification should be received per socket performance
|
| + // watcher because the delay between notifications is currently set to a
|
| + // high value.
|
| + EXPECT_EQ(1U, after_count_tcp_rtt_observations -
|
| + before_count_tcp_rtt_observations)
|
| + << i;
|
| + }
|
| + // At least one reset should be received for each request.
|
| + EXPECT_LE(kNumRequests,
|
| + estimator.watcher_reset_notification_received_count());
|
| +}
|
| +
|
| } // namespace net
|
|
|