1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/metrics_service.h"
7 #include "base/command_line.h"
8 #include "base/functional/bind.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/metrics/metrics_switches.h"
11 #include "components/metrics/test/test_metrics_service_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 class MetricsServiceClientTest : public testing::Test {
19 MetricsServiceClientTest() {}
21 MetricsServiceClientTest(const MetricsServiceClientTest&) = delete;
22 MetricsServiceClientTest& operator=(const MetricsServiceClientTest&) = delete;
24 ~MetricsServiceClientTest() override {}
29 TEST_F(MetricsServiceClientTest, TestUploadIntervalDefaultsToStandard) {
30 TestMetricsServiceClient client;
32 ASSERT_EQ(client.GetStandardUploadInterval(), client.GetUploadInterval());
35 TEST_F(MetricsServiceClientTest, TestModifyMetricsUploadInterval) {
36 TestMetricsServiceClient client;
38 // Flip it a few times to make sure we really can modify it. Values are
39 // arbitrary (but positive, because the upload interval should be).
40 int specified_upload_sec = 800;
41 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
42 switches::kMetricsUploadIntervalSec,
43 base::NumberToString(specified_upload_sec));
44 ASSERT_EQ(base::Seconds(specified_upload_sec), client.GetUploadInterval());
46 base::CommandLine::ForCurrentProcess()->RemoveSwitch(
47 switches::kMetricsUploadIntervalSec);
49 specified_upload_sec = 30;
50 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
51 switches::kMetricsUploadIntervalSec,
52 base::NumberToString(specified_upload_sec));
53 ASSERT_EQ(base::Seconds(specified_upload_sec), client.GetUploadInterval());
56 TEST_F(MetricsServiceClientTest, TestUploadIntervalLimitedForDos) {
57 TestMetricsServiceClient client;
59 // If we set the upload interval too small, it should be limited to prevent
60 // the possibility of DOS'ing the backend. This should be a safe guess for a
61 // value strictly smaller than the DOS limit.
62 int too_short_upload_sec = 2;
63 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
64 switches::kMetricsUploadIntervalSec,
65 base::NumberToString(too_short_upload_sec));
66 // Upload interval should be the DOS rate limit.
67 ASSERT_EQ(base::Seconds(20), client.GetUploadInterval());
70 } // namespace metrics