blob: c40719a17f625c75c0d47a4d9a7936c3cdf8608a [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>
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
27namespace android {
28namespace os {
29namespace statsd {
30
31using namespace testing;
32using std::make_shared;
33using std::shared_ptr;
34using std::vector;
35using std::this_thread::sleep_for;
36using testing::Contains;
37
Tej Singh89817632019-12-09 16:58:08 -080038namespace {
Chenjie Yu0bd73db2018-12-16 07:37:04 -080039// cooldown time 1sec.
40int pullTagId = 10014;
41
42bool pullSuccess;
43vector<std::shared_ptr<LogEvent>> pullData;
44long pullDelayNs;
45
46class FakePuller : public StatsPuller {
47public:
48 FakePuller() : StatsPuller(pullTagId){};
49
50private:
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
58FakePuller puller;
59
60shared_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
67class StatsPullerTest : public ::testing::Test {
68public:
69 StatsPullerTest() {
70 }
71
72 void SetUp() override {
73 puller.ForceClearCache();
74 pullSuccess = false;
75 pullDelayNs = 0;
76 pullData.clear();
77 }
78};
79
Tej Singh89817632019-12-09 16:58:08 -080080} // Anonymous namespace.
81
82TEST_F(StatsPullerTest, PullSuccess) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -080083 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
110TEST_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
140TEST_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
159TEST_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
169TEST_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
180TEST_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
207TEST_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
229GTEST_LOG_(INFO) << "This test does nothing.\n";
230#endif