metrics_service_client_unittest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2019 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.
  4. #include "components/metrics/metrics_service.h"
  5. #include "base/bind.h"
  6. #include "base/command_line.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "components/metrics/metrics_switches.h"
  9. #include "components/metrics/test/test_metrics_service_client.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace metrics {
  12. namespace {
  13. class MetricsServiceClientTest : public testing::Test {
  14. public:
  15. MetricsServiceClientTest() {}
  16. MetricsServiceClientTest(const MetricsServiceClientTest&) = delete;
  17. MetricsServiceClientTest& operator=(const MetricsServiceClientTest&) = delete;
  18. ~MetricsServiceClientTest() override {}
  19. };
  20. } // namespace
  21. TEST_F(MetricsServiceClientTest, TestUploadIntervalDefaultsToStandard) {
  22. TestMetricsServiceClient client;
  23. ASSERT_EQ(client.GetStandardUploadInterval(), client.GetUploadInterval());
  24. }
  25. TEST_F(MetricsServiceClientTest, TestModifyMetricsUploadInterval) {
  26. TestMetricsServiceClient client;
  27. // Flip it a few times to make sure we really can modify it. Values are
  28. // arbitrary (but positive, because the upload interval should be).
  29. int specified_upload_sec = 800;
  30. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  31. switches::kMetricsUploadIntervalSec,
  32. base::NumberToString(specified_upload_sec));
  33. ASSERT_EQ(base::Seconds(specified_upload_sec), client.GetUploadInterval());
  34. base::CommandLine::ForCurrentProcess()->RemoveSwitch(
  35. switches::kMetricsUploadIntervalSec);
  36. specified_upload_sec = 30;
  37. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  38. switches::kMetricsUploadIntervalSec,
  39. base::NumberToString(specified_upload_sec));
  40. ASSERT_EQ(base::Seconds(specified_upload_sec), client.GetUploadInterval());
  41. }
  42. TEST_F(MetricsServiceClientTest, TestUploadIntervalLimitedForDos) {
  43. TestMetricsServiceClient client;
  44. // If we set the upload interval too small, it should be limited to prevent
  45. // the possibility of DOS'ing the backend. This should be a safe guess for a
  46. // value strictly smaller than the DOS limit.
  47. int too_short_upload_sec = 2;
  48. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  49. switches::kMetricsUploadIntervalSec,
  50. base::NumberToString(too_short_upload_sec));
  51. // Upload interval should be the DOS rate limit.
  52. ASSERT_EQ(base::Seconds(20), client.GetUploadInterval());
  53. }
  54. } // namespace metrics