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

Commit f9b848a8 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Call new dumpstate listener methods"

parents c9a388a1 babf6c7a
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -2256,6 +2256,28 @@ void Dumpstate::SetOptions(std::unique_ptr<DumpOptions> options) {
    options_ = std::move(options);
}

Dumpstate::RunStatus Dumpstate::Run() {
    Dumpstate::RunStatus status = RunInternal();
    if (listener_ != nullptr) {
        switch (status) {
            case Dumpstate::RunStatus::OK:
                // TODO(b/111441001): duration argument does not make sense. Remove.
                listener_->onFinished(0 /* duration */, options_->notification_title,
                                      options_->notification_description);
                break;
            case Dumpstate::RunStatus::HELP:
                break;
            case Dumpstate::RunStatus::INVALID_INPUT:
                listener_->onError(android::os::IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
                break;
            case Dumpstate::RunStatus::ERROR:
                listener_->onError(android::os::IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
                break;
        }
    }
    return status;
}

/*
 * Dumps relevant information to a bugreport based on the given options.
 *
@@ -2277,7 +2299,7 @@ void Dumpstate::SetOptions(std::unique_ptr<DumpOptions> options) {
 * Bugreports are first generated in a local directory and later copied to the caller's fd or
 * directory.
 */
Dumpstate::RunStatus Dumpstate::Run() {
Dumpstate::RunStatus Dumpstate::RunInternal() {
    LogDumpOptions(*options_);
    if (!options_->ValidateOptions()) {
        MYLOGE("Invalid options specified\n");
@@ -2482,6 +2504,7 @@ Dumpstate::RunStatus Dumpstate::Run() {
    /* tell activity manager we're done */
    if (options_->do_broadcast) {
        SendBugreportFinishedBroadcast();
        // Note that listener_ is notified in Run();
    }

    MYLOGD("Final progress: %d/%d (estimated %d)\n", progress_->Get(), progress_->GetMax(),
+2 −0
Original line number Diff line number Diff line
@@ -449,6 +449,8 @@ class Dumpstate {
    std::vector<DumpData> anr_data_;

  private:
    RunStatus RunInternal();

    // Used by GetInstance() only.
    explicit Dumpstate(const std::string& version = VERSION_CURRENT);

+10 −2
Original line number Diff line number Diff line
@@ -656,8 +656,9 @@ class DumpstateTest : public DumpstateBaseTest {
        }

        if (update_progress) {
            message += android::base::StringPrintf("Setting progress (%s): %d/%d\n",
                                                   listener_name.c_str(), progress, max);
            message += android::base::StringPrintf("Setting progress (%s): %d/%d (%d%%)\n",
                                                   listener_name.c_str(), progress, max,
                                                   (100 * progress / max));
        }

        return message;
@@ -816,12 +817,14 @@ TEST_F(DumpstateTest, RunCommandProgress) {
    SetProgress(0, 30);

    EXPECT_CALL(*listener, onProgressUpdated(20));
    EXPECT_CALL(*listener, onProgress(66));  // 20/30 %
    EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
    std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30);
    EXPECT_THAT(out, StrEq("stdout\n"));
    EXPECT_THAT(err, StrEq("stderr\n" + progress_message));

    EXPECT_CALL(*listener, onProgressUpdated(30));
    EXPECT_CALL(*listener, onProgress(100));  // 35/35 %
    EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build()));
    progress_message = GetProgressMessage(ds.listener_name_, 30, 30);
    EXPECT_THAT(out, StrEq("stdout\n"));
@@ -830,6 +833,7 @@ TEST_F(DumpstateTest, RunCommandProgress) {
    // Run a command that will increase maximum timeout.
    EXPECT_CALL(*listener, onProgressUpdated(31));
    EXPECT_CALL(*listener, onMaxProgressUpdated(37));
    EXPECT_CALL(*listener, onProgress(83));  // 31/37 %
    EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
    progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30);  // 20% increase
    EXPECT_THAT(out, StrEq("stdout\n"));
@@ -838,6 +842,7 @@ TEST_F(DumpstateTest, RunCommandProgress) {
    // Make sure command ran while in dry_run is counted.
    SetDryRun(true);
    EXPECT_CALL(*listener, onProgressUpdated(35));
    EXPECT_CALL(*listener, onProgress(94));  // 35/37 %
    EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
    progress_message = GetProgressMessage(ds.listener_name_, 35, 37);
    EXPECT_THAT(out, IsEmpty());
@@ -854,6 +859,7 @@ TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) {

    // First update should always be sent.
    EXPECT_CALL(*listener, onProgressUpdated(1));
    EXPECT_CALL(*listener, onProgress(12));  // 1/12 %
    EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
    std::string progress_message = GetProgressMessage(ds.listener_name_, 1, 8);
    EXPECT_THAT(out, StrEq("stdout\n"));
@@ -866,6 +872,7 @@ TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) {

    // Third update should be sent because it reaches threshold (6 - 1 = 5).
    EXPECT_CALL(*listener, onProgressUpdated(6));
    EXPECT_CALL(*listener, onProgress(75));  // 6/8 %
    EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
    progress_message = GetProgressMessage(ds.listener_name_, 6, 8);
    EXPECT_THAT(out, StrEq("stdout\n"));
@@ -1105,6 +1112,7 @@ TEST_F(DumpstateTest, DumpFileUpdateProgress) {
    SetProgress(0, 30);

    EXPECT_CALL(*listener, onProgressUpdated(5));
    EXPECT_CALL(*listener, onProgress(16));  // 5/30 %
    EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));

    std::string progress_message =
+10 −4
Original line number Diff line number Diff line
@@ -949,16 +949,22 @@ void Dumpstate::UpdateProgress(int32_t delta_sec) {
        fsync(control_socket_fd_);
    }

    int percent = 100 * progress / max;
    if (listener_ != nullptr) {
        if (progress % 100 == 0) {
            // We don't want to spam logcat, so only log multiples of 100.
            MYLOGD("Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress, max);
        if (percent % 5 == 0) {
            // We don't want to spam logcat, so only log multiples of 5.
            MYLOGD("Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(), progress, max,
                   percent);
        } else {
            // stderr is ignored on normal invocations, but useful when calling
            // /system/bin/dumpstate directly for debuggging.
            fprintf(stderr, "Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress, max);
            fprintf(stderr, "Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(),
                    progress, max, percent);
        }
        // TODO(b/111441001): Remove in favor of onProgress
        listener_->onProgressUpdated(progress);

        listener_->onProgress(percent);
    }
}