blob: a62ad96be1953b7caa522e02852a5f5305dc19d1 [file] [log] [blame]
Yifan Hongaf766e62021-06-14 13:24:19 -07001/*
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
28using android::base::testing::Ok;
29using testing::Optional;
30
31namespace android {
32
33TEST(UtilsHost, ExecuteImmediately) {
34 auto result = execute({"echo", "foo"}, nullptr);
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070035 ASSERT_TRUE(result.has_value());
Yifan Hongaf766e62021-06-14 13:24:19 -070036 EXPECT_THAT(result->exitCode, Optional(EX_OK));
Colin Crossc9a77aa2021-09-13 16:31:38 -070037 EXPECT_EQ(result->stdoutStr, "foo\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070038}
39
Steven Moreland4209a522023-01-11 02:10:04 +000040template <typename T>
41auto 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 Hongaf766e62021-06-14 13:24:19 -070046TEST(UtilsHost, ExecuteLongRunning) {
Steven Moreland4209a522023-01-11 02:10:04 +000047 auto start = std::chrono::system_clock::now();
Yifan Hongaf766e62021-06-14 13:24:19 -070048
49 {
Steven Moreland4209a522023-01-11 02:10:04 +000050 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 Crossc9a77aa2021-09-13 16:31:38 -070055 return android::base::EndsWith(commandResult.stdoutStr, "\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070056 });
Steven Moreland4209a522023-01-11 02:10:04 +000057 auto elapsedMs = millisSince(start);
Yifan Hongaf766e62021-06-14 13:24:19 -070058 EXPECT_GE(elapsedMs, 1000);
Steven Morelandc50727b2024-11-07 05:17:20 +000059 EXPECT_LT(elapsedMs, 3000); // b/377571547: higher to reduce flake
Yifan Hongaf766e62021-06-14 13:24:19 -070060
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070061 ASSERT_TRUE(result.has_value());
Yifan Hongaf766e62021-06-14 13:24:19 -070062 EXPECT_EQ(std::nullopt, result->exitCode);
Colin Crossc9a77aa2021-09-13 16:31:38 -070063 EXPECT_EQ(result->stdoutStr, "foo\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070064 }
65
66 // ~CommandResult() called, child process is killed.
67 // Assert that the second sleep does not finish.
Steven Morelandc50727b2024-11-07 05:17:20 +000068 EXPECT_LT(millisSince(start), 3000);
Yifan Hongaf766e62021-06-14 13:24:19 -070069}
70
71TEST(UtilsHost, ExecuteLongRunning2) {
Steven Moreland4209a522023-01-11 02:10:04 +000072 auto start = std::chrono::system_clock::now();
Yifan Hongaf766e62021-06-14 13:24:19 -070073
74 {
75 std::vector<std::string> args{"sh", "-c",
Steven Moreland4209a522023-01-11 02:10:04 +000076 "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 Crossc9a77aa2021-09-13 16:31:38 -070080 return android::base::EndsWith(commandResult.stdoutStr, "\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070081 });
Steven Moreland4209a522023-01-11 02:10:04 +000082 auto elapsedMs = millisSince(start);
Yifan Hongaf766e62021-06-14 13:24:19 -070083 EXPECT_GE(elapsedMs, 4000);
84 EXPECT_LT(elapsedMs, 6000);
85
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070086 ASSERT_TRUE(result.has_value());
Yifan Hongaf766e62021-06-14 13:24:19 -070087 EXPECT_EQ(std::nullopt, result->exitCode);
Colin Crossc9a77aa2021-09-13 16:31:38 -070088 EXPECT_EQ(result->stdoutStr, "foo\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070089 }
90
91 // ~CommandResult() called, child process is killed.
92 // Assert that the second sleep does not finish.
Steven Moreland4209a522023-01-11 02:10:04 +000093 EXPECT_LT(millisSince(start), 6000);
Yifan Hongaf766e62021-06-14 13:24:19 -070094}
95
96TEST(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 Wasilczyk88aa8c32023-11-01 09:46:07 -0700107 ASSERT_TRUE(result.has_value());
Yifan Hongaf766e62021-06-14 13:24:19 -0700108 EXPECT_EQ(std::nullopt, result->exitCode);
109 EXPECT_THAT(result->signal, Optional(SIGKILL));
110}
111
112} // namespace android