Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <sysexits.h> |
| 18 | |
| 19 | #include <chrono> |
| 20 | |
| 21 | #include <android-base/result-gmock.h> |
| 22 | #include <android-base/strings.h> |
| 23 | #include <gmock/gmock.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "../UtilsHost.h" |
| 27 | |
| 28 | using android::base::testing::Ok; |
| 29 | using testing::Optional; |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | TEST(UtilsHost, ExecuteImmediately) { |
| 34 | auto result = execute({"echo", "foo"}, nullptr); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(result.has_value()); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 36 | EXPECT_THAT(result->exitCode, Optional(EX_OK)); |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 37 | EXPECT_EQ(result->stdoutStr, "foo\n"); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 40 | template <typename T> |
| 41 | auto millisSince(std::chrono::time_point<T> now) { |
| 42 | auto elapsed = std::chrono::system_clock::now() - now; |
| 43 | return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count(); |
| 44 | } |
| 45 | |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 46 | TEST(UtilsHost, ExecuteLongRunning) { |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 47 | auto start = std::chrono::system_clock::now(); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 48 | |
| 49 | { |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 50 | std::vector<std::string> |
| 51 | args{"sh", "-c", "sleep 0.5 && echo -n f && sleep 0.5 && echo oo && sleep 100"}; |
| 52 | auto result = execute(std::move(args), [&](const CommandResult& commandResult) { |
| 53 | std::cout << millisSince(start) |
| 54 | << "ms: GOT PARTIAL COMMAND RESULT:" << commandResult.stdoutStr << std::endl; |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 55 | return android::base::EndsWith(commandResult.stdoutStr, "\n"); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 56 | }); |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 57 | auto elapsedMs = millisSince(start); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 58 | EXPECT_GE(elapsedMs, 1000); |
Steven Moreland | c50727b | 2024-11-07 05:17:20 +0000 | [diff] [blame] | 59 | EXPECT_LT(elapsedMs, 3000); // b/377571547: higher to reduce flake |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 60 | |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(result.has_value()); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 62 | EXPECT_EQ(std::nullopt, result->exitCode); |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 63 | EXPECT_EQ(result->stdoutStr, "foo\n"); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // ~CommandResult() called, child process is killed. |
| 67 | // Assert that the second sleep does not finish. |
Steven Moreland | c50727b | 2024-11-07 05:17:20 +0000 | [diff] [blame] | 68 | EXPECT_LT(millisSince(start), 3000); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST(UtilsHost, ExecuteLongRunning2) { |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 72 | auto start = std::chrono::system_clock::now(); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 73 | |
| 74 | { |
| 75 | std::vector<std::string> args{"sh", "-c", |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 76 | "sleep 2 && echo -n f && sleep 2 && echo oo && sleep 100"}; |
| 77 | auto result = execute(std::move(args), [&](const CommandResult& commandResult) { |
| 78 | std::cout << millisSince(start) |
| 79 | << "ms: GOT PARTIAL COMMAND RESULT:" << commandResult.stdoutStr << std::endl; |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 80 | return android::base::EndsWith(commandResult.stdoutStr, "\n"); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 81 | }); |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 82 | auto elapsedMs = millisSince(start); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 83 | EXPECT_GE(elapsedMs, 4000); |
| 84 | EXPECT_LT(elapsedMs, 6000); |
| 85 | |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(result.has_value()); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 87 | EXPECT_EQ(std::nullopt, result->exitCode); |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 88 | EXPECT_EQ(result->stdoutStr, "foo\n"); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // ~CommandResult() called, child process is killed. |
| 92 | // Assert that the second sleep does not finish. |
Steven Moreland | 4209a52 | 2023-01-11 02:10:04 +0000 | [diff] [blame] | 93 | EXPECT_LT(millisSince(start), 6000); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST(UtilsHost, KillWithSigKill) { |
| 97 | std::vector<std::string> args{"sh", "-c", "echo foo && sleep 10"}; |
| 98 | auto result = execute(std::move(args), [](const CommandResult& commandResult) { |
| 99 | // FOR TEST PURPOSE ONLY. DON'T DO THIS! |
| 100 | if (commandResult.pid.has_value()) { |
| 101 | (void)kill(*commandResult.pid, SIGKILL); |
| 102 | } |
| 103 | // FOR TEST PURPOSE ONLY. DON'T DO THIS! |
| 104 | return false; |
| 105 | }); |
| 106 | |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 107 | ASSERT_TRUE(result.has_value()); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 108 | EXPECT_EQ(std::nullopt, result->exitCode); |
| 109 | EXPECT_THAT(result->signal, Optional(SIGKILL)); |
| 110 | } |
| 111 | |
| 112 | } // namespace android |