dbus_statistics_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2012 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 "dbus/dbus_statistics.h"
  5. #include "base/compiler_specific.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace dbus {
  8. class DBusStatisticsTest : public testing::Test {
  9. public:
  10. DBusStatisticsTest() = default;
  11. DBusStatisticsTest(const DBusStatisticsTest&) = delete;
  12. DBusStatisticsTest& operator=(const DBusStatisticsTest&) = delete;
  13. void SetUp() override { statistics::Initialize(); }
  14. void TearDown() override { statistics::Shutdown(); }
  15. protected:
  16. void AddTestMethodCalls() {
  17. statistics::AddSentMethodCall(
  18. "service1", "service1.interface1", "method1");
  19. statistics::AddReceivedSignal(
  20. "service1", "service1.interface1", "method1");
  21. statistics::AddBlockingSentMethodCall(
  22. "service1", "service1.interface1", "method1");
  23. statistics::AddSentMethodCall(
  24. "service1", "service1.interface1", "method2");
  25. statistics::AddSentMethodCall(
  26. "service1", "service1.interface1", "method2");
  27. statistics::AddReceivedSignal(
  28. "service1", "service1.interface1", "method2");
  29. statistics::AddSentMethodCall(
  30. "service1", "service1.interface1", "method3");
  31. statistics::AddSentMethodCall(
  32. "service1", "service1.interface1", "method3");
  33. statistics::AddSentMethodCall(
  34. "service1", "service1.interface1", "method3");
  35. statistics::AddSentMethodCall(
  36. "service1", "service1.interface2", "method1");
  37. statistics::AddSentMethodCall(
  38. "service1", "service1.interface2", "method2");
  39. statistics::AddSentMethodCall(
  40. "service2", "service2.interface1", "method1");
  41. }
  42. };
  43. TEST_F(DBusStatisticsTest, TestDBusStatsBasic) {
  44. int sent = 0, received = 0, block = 0;
  45. // Add a sent call
  46. statistics::AddSentMethodCall("service1", "service1.interface1", "method1");
  47. ASSERT_TRUE(statistics::testing::GetCalls(
  48. "service1", "service1.interface1", "method1", &sent, &received, &block));
  49. EXPECT_EQ(1, sent);
  50. EXPECT_EQ(0, received);
  51. EXPECT_EQ(0, block);
  52. // Add a received call
  53. statistics::AddReceivedSignal("service1", "service1.interface1", "method1");
  54. ASSERT_TRUE(statistics::testing::GetCalls(
  55. "service1", "service1.interface1", "method1", &sent, &received, &block));
  56. EXPECT_EQ(1, sent);
  57. EXPECT_EQ(1, received);
  58. EXPECT_EQ(0, block);
  59. // Add a block call
  60. statistics::AddBlockingSentMethodCall(
  61. "service1", "service1.interface1", "method1");
  62. ASSERT_TRUE(statistics::testing::GetCalls(
  63. "service1", "service1.interface1", "method1", &sent, &received, &block));
  64. EXPECT_EQ(1, sent);
  65. EXPECT_EQ(1, received);
  66. EXPECT_EQ(1, block);
  67. }
  68. TEST_F(DBusStatisticsTest, TestDBusStatsMulti) {
  69. int sent = 0, received = 0, block = 0;
  70. // Add some more stats to exercise accessing multiple different stats.
  71. AddTestMethodCalls();
  72. // Make sure all entries can be found in the set and their counts were
  73. // incremented correctly.
  74. ASSERT_TRUE(statistics::testing::GetCalls(
  75. "service1", "service1.interface1", "method1", &sent, &received, &block));
  76. EXPECT_EQ(1, sent);
  77. EXPECT_EQ(1, received);
  78. ASSERT_TRUE(statistics::testing::GetCalls(
  79. "service1", "service1.interface1", "method2", &sent, &received, &block));
  80. EXPECT_EQ(2, sent);
  81. EXPECT_EQ(1, received);
  82. ASSERT_TRUE(statistics::testing::GetCalls(
  83. "service1", "service1.interface1", "method3", &sent, &received, &block));
  84. EXPECT_EQ(3, sent);
  85. EXPECT_EQ(0, received);
  86. ASSERT_TRUE(statistics::testing::GetCalls(
  87. "service1", "service1.interface2", "method1", &sent, &received, &block));
  88. EXPECT_EQ(1, sent);
  89. EXPECT_EQ(0, received);
  90. ASSERT_TRUE(statistics::testing::GetCalls(
  91. "service1", "service1.interface2", "method2", &sent, &received, &block));
  92. EXPECT_EQ(1, sent);
  93. EXPECT_EQ(0, received);
  94. ASSERT_TRUE(statistics::testing::GetCalls(
  95. "service2", "service2.interface1", "method1", &sent, &received, &block));
  96. EXPECT_EQ(1, sent);
  97. EXPECT_EQ(0, received);
  98. ASSERT_FALSE(statistics::testing::GetCalls(
  99. "service1", "service1.interface3", "method2", &sent, &received, &block));
  100. }
  101. TEST_F(DBusStatisticsTest, TestGetAsString) {
  102. std::string output_none = GetAsString(statistics::SHOW_SERVICE,
  103. statistics::FORMAT_TOTALS);
  104. EXPECT_EQ("No DBus calls.", output_none);
  105. AddTestMethodCalls();
  106. std::string output_service = GetAsString(statistics::SHOW_SERVICE,
  107. statistics::FORMAT_TOTALS);
  108. const std::string expected_output_service(
  109. "service1: Sent (BLOCKING): 1 Sent: 8 Received: 2\n"
  110. "service2: Sent: 1\n");
  111. EXPECT_EQ(expected_output_service, output_service);
  112. std::string output_interface = GetAsString(statistics::SHOW_INTERFACE,
  113. statistics::FORMAT_TOTALS);
  114. const std::string expected_output_interface(
  115. "service1.interface1: Sent (BLOCKING): 1 Sent: 6 Received: 2\n"
  116. "service1.interface2: Sent: 2\n"
  117. "service2.interface1: Sent: 1\n");
  118. EXPECT_EQ(expected_output_interface, output_interface);
  119. std::string output_per_minute = GetAsString(statistics::SHOW_INTERFACE,
  120. statistics::FORMAT_PER_MINUTE);
  121. const std::string expected_output_per_minute(
  122. "service1.interface1: Sent (BLOCKING): 1/min Sent: 6/min"
  123. " Received: 2/min\n"
  124. "service1.interface2: Sent: 2/min\n"
  125. "service2.interface1: Sent: 1/min\n");
  126. EXPECT_EQ(expected_output_per_minute, output_per_minute);
  127. std::string output_all = GetAsString(statistics::SHOW_INTERFACE,
  128. statistics::FORMAT_ALL);
  129. const std::string expected_output_all(
  130. "service1.interface1: Sent (BLOCKING): 1 (1/min) Sent: 6 (6/min)"
  131. " Received: 2 (2/min)\n"
  132. "service1.interface2: Sent: 2 (2/min)\n"
  133. "service2.interface1: Sent: 1 (1/min)\n");
  134. EXPECT_EQ(expected_output_all, output_all);
  135. std::string output_method = GetAsString(statistics::SHOW_METHOD,
  136. statistics::FORMAT_TOTALS);
  137. const std::string expected_output_method(
  138. "service1.interface1.method1: Sent (BLOCKING): 1 Sent: 1 Received: 1\n"
  139. "service1.interface1.method2: Sent: 2 Received: 1\n"
  140. "service1.interface1.method3: Sent: 3\n"
  141. "service1.interface2.method1: Sent: 1\n"
  142. "service1.interface2.method2: Sent: 1\n"
  143. "service2.interface1.method1: Sent: 1\n");
  144. EXPECT_EQ(expected_output_method, output_method);
  145. }
  146. } // namespace dbus