Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client_unittest.cc |
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client_unittest.cc |
index 1b93ac82d20aecd5af3805a9ab7c7cb8f308e6b0..605b589d51a152f17520348e847a2a518ef83986 100644 |
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client_unittest.cc |
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client_unittest.cc |
@@ -1,24 +1,26 @@ |
// Copyright 2016 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client.h" |
#include <memory> |
#include <string> |
+#include "base/command_line.h" |
#include "base/memory/ptr_util.h" |
#include "base/message_loop/message_loop.h" |
#include "base/time/time.h" |
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data.h" |
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_page_load_timing.h" |
+#include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" |
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_util.h" |
#include "components/data_reduction_proxy/proto/client_config.pb.h" |
#include "components/data_reduction_proxy/proto/pageload_metrics.pb.h" |
#include "net/url_request/test_url_fetcher_factory.h" |
#include "net/url_request/url_request_context_getter.h" |
#include "net/url_request/url_request_test_util.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "url/gurl.h" |
namespace data_reduction_proxy { |
@@ -30,30 +32,37 @@ static const char kFakeURL[] = "http://www.google.com/"; |
} // namespace |
// Controls whether a pingback is sent or not. |
class TestDataReductionProxyPingbackClient |
: public DataReductionProxyPingbackClient { |
public: |
TestDataReductionProxyPingbackClient( |
net::URLRequestContextGetter* url_request_context_getter) |
: DataReductionProxyPingbackClient(url_request_context_getter), |
- should_send_pingback_(false) {} |
+ should_override_random_(false), |
+ override_value_(0.0f) {} |
- void set_should_send_pingback(bool should_send_pingback) { |
- should_send_pingback_ = should_send_pingback; |
+ void OverrideRandom(bool should_override_random, float override_value) { |
+ should_override_random_ = should_override_random; |
+ override_value_ = override_value; |
} |
private: |
- bool ShouldSendPingback() const override { return should_send_pingback_; } |
+ float GenerateRandomFloat() const override { |
+ if (should_override_random_) |
+ return override_value_; |
+ return DataReductionProxyPingbackClient::GenerateRandomFloat(); |
+ } |
- bool should_send_pingback_; |
+ bool should_override_random_; |
+ float override_value_; |
}; |
class DataReductionProxyPingbackClientTest : public testing::Test { |
public: |
DataReductionProxyPingbackClientTest() |
: timing_(base::Time::FromJsTime(1500), |
base::TimeDelta::FromMilliseconds(1600), |
base::TimeDelta::FromMilliseconds(1700), |
base::TimeDelta::FromMilliseconds(1800), |
base::TimeDelta::FromMilliseconds(1900)) {} |
@@ -86,21 +95,22 @@ class DataReductionProxyPingbackClientTest : public testing::Test { |
base::MessageLoopForIO message_loop_; |
scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
std::unique_ptr<TestDataReductionProxyPingbackClient> pingback_client_; |
net::TestURLFetcherFactory factory_; |
DataReductionProxyPageLoadTiming timing_; |
}; |
TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { |
Init(); |
EXPECT_FALSE(factory()->GetFetcherByID(0)); |
- pingback_client()->set_should_send_pingback(true); |
+ pingback_client()->OverrideRandom(true, 0.5f); |
+ pingback_client()->SetPingbackReportingFraction(1.0f); |
CreateAndSendPingback(); |
net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
RecordPageloadMetricsRequest batched_request; |
batched_request.ParseFromString(test_fetcher->upload_data()); |
PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
EXPECT_EQ( |
timing().navigation_start, |
protobuf_parser::TimestampToTime(pageload_metrics.first_request_time())); |
@@ -118,31 +128,73 @@ TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { |
EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); |
EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); |
test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
EXPECT_FALSE(factory()->GetFetcherByID(0)); |
} |
TEST_F(DataReductionProxyPingbackClientTest, SendTwoPingbacks) { |
Init(); |
EXPECT_FALSE(factory()->GetFetcherByID(0)); |
- pingback_client()->set_should_send_pingback(true); |
+ pingback_client()->OverrideRandom(true, 0.5f); |
+ pingback_client()->SetPingbackReportingFraction(1.0f); |
CreateAndSendPingback(); |
CreateAndSendPingback(); |
net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
EXPECT_TRUE(factory()->GetFetcherByID(0)); |
test_fetcher = factory()->GetFetcherByID(0); |
test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
EXPECT_FALSE(factory()->GetFetcherByID(0)); |
} |
TEST_F(DataReductionProxyPingbackClientTest, NoPingbackSent) { |
Init(); |
EXPECT_FALSE(factory()->GetFetcherByID(0)); |
- pingback_client()->set_should_send_pingback(false); |
+ pingback_client()->OverrideRandom(true, 0.5f); |
+ pingback_client()->SetPingbackReportingFraction(0.0f); |
CreateAndSendPingback(); |
EXPECT_FALSE(factory()->GetFetcherByID(0)); |
} |
+TEST_F(DataReductionProxyPingbackClientTest, VerifyReportingBehvaior) { |
+ Init(); |
+ EXPECT_FALSE(factory()->GetFetcherByID(0)); |
+ |
+ // Verify that if the random number is less than the reporting fraction, the |
+ // pingback is created. |
+ pingback_client()->SetPingbackReportingFraction(0.5f); |
+ pingback_client()->OverrideRandom(true, 0.4f); |
+ CreateAndSendPingback(); |
+ net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
+ EXPECT_TRUE(test_fetcher); |
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
+ |
+ // Verify that if the random number is greater than the reporting fraction, |
+ // the pingback is not created. |
+ pingback_client()->OverrideRandom(true, 0.6f); |
+ CreateAndSendPingback(); |
+ test_fetcher = factory()->GetFetcherByID(0); |
+ EXPECT_FALSE(test_fetcher); |
+ |
+ // Verify that if the random number is equal to the reporting fraction, the |
+ // pingback is not created. Specifically, if the reporting fraction is zero, |
+ // and the random number is zero, no pingback is sent. |
+ pingback_client()->SetPingbackReportingFraction(0.0f); |
+ pingback_client()->OverrideRandom(true, 0.0f); |
+ CreateAndSendPingback(); |
+ test_fetcher = factory()->GetFetcherByID(0); |
+ EXPECT_FALSE(test_fetcher); |
+ |
+ // Verify that the command line flag forces a pingback. |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ data_reduction_proxy::switches::kEnableDataReductionProxyForcePingback); |
+ pingback_client()->SetPingbackReportingFraction(0.0f); |
+ pingback_client()->OverrideRandom(true, 1.0f); |
+ CreateAndSendPingback(); |
+ test_fetcher = factory()->GetFetcherByID(0); |
+ EXPECT_TRUE(test_fetcher); |
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
+} |
+ |
} // namespace data_reduction_proxy |