Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89817631 authored by Tej Singh's avatar Tej Singh
Browse files

Puller API: Unit tests.

Tests 4 key things: A successful pull, a failed pull and two timeout
cases.

Timeout case 1: StatsPullerCallback should stop early and return true.
Timeout case 2: StatsCallback should note that the pull timed out,
notify statsd stats, and return false.

Test: bit statsd_test:*
Bug: 145310627
Change-Id: Id87089f04e1cf54a622b3f15585341ecdcd21f7f
parent 22bd0ac2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -250,6 +250,7 @@ cc_test {
        "tests/external/GpuStatsPuller_test.cpp",
        "tests/external/IncidentReportArgs_test.cpp",
        "tests/external/puller_util_test.cpp",
        "tests/external/StatsCallbackPuller_test.cpp",
        "tests/external/StatsPuller_test.cpp",
        "tests/external/SurfaceflingerStatsPuller_test.cpp",
        "tests/FieldValue_test.cpp",
+10 −11
Original line number Diff line number Diff line
@@ -35,8 +35,9 @@ namespace android {
namespace os {
namespace statsd {

StatsCallbackPuller::StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback)
    : StatsPuller(tagId), mCallback(callback) {
StatsCallbackPuller::StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback,
                                         int64_t timeoutNs)
    : StatsPuller(tagId), mCallback(callback), mTimeoutNs(timeoutNs) {
    VLOG("StatsCallbackPuller created for tag %d", tagId);
}

@@ -64,10 +65,9 @@ bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
                {
                    lock_guard<mutex> lk(*cv_mutex);
                    for (const StatsEventParcel& parcel: output) {
                        shared_ptr<LogEvent> event =
                              make_shared<LogEvent>(const_cast<uint8_t*>(parcel.buffer.data()),
                                                    parcel.buffer.size(),
                                                    /*uid=*/ -1);
                        shared_ptr<LogEvent> event = make_shared<LogEvent>(
                                const_cast<uint8_t*>(parcel.buffer.data()), parcel.buffer.size(),
                                /*uid=*/-1, /*useNewSchema=*/true);
                        sharedData->push_back(event);
                    }
                    *pullSuccess = success;
@@ -76,7 +76,8 @@ bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
                cv->notify_one();
            });

    // Initiate the pull.
    // Initiate the pull. This is a oneway call to a different process, except
    // in unit tests. In process calls are not oneway.
    Status status = mCallback->onPullAtom(mTagId, resultReceiver);
    if (!status.isOk()) {
        return false;
@@ -84,10 +85,8 @@ bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {

    {
        unique_lock<mutex> unique_lk(*cv_mutex);
        int64_t pullTimeoutNs =
                StatsPullerManager::kAllPullAtomInfo.at({.atomTag = mTagId}).pullTimeoutNs;
        // Wait until the pull finishes, or until the pull timeout.
        cv->wait_for(unique_lk, chrono::nanoseconds(pullTimeoutNs),
        cv->wait_for(unique_lk, chrono::nanoseconds(mTimeoutNs),
                     [pullFinish] { return *pullFinish; });
        if (!*pullFinish) {
            // Note: The parent stats puller will also note that there was a timeout and that the
@@ -96,7 +95,7 @@ bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
            return true;
        } else {
            // Only copy the data if we did not timeout and the pull was successful.
            if (pullSuccess) {
            if (*pullSuccess) {
                *data = std::move(*sharedData);
            }
            VLOG("StatsCallbackPuller::pull succeeded for %d", mTagId);
+7 −1
Original line number Diff line number Diff line
@@ -27,11 +27,17 @@ namespace statsd {

class StatsCallbackPuller : public StatsPuller {
public:
    explicit StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback);
    explicit StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback,
                                 int64_t timeoutNs);

private:
    bool PullInternal(vector<std::shared_ptr<LogEvent> >* data) override;
    const sp<IPullAtomCallback> mCallback;
    const int64_t mTimeoutNs;

    FRIEND_TEST(StatsCallbackPullerTest, PullFail);
    FRIEND_TEST(StatsCallbackPullerTest, PullSuccess);
    FRIEND_TEST(StatsCallbackPullerTest, PullTimeout);
};

}  // namespace statsd
+5 −4
Original line number Diff line number Diff line
@@ -497,9 +497,10 @@ void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t a
    VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
    // TODO: linkToDeath with the callback so that we can remove it and delete the puller.
    StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
    kAllPullAtomInfo[{.atomTag = atomTag}] = {.additiveFields = additiveFields,
    kAllPullAtomInfo[{.atomTag = atomTag}] = {
            .additiveFields = additiveFields,
            .coolDownNs = coolDownNs,
                                              .puller = new StatsCallbackPuller(atomTag, callback),
            .puller = new StatsCallbackPuller(atomTag, callback, timeoutNs),
            .pullTimeoutNs = timeoutNs,
    };
}
+11 −0
Original line number Diff line number Diff line
@@ -52,6 +52,17 @@ LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid)
#endif
}

LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid, bool useNewSchema)
    : mBuf(msg), mRemainingLen(len), mLogdTimestampNs(time(nullptr)), mLogUid(uid) {
    if (useNewSchema) {
        initNew();
    } else {
        mContext = create_android_log_parser((char*)msg, len);
        init(mContext);
        if (mContext) android_log_destroy(&mContext);  // set mContext to NULL
    }
}

LogEvent::LogEvent(const LogEvent& event) {
    mTagId = event.mTagId;
    mLogUid = event.mLogUid;
Loading