blob: 02a43e4e179a91990d9e11b57dbabd207380998b [file] [log] [blame]
Chenjie Yu0bd73db2018-12-16 07:37:04 -08001// 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>
tsaichristine7747d372020-02-28 17:36:59 -080018
Chenjie Yu0bd73db2018-12-16 07:37:04 -080019#include <chrono>
20#include <thread>
21#include <vector>
tsaichristine7747d372020-02-28 17:36:59 -080022
Chenjie Yu0bd73db2018-12-16 07:37:04 -080023#include "../metrics/metrics_test_helper.h"
24#include "src/stats_log_util.h"
tsaichristine7747d372020-02-28 17:36:59 -080025#include "stats_event.h"
Chenjie Yu0bd73db2018-12-16 07:37:04 -080026#include "tests/statsd_test_util.h"
27
28#ifdef __ANDROID__
29
30namespace android {
31namespace os {
32namespace statsd {
33
34using namespace testing;
35using std::make_shared;
36using std::shared_ptr;
37using std::vector;
38using std::this_thread::sleep_for;
39using testing::Contains;
40
Tej Singh89817632019-12-09 16:58:08 -080041namespace {
Chenjie Yu0bd73db2018-12-16 07:37:04 -080042// cooldown time 1sec.
43int pullTagId = 10014;
44
45bool pullSuccess;
46vector<std::shared_ptr<LogEvent>> pullData;
47long pullDelayNs;
48
49class FakePuller : public StatsPuller {
50public:
Tej Singh5b4951b2020-01-24 13:23:56 -080051 FakePuller() : StatsPuller(pullTagId, /*coolDown=*/NS_PER_SEC, /*timeout=*/NS_PER_SEC / 2){};
Chenjie Yu0bd73db2018-12-16 07:37:04 -080052
53private:
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
61FakePuller puller;
62
tsaichristine7747d372020-02-28 17:36:59 -080063std::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);
tsaichristine7747d372020-02-28 17:36:59 -080067 AStatsEvent_writeInt64(statsEvent, value);
tsaichristine7747d372020-02-28 17:36:59 -080068
69 std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
tsaichristine8dca82e2020-04-07 09:40:03 -070070 parseStatsEventToLogEvent(statsEvent, logEvent.get());
tsaichristine7747d372020-02-28 17:36:59 -080071 return logEvent;
72}
Chenjie Yu0bd73db2018-12-16 07:37:04 -080073
74class StatsPullerTest : public ::testing::Test {
75public:
76 StatsPullerTest() {
77 }
78
79 void SetUp() override {
80 puller.ForceClearCache();
81 pullSuccess = false;
82 pullDelayNs = 0;
83 pullData.clear();
84 }
85};
86
Tej Singh89817632019-12-09 16:58:08 -080087} // Anonymous namespace.
88
tsaichristine7747d372020-02-28 17:36:59 -080089TEST_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 Qureshidff78d62020-05-11 13:37:43 -070096 ASSERT_EQ(1, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -080097 EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId());
98 EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs());
Muhammad Qureshidff78d62020-05-11 13:37:43 -070099 ASSERT_EQ(1, dataHolder[0]->size());
tsaichristine7747d372020-02-28 17:36:59 -0800100 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 Qureshidff78d62020-05-11 13:37:43 -0700110 ASSERT_EQ(1, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800111 EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId());
112 EXPECT_EQ(2222L, dataHolder[0]->GetElapsedTimestampNs());
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700113 ASSERT_EQ(1, dataHolder[0]->size());
tsaichristine7747d372020-02-28 17:36:59 -0800114 EXPECT_EQ(44, dataHolder[0]->getValues()[0].mValue.int_value);
115}
116
117TEST_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 Qureshidff78d62020-05-11 13:37:43 -0700124 ASSERT_EQ(1, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800125 EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId());
126 EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs());
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700127 ASSERT_EQ(1, dataHolder[0]->size());
tsaichristine7747d372020-02-28 17:36:59 -0800128 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 Qureshidff78d62020-05-11 13:37:43 -0700138 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800139
140 pullSuccess = true;
141 dataHolder.clear();
142 EXPECT_FALSE(puller.Pull(&dataHolder));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700143 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800144}
145
146// Test pull takes longer than timeout, 2nd pull happens shorter than cooldown
147TEST_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 Qureshidff78d62020-05-11 13:37:43 -0700155 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800156
157 pullData.clear();
158 pullData.push_back(createSimpleEvent(2222L, 44));
159
160 pullSuccess = true;
161 dataHolder.clear();
162 EXPECT_FALSE(puller.Pull(&dataHolder));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700163 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800164}
165
166TEST_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 Qureshidff78d62020-05-11 13:37:43 -0700173 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800174}
175
176TEST_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 Qureshidff78d62020-05-11 13:37:43 -0700184 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800185}
186
187TEST_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 Qureshidff78d62020-05-11 13:37:43 -0700194 ASSERT_EQ(1, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800195 EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId());
196 EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs());
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700197 ASSERT_EQ(1, dataHolder[0]->size());
tsaichristine7747d372020-02-28 17:36:59 -0800198 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 Qureshidff78d62020-05-11 13:37:43 -0700207 ASSERT_EQ(1, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800208 EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId());
209 EXPECT_EQ(1111L, dataHolder[0]->GetElapsedTimestampNs());
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700210 ASSERT_EQ(1, dataHolder[0]->size());
tsaichristine7747d372020-02-28 17:36:59 -0800211 EXPECT_EQ(33, dataHolder[0]->getValues()[0].mValue.int_value);
212}
213
214TEST_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 Qureshidff78d62020-05-11 13:37:43 -0700221 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800222
223 pullData.clear();
224 pullData.push_back(createSimpleEvent(2222L, 44));
225
226 pullSuccess = true;
227
228 EXPECT_FALSE(puller.Pull(&dataHolder));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700229 ASSERT_EQ(0, dataHolder.size());
tsaichristine7747d372020-02-28 17:36:59 -0800230}
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800231
232} // namespace statsd
233} // namespace os
234} // namespace android
235#else
236GTEST_LOG_(INFO) << "This test does nothing.\n";
237#endif