Skip to content

Commit

Permalink
fix: Update instrumentation process handling logic (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jul 3, 2024
1 parent fcb6790 commit 75da896
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
47 changes: 35 additions & 12 deletions lib/uiautomator2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TEST_APK_PATH as testApkPath,
version as serverVersion
} from 'appium-uiautomator2-server';
import { util, logger, timing } from 'appium/support';
import { util, timing } from 'appium/support';
import B from 'bluebird';
import axios from 'axios';

Expand All @@ -17,7 +17,6 @@ const SERVICES_LAUNCH_TIMEOUT = 30000;
const SERVER_PACKAGE_ID = 'io.appium.uiautomator2.server';
const SERVER_TEST_PACKAGE_ID = `${SERVER_PACKAGE_ID}.test`;
const INSTRUMENTATION_TARGET = `${SERVER_TEST_PACKAGE_ID}/androidx.test.runner.AndroidJUnitRunner`;
const instrumentationLogger = logger.getLogger('Instrumentation');

class UIA2Proxy extends JWProxy {
/** @type {boolean} */
Expand Down Expand Up @@ -50,6 +49,9 @@ class UiAutomator2Server {
/** @type {boolean|undefined} */
disableSuppressAccessibilityService;

/** @type {import('teen_process').SubProcess|null} */
instrumentationProcess;

constructor (log, opts = {}) {
for (let req of REQD_PARAMS) {
if (!opts || !util.hasValue(opts[req])) {
Expand All @@ -72,6 +74,7 @@ class UiAutomator2Server {
this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);
this.proxyCommand = this.jwproxy.command.bind(this.jwproxy);
this.jwproxy.didInstrumentationExit = false;
this.instrumentationProcess = null;
}

/**
Expand Down Expand Up @@ -255,6 +258,9 @@ class UiAutomator2Server {
while (retries < maxRetries) {
this.log.info(`Waiting up to ${timeout}ms for UiAutomator2 to be online...`);
this.jwproxy.didInstrumentationExit = false;
try {
await this.stopInstrumentationProcess();
} catch (ign) {}
await this.startInstrumentationProcess();
if (!this.jwproxy.didInstrumentationExit) {
try {
Expand Down Expand Up @@ -312,18 +318,30 @@ class UiAutomator2Server {
// Disable Google analytics to prevent possible fatal exception
cmd.push('-e', 'disableAnalytics', 'true');
cmd.push(INSTRUMENTATION_TARGET);
const instrumentationProcess = this.adb.createSubProcess(['shell', ...cmd]);
instrumentationProcess.on('output', (stdout, stderr) => {
const output = _.trim(stdout || stderr);
if (output) {
instrumentationLogger.debug(output);
}
});
instrumentationProcess.on('exit', (code) => {
instrumentationLogger.debug(`The process has exited with code ${code}`);
this.instrumentationProcess = this.adb.createSubProcess(['shell', ...cmd]);
for (const streamName of ['stderr', 'stdout']) {
this.instrumentationProcess.on(`line-${streamName}`, (line) => this.log.debug(`[Instrumentation] ${line}`));
}
this.instrumentationProcess.once('exit', (code, signal) => {
this.log.debug(`[Instrumentation] The process has exited with code ${code}, signal ${signal}`);
this.jwproxy.didInstrumentationExit = true;
});
await instrumentationProcess.start(0);
await this.instrumentationProcess.start(0);
}

async stopInstrumentationProcess () {
if (!this.instrumentationProcess) {
return;
}

try {
if (this.instrumentationProcess.isRunning) {
await this.instrumentationProcess.stop();
}
} finally {
this.instrumentationProcess.removeAllListeners();
this.instrumentationProcess = null;
}
}

async deleteSession () {
Expand All @@ -336,6 +354,11 @@ class UiAutomator2Server {
this.log.warn(`Did not get confirmation UiAutomator2 deleteSession worked; ` +
`Error was: ${err}`);
}
try {
await this.stopInstrumentationProcess();
} catch (err) {
this.log.warn(`Could not stop the instrumentation process. Original error: ${err.message}`);
}
}

async cleanupAutomationLeftovers (strictCleanup = false) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"singleQuote": true
},
"dependencies": {
"appium-adb": "^12.2.0",
"appium-adb": "^12.4.7",
"appium-android-driver": "^9.7.0",
"appium-chromedriver": "^5.6.28",
"appium-uiautomator2-server": "^7.0.14",
Expand All @@ -68,7 +68,7 @@
"lodash": "^4.17.4",
"portscanner": "^2.2.0",
"source-map-support": "^0.x",
"teen_process": "^2.0.0",
"teen_process": "^2.2.0",
"type-fest": "^4.4.0"
},
"devDependencies": {
Expand Down

0 comments on commit 75da896

Please sign in to comment.