Skip to content

Commit

Permalink
fix: Tune application termination condition (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Aug 2, 2024
1 parent 78e339e commit 970085a
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions lib/commands/app-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ export async function mobileRemoveApp(opts) {
*/
export async function terminateApp(appId, options = {}) {
this.log.info(`Terminating '${appId}'`);
if (!(await this.adb.processExists(appId))) {
const pids = await this.adb.getPIDsByName(appId);
if (_.isEmpty(pids)) {
this.log.info(`The app '${appId}' is not running`);
return false;
}
Expand All @@ -187,19 +188,42 @@ export async function terminateApp(appId, options = {}) {

if (timeout <= 0) {
this.log.info(
`'${appId}' has been terminated. Skip checking the application process state ` +
`since the timeout was set as ${timeout}ms`,
`'${appId}' has been terminated. Skipping checking of the application process state ` +
`since the timeout was set to ${timeout}ms`,
);
return true;
}

/** @type {number[]} */
let currentPids = [];
try {
await waitForCondition(async () => (await this.queryAppState(appId)) <= APP_STATE.NOT_RUNNING, {
await waitForCondition(async () => {
if (await this.queryAppState(appId) <= APP_STATE.NOT_RUNNING) {
return true;
}
currentPids = await this.adb.getPIDsByName(appId);
if (_.isEmpty(currentPids) || _.isEmpty(_.intersection(pids, currentPids))) {
this.log.info(
`The application '${appId}' was reported running, ` +
`although all process ids belonging to it have been changed: ` +
`(${JSON.stringify(pids)} -> ${JSON.stringify(currentPids)}). ` +
`Assuming the termination was successful.`
);
return true;
}
return false;
}, {
waitMs: timeout,
intervalMs: 100,
});
} catch (e) {
this.log.errorAndThrow(`'${appId}' is still running after ${timeout}ms timeout`);
if (!_.isEmpty(currentPids) && !_.isEmpty(_.difference(pids, currentPids))) {
this.log.warn(
`Some of processes belonging to the '${appId}' applcation are still running ` +
`after ${timeout}ms (${JSON.stringify(pids)} -> ${JSON.stringify(currentPids)})`
);
}
throw this.log.errorWithException(`'${appId}' is still running after ${timeout}ms timeout`);
}
this.log.info(`'${appId}' has been successfully terminated`);
return true;
Expand Down

0 comments on commit 970085a

Please sign in to comment.