Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/emulator-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ async function launchEmulator(avdName, disableAnimations, disableLinuxHardwareAc
});
// wait for emulator to complete booting
await waitForDevice(port, emulatorBootTimeout);
// boot_completed doesn't guarantee every service is registered yet (#432)
await waitForService(port, 'input');
await adb(port, `shell input keyevent 82`);
if (disableAnimations) {
console.log('Disabling animations.');
Expand Down Expand Up @@ -175,6 +177,33 @@ async function waitForDevice(port, emulatorBootTimeout) {
attempts++;
}
}
/**
* Wait for a ServiceManager-registered service (e.g. 'input') to be available.
*/
async function waitForService(port, serviceName, maxAttempts = 15, retryIntervalSeconds = 1) {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
let result = '';
try {
await exec.exec(`adb -s emulator-${port} shell service check ${serviceName}`, [], {
listeners: {
stdout: (data) => {
result += data.toString();
},
},
});
}
catch (error) {
console.warn(error instanceof Error ? error.message : error);
}
if (result.includes('found') && !result.includes('not found')) {
return;
}
if (attempt === maxAttempts) {
throw new Error(`Timeout waiting for the '${serviceName}' service to become available.`);
}
await delay(retryIntervalSeconds * 1000);
}
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
32 changes: 32 additions & 0 deletions src/emulator-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export async function launchEmulator(

// wait for emulator to complete booting
await waitForDevice(port, emulatorBootTimeout);

// boot_completed doesn't guarantee every service is registered yet (#432)
await waitForService(port, 'input');
await adb(port, `shell input keyevent 82`);

if (disableAnimations) {
Expand Down Expand Up @@ -170,6 +173,35 @@ async function waitForDevice(port: number, emulatorBootTimeout: number): Promise
}
}

/**
* Wait for a ServiceManager-registered service (e.g. 'input') to be available.
*/
async function waitForService(port: number, serviceName: string, maxAttempts = 15, retryIntervalSeconds = 1): Promise<void> {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
let result = '';
try {
await exec.exec(`adb -s emulator-${port} shell service check ${serviceName}`, [], {
listeners: {
stdout: (data: Buffer) => {
result += data.toString();
},
},
});
} catch (error) {
console.warn(error instanceof Error ? error.message : error);
}

if (result.includes('found') && !result.includes('not found')) {
return;
}

if (attempt === maxAttempts) {
throw new Error(`Timeout waiting for the '${serviceName}' service to become available.`);
}
await delay(retryIntervalSeconds * 1000);
}
}

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}