pending_operations_unittest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2022 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 <string>
  5. #include "base/bind.h"
  6. #include "base/test/metrics/histogram_tester.h"
  7. #include "base/test/task_environment.h"
  8. #include "base/time/time.h"
  9. #include "media/capabilities/pending_operations.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace media {
  13. namespace {
  14. class PendingOperationsTest : public ::testing::Test {
  15. protected:
  16. base::test::TaskEnvironment task_environment_{
  17. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  18. base::HistogramTester histogram_;
  19. };
  20. // Test that histogram is created with correct data.
  21. TEST_F(PendingOperationsTest, OperationTiming) {
  22. const std::string kUmaPrefix = "Media.PendingOperations.";
  23. const std::string kOperation = "init";
  24. constexpr base::TimeDelta kInitDelay = base::Seconds(2);
  25. PendingOperations pending_operations(kUmaPrefix);
  26. PendingOperations::Id init_id = pending_operations.Start(kOperation);
  27. EXPECT_EQ(pending_operations.get_pending_ops_for_test().size(), 1u);
  28. task_environment_.FastForwardBy(kInitDelay);
  29. pending_operations.Complete(init_id);
  30. // No pending operations.
  31. EXPECT_TRUE(pending_operations.get_pending_ops_for_test().empty());
  32. // UMA histogram emitted.
  33. histogram_.ExpectUniqueSample(kUmaPrefix + kOperation,
  34. kInitDelay.InMicroseconds(), 1);
  35. }
  36. // Test that timeout histogram works.
  37. TEST_F(PendingOperationsTest, OperationTimeout) {
  38. const std::string kUmaPrefix = "Media.PendingOperations.";
  39. const std::string kOperation = "read";
  40. constexpr base::TimeDelta kLongTimeout = base::Hours(1);
  41. // Current setting of the pending operation timeout.
  42. constexpr base::TimeDelta kPendingOperationTimeout = base::Seconds(30);
  43. PendingOperations pending_operations(kUmaPrefix);
  44. pending_operations.Start(kOperation);
  45. EXPECT_EQ(pending_operations.get_pending_ops_for_test().size(), 1u);
  46. task_environment_.FastForwardBy(kLongTimeout);
  47. // No pending operations.
  48. EXPECT_TRUE(pending_operations.get_pending_ops_for_test().empty());
  49. // UMA histogram emitted, operation reported as timeout.
  50. histogram_.ExpectUniqueSample(kUmaPrefix + kOperation,
  51. kPendingOperationTimeout.InMicroseconds(), 1);
  52. }
  53. // Nested operations.
  54. struct SimulatedOperation {
  55. std::string name;
  56. base::TimeDelta start_time;
  57. base::TimeDelta stop_time;
  58. PendingOperations::Id id;
  59. };
  60. TEST_F(PendingOperationsTest, NestedOperation) {
  61. const std::string kUmaPrefix = "Media.PendingOperations.";
  62. PendingOperations pending_operations(kUmaPrefix);
  63. // A list of operations named after their start and stop time.
  64. SimulatedOperation operations[] = {
  65. {"0_10", base::Milliseconds(0), base::Milliseconds(10), 0},
  66. {"5_15", base::Milliseconds(5), base::Milliseconds(15), 0},
  67. {"6_30", base::Milliseconds(6), base::Milliseconds(30), 0},
  68. {"10_12", base::Milliseconds(10), base::Milliseconds(12), 0},
  69. {"20_27", base::Milliseconds(20), base::Milliseconds(27), 0},
  70. {"5_80", base::Milliseconds(5), base::Milliseconds(80), 0},
  71. {"30_60", base::Milliseconds(30), base::Milliseconds(60), 0},
  72. {"25_90", base::Milliseconds(25), base::Milliseconds(90), 0},
  73. };
  74. size_t expected_pending_operations = 0;
  75. // Run a loop from 0 to 100 ms.
  76. base::TimeDelta tick_length = base::Milliseconds(1);
  77. for (base::TimeDelta elapsed_time; elapsed_time < base::Milliseconds(100);
  78. elapsed_time += tick_length) {
  79. // Start/Complete each operation if the elapsed time has reached the
  80. // corresponding start/stop time.
  81. for (auto& operation : operations) {
  82. if (operation.start_time == elapsed_time) {
  83. operation.id = pending_operations.Start(operation.name);
  84. ++expected_pending_operations;
  85. }
  86. if (operation.stop_time == elapsed_time) {
  87. pending_operations.Complete(operation.id);
  88. --expected_pending_operations;
  89. }
  90. }
  91. EXPECT_EQ(pending_operations.get_pending_ops_for_test().size(),
  92. expected_pending_operations);
  93. task_environment_.FastForwardBy(tick_length);
  94. }
  95. for (const auto& operation : operations) {
  96. histogram_.ExpectUniqueSample(
  97. kUmaPrefix + operation.name,
  98. (operation.stop_time - operation.start_time).InMicroseconds(), 1);
  99. }
  100. }
  101. } // namespace
  102. } // namespace media