lmkd/tests: handle the case when process exits before reaping starts

Current test will fail in the case when the target process exits before
process_mrelease is called. However this is possible and should not be
treated as an invalid case. Add special handling of this case in the
test.

Bug: 254880957
Test: run lmkd_tests until this case happens
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: Ica5b28fa52a5f42a771fb9516112e58b66291068
Merged-In: Ica5b28fa52a5f42a771fb9516112e58b66291068
This commit is contained in:
Suren Baghdasaryan 2022-10-24 20:12:23 +00:00
parent 683da9812c
commit 2300230ea4
1 changed files with 24 additions and 3 deletions

View File

@ -38,10 +38,13 @@ using namespace android::base;
#define LMKD_LOGCAT_MARKER "lowmemorykiller"
#define LMKD_KILL_TEMPLATE "Kill \'[^']*\' \\\(%d\\)"
#define LMKD_REAP_TEMPLATE "Process %d was reaped"
#define LMKD_REAP_FAIL_TEMPLATE "process_mrelease %d failed"
#define LMKD_KILL_LINE_START LMKD_LOGCAT_MARKER ": Kill"
#define LMKD_REAP_LINE_START LMKD_LOGCAT_MARKER ": Process"
#define LMKD_REAP_TIME_TEMPLATE LMKD_LOGCAT_MARKER ": Process %d was reaped in %ldms"
#define LMKD_REAP_MRELESE_ERR_MARKER ": process_mrelease"
#define LMKD_REAP_NO_PROCESS_TEMPLATE ": process_mrelease %d failed: No such process"
#define ONE_MB (1 << 20)
@ -161,6 +164,12 @@ class LmkdTest : public ::testing::Test {
reap_pid == pid;
}
static bool ParseReapNoProcess(const std::string& line, pid_t pid) {
int reap_pid;
return sscanf(line.c_str(), LMKD_REAP_NO_PROCESS_TEMPLATE, &reap_pid) == 1 &&
reap_pid == pid;
}
private:
int sock;
uid_t uid;
@ -187,8 +196,9 @@ TEST_F(LmkdTest, TargetReaping) {
FAIL() << "Target process " << pid << " was not killed";
}
std::string regex =
StringPrintf("((" LMKD_KILL_TEMPLATE ")|(" LMKD_REAP_TEMPLATE "))", pid, pid);
std::string regex = StringPrintf("((" LMKD_KILL_TEMPLATE ")|(" LMKD_REAP_TEMPLATE
")|(" LMKD_REAP_FAIL_TEMPLATE "))",
pid, pid, pid);
std::string logcat_out = ReadLogcat(LMKD_LOGCAT_MARKER ":I", regex);
// find kill report
@ -202,7 +212,18 @@ TEST_F(LmkdTest, TargetReaping) {
// find reap duration report
line_start = logcat_out.find(LMKD_REAP_LINE_START, line_end);
ASSERT_TRUE(line_start != std::string::npos) << "Reaping time report is not found";
if (line_start == std::string::npos) {
// Target might have exited before reaping started
line_start = logcat_out.find(LMKD_REAP_MRELESE_ERR_MARKER, line_end);
ASSERT_TRUE(line_start != std::string::npos) << "Reaping time report is not found";
line_end = logcat_out.find('\n', line_start);
line = logcat_out.substr(line_start, line_end == std::string::npos ? std::string::npos
: line_end - line_start);
ASSERT_TRUE(ParseReapNoProcess(line, pid)) << "Failed to reap the target " << pid;
return;
}
line_end = logcat_out.find('\n', line_start);
line = logcat_out.substr(
line_start, line_end == std::string::npos ? std::string::npos : line_end - line_start);