Skip to content

Commit b690114

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

File tree

2 files changed

+92
-27
lines changed

2 files changed

+92
-27
lines changed

lib/emulator-manager.js

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use strict";
2-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function (o, m, k, k2) {
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);
9-
}) : (function(o, m, k, k2) {
9+
}) : (function (o, m, k, k2) {
1010
if (k2 === undefined) k2 = k;
1111
o[k2] = m[k];
1212
}));
13-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
13+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function (o, v) {
1414
Object.defineProperty(o, "default", { enumerable: true, value: v });
15-
}) : function(o, v) {
15+
}) : function (o, v) {
1616
o["default"] = v;
1717
});
1818
var __importStar = (this && this.__importStar) || function (mod) {
@@ -72,6 +72,8 @@ function launchEmulator(systemImageApiLevel, target, arch, profile, cores, ramSi
7272
}
7373
// start emulator
7474
console.log('Starting emulator.');
75+
console.log('Starting tristans emulator.');
76+
7577
// Add a small delay to ensure emulator process starts properly
7678
yield exec.exec(`sh -c \\"${process.env.ANDROID_HOME}/emulator/emulator -port ${port} -avd "${avdName}" ${emulatorOptions} &"`, [], {
7779
listeners: {
@@ -82,8 +84,6 @@ function launchEmulator(systemImageApiLevel, target, arch, profile, cores, ramSi
8284
},
8385
},
8486
});
85-
// Wait a few seconds for emulator process to initialize before polling
86-
yield delay(5000);
8787
// wait for emulator to complete booting
8888
yield waitForDevice(port, emulatorBootTimeout);
8989
yield adb(port, `shell input keyevent 82`);
@@ -134,11 +134,46 @@ function adb(port, command) {
134134
*/
135135
function waitForDevice(port, emulatorBootTimeout) {
136136
return __awaiter(this, void 0, void 0, function* () {
137-
let booted = false;
138-
let attempts = 0;
137+
const startTime = Date.now();
139138
const retryInterval = 2; // retry every 2 seconds
140-
const maxAttempts = emulatorBootTimeout / 2;
139+
// Step 1: Poll until device appears in ADB
140+
console.log('Waiting for emulator device to connect...');
141+
let deviceConnected = false;
142+
while (!deviceConnected) {
143+
const elapsedSeconds = (Date.now() - startTime) / 1000;
144+
if (elapsedSeconds > emulatorBootTimeout) {
145+
throw new Error('Timeout waiting for emulator device to connect.');
146+
}
147+
try {
148+
// Try to get device state - will fail if device doesn't exist
149+
let state = '';
150+
yield exec.exec(`adb -s emulator-${port} get-state`, [], {
151+
listeners: {
152+
stdout: (data) => {
153+
state += data.toString();
154+
},
155+
},
156+
});
157+
if (state.trim() === 'device') {
158+
deviceConnected = true;
159+
}
160+
}
161+
catch (error) {
162+
// Device not found yet, keep waiting
163+
console.warn(error instanceof Error ? error.message : error);
164+
}
165+
if (!deviceConnected) {
166+
yield delay(retryInterval * 1000);
167+
}
168+
}
169+
console.log('Device connected. Waiting for boot to complete...');
170+
// Step 2: Poll for boot completion
171+
let booted = false;
141172
while (!booted) {
173+
const elapsedSeconds = (Date.now() - startTime) / 1000;
174+
if (elapsedSeconds > emulatorBootTimeout) {
175+
throw new Error('Timeout waiting for emulator to boot.');
176+
}
142177
try {
143178
let result = '';
144179
yield exec.exec(`adb -s emulator-${port} shell getprop sys.boot_completed`, [], {
@@ -151,19 +186,14 @@ function waitForDevice(port, emulatorBootTimeout) {
151186
if (result.trim() === '1') {
152187
console.log('Emulator booted.');
153188
booted = true;
154-
break;
155189
}
156190
}
157191
catch (error) {
158192
console.warn(error instanceof Error ? error.message : error);
159193
}
160-
if (attempts < maxAttempts) {
194+
if (!booted) {
161195
yield delay(retryInterval * 1000);
162196
}
163-
else {
164-
throw new Error(`Timeout waiting for emulator to boot.`);
165-
}
166-
attempts++;
167197
}
168198
});
169199
}

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)