Skip to content

Commit 8c4a5f9

Browse files
committed
CU-868gk2m09 fix boot polling for emulator
1 parent c869de9 commit 8c4a5f9

File tree

2 files changed

+86
-23
lines changed

2 files changed

+86
-23
lines changed

lib/emulator-manager.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
33
if (k2 === undefined) k2 = k;
44
var desc = Object.getOwnPropertyDescriptor(m, k);
55
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6-
desc = { enumerable: true, get: function() { return m[k]; } };
6+
desc = { enumerable: true, get: function() { return m[k]; } };
77
}
88
Object.defineProperty(o, k2, desc);
99
}) : (function(o, m, k, k2) {
@@ -82,8 +82,6 @@ function launchEmulator(systemImageApiLevel, target, arch, profile, cores, ramSi
8282
},
8383
},
8484
});
85-
// Wait a few seconds for emulator process to initialize before polling
86-
yield delay(5000);
8785
// wait for emulator to complete booting
8886
yield waitForDevice(port, emulatorBootTimeout);
8987
yield adb(port, `shell input keyevent 82`);
@@ -134,11 +132,46 @@ function adb(port, command) {
134132
*/
135133
function waitForDevice(port, emulatorBootTimeout) {
136134
return __awaiter(this, void 0, void 0, function* () {
137-
let booted = false;
138-
let attempts = 0;
135+
const startTime = Date.now();
139136
const retryInterval = 2; // retry every 2 seconds
140-
const maxAttempts = emulatorBootTimeout / 2;
137+
// Step 1: Poll until device appears in ADB
138+
console.log('Waiting for emulator device to connect...');
139+
let deviceConnected = false;
140+
while (!deviceConnected) {
141+
const elapsedSeconds = (Date.now() - startTime) / 1000;
142+
if (elapsedSeconds > emulatorBootTimeout) {
143+
throw new Error('Timeout waiting for emulator device to connect.');
144+
}
145+
try {
146+
// Try to get device state - will fail if device doesn't exist
147+
let state = '';
148+
yield exec.exec(`adb -s emulator-${port} get-state`, [], {
149+
listeners: {
150+
stdout: (data) => {
151+
state += data.toString();
152+
},
153+
},
154+
});
155+
if (state.trim() === 'device') {
156+
deviceConnected = true;
157+
}
158+
}
159+
catch (error) {
160+
// Device not found yet, keep waiting
161+
console.warn(error instanceof Error ? error.message : error);
162+
}
163+
if (!deviceConnected) {
164+
yield delay(retryInterval * 1000);
165+
}
166+
}
167+
console.log('Device connected. Waiting for boot to complete...');
168+
// Step 2: Poll for boot completion
169+
let booted = false;
141170
while (!booted) {
171+
const elapsedSeconds = (Date.now() - startTime) / 1000;
172+
if (elapsedSeconds > emulatorBootTimeout) {
173+
throw new Error('Timeout waiting for emulator to boot.');
174+
}
142175
try {
143176
let result = '';
144177
yield exec.exec(`adb -s emulator-${port} shell getprop sys.boot_completed`, [], {
@@ -151,19 +184,14 @@ function waitForDevice(port, emulatorBootTimeout) {
151184
if (result.trim() === '1') {
152185
console.log('Emulator booted.');
153186
booted = true;
154-
break;
155187
}
156188
}
157189
catch (error) {
158190
console.warn(error instanceof Error ? error.message : error);
159191
}
160-
if (attempts < maxAttempts) {
192+
if (!booted) {
161193
yield delay(retryInterval * 1000);
162194
}
163-
else {
164-
throw new Error(`Timeout waiting for emulator to boot.`);
165-
}
166-
attempts++;
167195
}
168196
});
169197
}

src/emulator-manager.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ export async function launchEmulator(
7777
},
7878
});
7979

80-
// Wait a few seconds for emulator process to initialize before polling
81-
await delay(5000);
82-
8380
// wait for emulator to complete booting
8481
await waitForDevice(port, emulatorBootTimeout);
8582
await adb(port, `shell input keyevent 82`);
@@ -123,11 +120,53 @@ async function adb(port: number, command: string): Promise<number> {
123120
* Wait for emulator to boot.
124121
*/
125122
async function waitForDevice(port: number, emulatorBootTimeout: number): Promise<void> {
126-
let booted = false;
127-
let attempts = 0;
123+
const startTime = Date.now();
128124
const retryInterval = 2; // retry every 2 seconds
129-
const maxAttempts = emulatorBootTimeout / 2;
125+
126+
// Step 1: Poll until device appears in ADB
127+
console.log('Waiting for emulator device to connect...');
128+
let deviceConnected = false;
129+
130+
while (!deviceConnected) {
131+
const elapsedSeconds = (Date.now() - startTime) / 1000;
132+
if (elapsedSeconds > emulatorBootTimeout) {
133+
throw new Error('Timeout waiting for emulator device to connect.');
134+
}
135+
136+
try {
137+
// Try to get device state - will fail if device doesn't exist
138+
let state = '';
139+
await exec.exec(`adb -s emulator-${port} get-state`, [], {
140+
listeners: {
141+
stdout: (data: Buffer) => {
142+
state += data.toString();
143+
},
144+
},
145+
});
146+
if (state.trim() === 'device') {
147+
deviceConnected = true;
148+
}
149+
} catch (error) {
150+
// Device not found yet, keep waiting
151+
console.warn(error instanceof Error ? error.message : error);
152+
}
153+
154+
if (!deviceConnected) {
155+
await delay(retryInterval * 1000);
156+
}
157+
}
158+
159+
console.log('Device connected. Waiting for boot to complete...');
160+
161+
// Step 2: Poll for boot completion
162+
let booted = false;
163+
130164
while (!booted) {
165+
const elapsedSeconds = (Date.now() - startTime) / 1000;
166+
if (elapsedSeconds > emulatorBootTimeout) {
167+
throw new Error('Timeout waiting for emulator to boot.');
168+
}
169+
131170
try {
132171
let result = '';
133172
await exec.exec(`adb -s emulator-${port} shell getprop sys.boot_completed`, [], {
@@ -140,18 +179,14 @@ async function waitForDevice(port: number, emulatorBootTimeout: number): Promise
140179
if (result.trim() === '1') {
141180
console.log('Emulator booted.');
142181
booted = true;
143-
break;
144182
}
145183
} catch (error) {
146184
console.warn(error instanceof Error ? error.message : error);
147185
}
148186

149-
if (attempts < maxAttempts) {
187+
if (!booted) {
150188
await delay(retryInterval * 1000);
151-
} else {
152-
throw new Error(`Timeout waiting for emulator to boot.`);
153189
}
154-
attempts++;
155190
}
156191
}
157192

0 commit comments

Comments
 (0)