Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 1 | // Copyright (C) 2018 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | #include <stdio.h> |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 18 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 19 | #include <chrono> |
| 20 | #include <thread> |
| 21 | #include <vector> |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 22 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 23 | #include "../metrics/metrics_test_helper.h" |
| 24 | #include "src/stats_log_util.h" |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 25 | #include "stats_event.h" |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 26 | #include "tests/statsd_test_util.h" |
| 27 | |
| 28 | #ifdef __ANDROID__ |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
| 34 | using namespace testing; |
| 35 | using std::make_shared; |
| 36 | using std::shared_ptr; |
| 37 | using std::vector; |
| 38 | using std::this_thread::sleep_for; |
| 39 | using testing::Contains; |
| 40 | |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 41 | namespace { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 42 | // cooldown time 1sec. |
| 43 | int pullTagId = 10014; |
| 44 | |
| 45 | bool pullSuccess; |
| 46 | vector<std::shared_ptr<LogEvent>> pullData; |
| 47 | long pullDelayNs; |
| 48 | |
| 49 | class FakePuller : public StatsPuller { |
| 50 | public: |
Tej Singh | 5b4951b | 2020-01-24 13:23:56 -0800 | [diff] [blame] | 51 | FakePuller() : StatsPuller(pullTagId, /*coolDown=*/NS_PER_SEC, /*timeout=*/NS_PER_SEC / 2){}; |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | bool PullInternal(vector<std::shared_ptr<LogEvent>>* data) override { |
| 55 | (*data) = pullData; |
| 56 | sleep_for(std::chrono::nanoseconds(pullDelayNs)); |
| 57 | return pullSuccess; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | FakePuller puller; |
| 62 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 63 | std::unique_ptr<LogEvent> createSimpleEvent(int64_t eventTimeNs, int64_t value) { |
| 64 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 65 | AStatsEvent_setAtomId(statsEvent, pullTagId); |
| 66 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 67 | AStatsEvent_writeInt64(statsEvent, value); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 68 | |
| 69 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 70 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 71 | return logEvent; |
| 72 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 73 | |
| 74 | class StatsPullerTest : public ::testing::Test { |
| 75 | public: |
| 76 | StatsPullerTest() { |
| 77 | } |
| 78 | |
| 79 | void SetUp() override { |
| 80 | puller.ForceClearCache(); |
| 81 | pullSuccess = false; |
| 82 | pullDelayNs = 0; |
| 83 | pullData.clear(); |
| 84 | } |
| 85 | }; |
| 86 | |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 87 | } // Anonymous namespace. |
| 88 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 89 | TEST_F(StatsPullerTest, PullSuccess) { |
| 90 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 91 | |
| 92 | pullSuccess = true; |
| 93 | |
| 94 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 95 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 96 | ASSERT_EQ(1, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 97 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 98 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 99 | ASSERT_EQ(1, dataHolder[0]->size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 100 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 101 | |
| 102 | sleep_for(std::chrono::seconds(1)); |
| 103 | |
| 104 | pullData.clear(); |
| 105 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 106 | |
| 107 | pullSuccess = true; |
| 108 | |
| 109 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 110 | ASSERT_EQ(1, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 111 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 112 | EXPECT_EQ(2222L, dataHolder[0]->GetElapsedTimestampNs()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 113 | ASSERT_EQ(1, dataHolder[0]->size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 114 | EXPECT_EQ(44, dataHolder[0]->getValues()[0].mValue.int_value); |
| 115 | } |
| 116 | |
| 117 | TEST_F(StatsPullerTest, PullFailAfterSuccess) { |
| 118 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 119 | |
| 120 | pullSuccess = true; |
| 121 | |
| 122 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 123 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 124 | ASSERT_EQ(1, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 125 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 126 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 127 | ASSERT_EQ(1, dataHolder[0]->size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 128 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 129 | |
| 130 | sleep_for(std::chrono::seconds(1)); |
| 131 | |
| 132 | pullData.clear(); |
| 133 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 134 | |
| 135 | pullSuccess = false; |
| 136 | dataHolder.clear(); |
| 137 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 138 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 139 | |
| 140 | pullSuccess = true; |
| 141 | dataHolder.clear(); |
| 142 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 143 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Test pull takes longer than timeout, 2nd pull happens shorter than cooldown |
| 147 | TEST_F(StatsPullerTest, PullTakeTooLongAndPullFast) { |
| 148 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 149 | pullSuccess = true; |
| 150 | // timeout is 0.5 |
| 151 | pullDelayNs = (long)(0.8 * NS_PER_SEC); |
| 152 | |
| 153 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 154 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 155 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 156 | |
| 157 | pullData.clear(); |
| 158 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 159 | |
| 160 | pullSuccess = true; |
| 161 | dataHolder.clear(); |
| 162 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 163 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | TEST_F(StatsPullerTest, PullFail) { |
| 167 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 168 | |
| 169 | pullSuccess = false; |
| 170 | |
| 171 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 172 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 173 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | TEST_F(StatsPullerTest, PullTakeTooLong) { |
| 177 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 178 | |
| 179 | pullSuccess = true; |
| 180 | pullDelayNs = NS_PER_SEC; |
| 181 | |
| 182 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 183 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 184 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST_F(StatsPullerTest, PullTooFast) { |
| 188 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 189 | |
| 190 | pullSuccess = true; |
| 191 | |
| 192 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 193 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 194 | ASSERT_EQ(1, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 195 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 196 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 197 | ASSERT_EQ(1, dataHolder[0]->size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 198 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 199 | |
| 200 | pullData.clear(); |
| 201 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 202 | |
| 203 | pullSuccess = true; |
| 204 | |
| 205 | dataHolder.clear(); |
| 206 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 207 | ASSERT_EQ(1, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 208 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 209 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 210 | ASSERT_EQ(1, dataHolder[0]->size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 211 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 212 | } |
| 213 | |
| 214 | TEST_F(StatsPullerTest, PullFailsAndTooFast) { |
| 215 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 216 | |
| 217 | pullSuccess = false; |
| 218 | |
| 219 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 220 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 221 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 222 | |
| 223 | pullData.clear(); |
| 224 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 225 | |
| 226 | pullSuccess = true; |
| 227 | |
| 228 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 229 | ASSERT_EQ(0, dataHolder.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 230 | } |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 231 | |
| 232 | } // namespace statsd |
| 233 | } // namespace os |
| 234 | } // namespace android |
| 235 | #else |
| 236 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 237 | #endif |