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