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> |
| 18 | #include <chrono> |
| 19 | #include <thread> |
| 20 | #include <vector> |
| 21 | #include "../metrics/metrics_test_helper.h" |
| 22 | #include "src/stats_log_util.h" |
| 23 | #include "tests/statsd_test_util.h" |
| 24 | |
| 25 | #ifdef __ANDROID__ |
| 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
| 31 | using namespace testing; |
| 32 | using std::make_shared; |
| 33 | using std::shared_ptr; |
| 34 | using std::vector; |
| 35 | using std::this_thread::sleep_for; |
| 36 | using testing::Contains; |
| 37 | |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 38 | namespace { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 39 | // cooldown time 1sec. |
| 40 | int pullTagId = 10014; |
| 41 | |
| 42 | bool pullSuccess; |
| 43 | vector<std::shared_ptr<LogEvent>> pullData; |
| 44 | long pullDelayNs; |
| 45 | |
| 46 | class FakePuller : public StatsPuller { |
| 47 | public: |
| 48 | FakePuller() : StatsPuller(pullTagId){}; |
| 49 | |
| 50 | private: |
| 51 | bool PullInternal(vector<std::shared_ptr<LogEvent>>* data) override { |
| 52 | (*data) = pullData; |
| 53 | sleep_for(std::chrono::nanoseconds(pullDelayNs)); |
| 54 | return pullSuccess; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | FakePuller puller; |
| 59 | |
| 60 | shared_ptr<LogEvent> createSimpleEvent(int64_t eventTimeNs, int64_t value) { |
| 61 | shared_ptr<LogEvent> event = make_shared<LogEvent>(pullTagId, eventTimeNs); |
| 62 | event->write(value); |
| 63 | event->init(); |
| 64 | return event; |
| 65 | } |
| 66 | |
| 67 | class StatsPullerTest : public ::testing::Test { |
| 68 | public: |
| 69 | StatsPullerTest() { |
| 70 | } |
| 71 | |
| 72 | void SetUp() override { |
| 73 | puller.ForceClearCache(); |
| 74 | pullSuccess = false; |
| 75 | pullDelayNs = 0; |
| 76 | pullData.clear(); |
| 77 | } |
| 78 | }; |
| 79 | |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 80 | } // Anonymous namespace. |
| 81 | |
| 82 | TEST_F(StatsPullerTest, PullSuccess) { |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 83 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 84 | |
| 85 | pullSuccess = true; |
| 86 | |
| 87 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 88 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
| 89 | EXPECT_EQ(1, dataHolder.size()); |
| 90 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 91 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
| 92 | EXPECT_EQ(1, dataHolder[0]->size()); |
| 93 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 94 | |
| 95 | sleep_for(std::chrono::seconds(1)); |
| 96 | |
| 97 | pullData.clear(); |
| 98 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 99 | |
| 100 | pullSuccess = true; |
| 101 | |
| 102 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
| 103 | EXPECT_EQ(1, dataHolder.size()); |
| 104 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 105 | EXPECT_EQ(2222L, dataHolder[0]->GetElapsedTimestampNs()); |
| 106 | EXPECT_EQ(1, dataHolder[0]->size()); |
| 107 | EXPECT_EQ(44, dataHolder[0]->getValues()[0].mValue.int_value); |
| 108 | } |
| 109 | |
| 110 | TEST_F(StatsPullerTest, PullFailAfterSuccess) { |
| 111 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 112 | |
| 113 | pullSuccess = true; |
| 114 | |
| 115 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 116 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
| 117 | EXPECT_EQ(1, dataHolder.size()); |
| 118 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 119 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
| 120 | EXPECT_EQ(1, dataHolder[0]->size()); |
| 121 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 122 | |
| 123 | sleep_for(std::chrono::seconds(1)); |
| 124 | |
| 125 | pullData.clear(); |
| 126 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 127 | |
| 128 | pullSuccess = false; |
| 129 | dataHolder.clear(); |
| 130 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 131 | EXPECT_EQ(0, dataHolder.size()); |
| 132 | |
| 133 | pullSuccess = true; |
| 134 | dataHolder.clear(); |
| 135 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 136 | EXPECT_EQ(0, dataHolder.size()); |
| 137 | } |
| 138 | |
| 139 | // Test pull takes longer than timeout, 2nd pull happens shorter than cooldown |
| 140 | TEST_F(StatsPullerTest, PullTakeTooLongAndPullFast) { |
| 141 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 142 | pullSuccess = true; |
| 143 | // timeout is 0.5 |
| 144 | pullDelayNs = (long)(0.8 * NS_PER_SEC); |
| 145 | |
| 146 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 147 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 148 | EXPECT_EQ(0, dataHolder.size()); |
| 149 | |
| 150 | pullData.clear(); |
| 151 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 152 | |
| 153 | pullSuccess = true; |
| 154 | dataHolder.clear(); |
| 155 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 156 | EXPECT_EQ(0, dataHolder.size()); |
| 157 | } |
| 158 | |
| 159 | TEST_F(StatsPullerTest, PullFail) { |
| 160 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 161 | |
| 162 | pullSuccess = false; |
| 163 | |
| 164 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 165 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 166 | EXPECT_EQ(0, dataHolder.size()); |
| 167 | } |
| 168 | |
| 169 | TEST_F(StatsPullerTest, PullTakeTooLong) { |
| 170 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 171 | |
| 172 | pullSuccess = true; |
| 173 | pullDelayNs = NS_PER_SEC; |
| 174 | |
| 175 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 176 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 177 | EXPECT_EQ(0, dataHolder.size()); |
| 178 | } |
| 179 | |
| 180 | TEST_F(StatsPullerTest, PullTooFast) { |
| 181 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 182 | |
| 183 | pullSuccess = true; |
| 184 | |
| 185 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 186 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
| 187 | EXPECT_EQ(1, dataHolder.size()); |
| 188 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 189 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
| 190 | EXPECT_EQ(1, dataHolder[0]->size()); |
| 191 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 192 | |
| 193 | pullData.clear(); |
| 194 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 195 | |
| 196 | pullSuccess = true; |
| 197 | |
| 198 | dataHolder.clear(); |
| 199 | EXPECT_TRUE(puller.Pull(&dataHolder)); |
| 200 | EXPECT_EQ(1, dataHolder.size()); |
| 201 | EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId()); |
| 202 | EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs()); |
| 203 | EXPECT_EQ(1, dataHolder[0]->size()); |
| 204 | EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value); |
| 205 | } |
| 206 | |
| 207 | TEST_F(StatsPullerTest, PullFailsAndTooFast) { |
| 208 | pullData.push_back(createSimpleEvent(1111L, 33)); |
| 209 | |
| 210 | pullSuccess = false; |
| 211 | |
| 212 | vector<std::shared_ptr<LogEvent>> dataHolder; |
| 213 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 214 | EXPECT_EQ(0, dataHolder.size()); |
| 215 | |
| 216 | pullData.clear(); |
| 217 | pullData.push_back(createSimpleEvent(2222L, 44)); |
| 218 | |
| 219 | pullSuccess = true; |
| 220 | |
| 221 | EXPECT_FALSE(puller.Pull(&dataHolder)); |
| 222 | EXPECT_EQ(0, dataHolder.size()); |
| 223 | } |
| 224 | |
| 225 | } // namespace statsd |
| 226 | } // namespace os |
| 227 | } // namespace android |
| 228 | #else |
| 229 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 230 | #endif |