blob: 164d0b352168b6db4322201f1085aeef3a45cdfd [file] [log] [blame]
stevenjb@chromium.org3199bd12012-11-15 06:03:29 +09001// 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
stevenjb@chromium.org3199bd12012-11-15 06:03:29 +09007#include "base/compiler_specific.h"
avi0ad0ce02015-12-23 03:12:45 +09008#include "base/macros.h"
stevenjb@chromium.org3199bd12012-11-15 06:03:29 +09009#include "testing/gtest/include/gtest/gtest.h"
10
11namespace dbus {
12
13class DBusStatisticsTest : public testing::Test {
14 public:
Chris Watkins635e8902017-11-29 16:44:11 +090015 DBusStatisticsTest() = default;
stevenjb@chromium.org3199bd12012-11-15 06:03:29 +090016
dcheng7f5750d2014-12-30 03:30:17 +090017 void SetUp() override { statistics::Initialize(); }
stevenjb@chromium.org3199bd12012-11-15 06:03:29 +090018
dcheng7f5750d2014-12-30 03:30:17 +090019 void TearDown() override { statistics::Shutdown(); }
stevenjb@chromium.org3199bd12012-11-15 06:03:29 +090020
21 protected:
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
58TEST_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
87TEST_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
124TEST_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