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