summaryrefslogtreecommitdiff
path: root/cmds/statsd/tests/external/StatsCallbackPuller_test.cpp
blob: 85a60886349ec68470887422be398ded39ba3a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/external/StatsCallbackPuller.h"

#include <aidl/android/os/BnPullAtomCallback.h>
#include <aidl/android/os/IPullAtomResultReceiver.h>
#include <aidl/android/util/StatsEventParcel.h>
#include <android/binder_interface_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdio.h>

#include <chrono>
#include <thread>
#include <vector>

#include "../metrics/metrics_test_helper.h"
#include "src/stats_log_util.h"
#include "stats_event.h"
#include "tests/statsd_test_util.h"

#ifdef __ANDROID__

namespace android {
namespace os {
namespace statsd {

using namespace testing;
using Status = ::ndk::ScopedAStatus;
using aidl::android::os::BnPullAtomCallback;
using aidl::android::os::IPullAtomResultReceiver;
using aidl::android::util::StatsEventParcel;
using ::ndk::SharedRefBase;
using std::make_shared;
using std::shared_ptr;
using std::vector;
using std::this_thread::sleep_for;
using testing::Contains;

namespace {
int pullTagId = -12;
bool pullSuccess;
vector<int64_t> values;
int64_t pullDelayNs;
int64_t pullTimeoutNs;
int64_t pullCoolDownNs;
std::thread pullThread;

AStatsEvent* createSimpleEvent(int64_t value) {
    AStatsEvent* event = AStatsEvent_obtain();
    AStatsEvent_setAtomId(event, pullTagId);
    AStatsEvent_writeInt64(event, value);
    AStatsEvent_build(event);
    return event;
}

void executePull(const shared_ptr<IPullAtomResultReceiver>& resultReceiver) {
    // Convert stats_events into StatsEventParcels.
    vector<StatsEventParcel> parcels;
    for (int i = 0; i < values.size(); i++) {
        AStatsEvent* event = createSimpleEvent(values[i]);
        size_t size;
        uint8_t* buffer = AStatsEvent_getBuffer(event, &size);

        StatsEventParcel p;
        // vector.assign() creates a copy, but this is inevitable unless
        // stats_event.h/c uses a vector as opposed to a buffer.
        p.buffer.assign(buffer, buffer + size);
        parcels.push_back(std::move(p));
        AStatsEvent_release(event);
    }

    sleep_for(std::chrono::nanoseconds(pullDelayNs));
    resultReceiver->pullFinished(pullTagId, pullSuccess, parcels);
}

class FakePullAtomCallback : public BnPullAtomCallback {
public:
    Status onPullAtom(int atomTag,
                      const shared_ptr<IPullAtomResultReceiver>& resultReceiver) override {
        // Force pull to happen in separate thread to simulate binder.
        pullThread = std::thread(executePull, resultReceiver);
        return Status::ok();
    }
};

class StatsCallbackPullerTest : public ::testing::Test {
public:
    StatsCallbackPullerTest() {
    }

    void SetUp() override {
        pullSuccess = false;
        pullDelayNs = 0;
        values.clear();
        pullTimeoutNs = 10000000000LL;  // 10 seconds.
        pullCoolDownNs = 1000000000;    // 1 second.
    }

    void TearDown() override {
        if (pullThread.joinable()) {
            pullThread.join();
        }
        values.clear();
    }
};
}  // Anonymous namespace.

TEST_F(StatsCallbackPullerTest, PullSuccess) {
    shared_ptr<FakePullAtomCallback> cb = SharedRefBase::make<FakePullAtomCallback>();
    int64_t value = 43;
    pullSuccess = true;
    values.push_back(value);

    StatsCallbackPuller puller(pullTagId, cb, pullCoolDownNs, pullTimeoutNs, {});

    vector<std::shared_ptr<LogEvent>> dataHolder;
    int64_t startTimeNs = getElapsedRealtimeNs();
    EXPECT_TRUE(puller.PullInternal(&dataHolder));
    int64_t endTimeNs = getElapsedRealtimeNs();

    ASSERT_EQ(1, dataHolder.size());
    EXPECT_EQ(pullTagId, dataHolder[0]->GetTagId());
    EXPECT_LT(startTimeNs, dataHolder[0]->GetElapsedTimestampNs());
    EXPECT_GT(endTimeNs, dataHolder[0]->GetElapsedTimestampNs());
    ASSERT_EQ(1, dataHolder[0]->size());
    EXPECT_EQ(value, dataHolder[0]->getValues()[0].mValue.int_value);
}

TEST_F(StatsCallbackPullerTest, PullFail) {
    shared_ptr<FakePullAtomCallback> cb = SharedRefBase::make<FakePullAtomCallback>();
    pullSuccess = false;
    int64_t value = 1234;
    values.push_back(value);

    StatsCallbackPuller puller(pullTagId, cb, pullCoolDownNs, pullTimeoutNs, {});

    vector<shared_ptr<LogEvent>> dataHolder;
    EXPECT_FALSE(puller.PullInternal(&dataHolder));
    ASSERT_EQ(0, dataHolder.size());
}

TEST_F(StatsCallbackPullerTest, PullTimeout) {
    shared_ptr<FakePullAtomCallback> cb = SharedRefBase::make<FakePullAtomCallback>();
    pullSuccess = true;
    pullDelayNs = MillisToNano(5);  // 5ms.
    pullTimeoutNs = 10000;    // 10 microseconds.
    int64_t value = 4321;
    values.push_back(value);

    StatsCallbackPuller puller(pullTagId, cb, pullCoolDownNs, pullTimeoutNs, {});

    vector<shared_ptr<LogEvent>> dataHolder;
    int64_t startTimeNs = getElapsedRealtimeNs();
    // Returns true to let StatsPuller code evaluate the timeout.
    EXPECT_TRUE(puller.PullInternal(&dataHolder));
    int64_t endTimeNs = getElapsedRealtimeNs();
    int64_t actualPullDurationNs = endTimeNs - startTimeNs;

    // Pull should take at least the timeout amount of time, but should stop early because the delay
    // is bigger.
    EXPECT_LT(pullTimeoutNs, actualPullDurationNs);
    EXPECT_GT(pullDelayNs, actualPullDurationNs);
    ASSERT_EQ(0, dataHolder.size());

    // Let the pull return and make sure that the dataHolder is not modified.
    pullThread.join();
    ASSERT_EQ(0, dataHolder.size());
}

// Register a puller and ensure that the timeout logic works.
TEST_F(StatsCallbackPullerTest, RegisterAndTimeout) {
    shared_ptr<FakePullAtomCallback> cb = SharedRefBase::make<FakePullAtomCallback>();
    pullSuccess = true;
    pullDelayNs = MillisToNano(5);  // 5 ms.
    pullTimeoutNs = 10000;    // 10 microsseconds.
    int64_t value = 4321;
    int32_t uid = 123;
    values.push_back(value);

    sp<StatsPullerManager> pullerManager = new StatsPullerManager();
    pullerManager->RegisterPullAtomCallback(uid, pullTagId, pullCoolDownNs, pullTimeoutNs,
                                            vector<int32_t>(), cb);
    vector<shared_ptr<LogEvent>> dataHolder;
    int64_t startTimeNs = getElapsedRealtimeNs();
    // Returns false, since StatsPuller code will evaluate the timeout.
    EXPECT_FALSE(pullerManager->Pull(pullTagId, {uid}, startTimeNs, &dataHolder));
    int64_t endTimeNs = getElapsedRealtimeNs();
    int64_t actualPullDurationNs = endTimeNs - startTimeNs;

    // Pull should take at least the timeout amount of time, but should stop early because the delay
    // is bigger. Make sure that the time is closer to the timeout, than to the intended delay.
    EXPECT_LT(pullTimeoutNs, actualPullDurationNs);
    EXPECT_GT(pullDelayNs / 5, actualPullDurationNs);
    ASSERT_EQ(0, dataHolder.size());

    // Let the pull return and make sure that the dataHolder is not modified.
    pullThread.join();
    ASSERT_EQ(0, dataHolder.size());
}

}  // namespace statsd
}  // namespace os
}  // namespace android
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif