Skip to content

Commit 7233931

Browse files
committed
CB-10510: Add an optional timeout to emu start script
The script used to wait forever for the emulator to boot. If the emulator got stuck, it would never terminate. This timeout is being added to support cordova-medic and the CI.
1 parent d7e111f commit 7233931

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

bin/templates/cordova/lib/emulator.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var ONE_SECOND = 1000; // in milliseconds
4040
var ONE_MINUTE = 60 * ONE_SECOND; // in milliseconds
4141
var INSTALL_COMMAND_TIMEOUT = 5 * ONE_MINUTE; // in milliseconds
4242
var NUM_INSTALL_RETRIES = 3;
43+
var CHECK_BOOTED_INTERVAL = 3 * ONE_SECOND; // in milliseconds
4344
var EXEC_KILL_SIGNAL = 'SIGKILL';
4445

4546
/**
@@ -146,10 +147,12 @@ module.exports.list_targets = function() {
146147
* and returns the started ID of that emulator.
147148
* If no ID is given it will use the first image available,
148149
* if no image is available it will error out (maybe create one?).
150+
* If no boot timeout is given or the value is negative it will wait forever for
151+
* the emulator to boot
149152
*
150153
* Returns a promise.
151154
*/
152-
module.exports.start = function(emulator_ID) {
155+
module.exports.start = function(emulator_ID, boot_timeout) {
153156
var self = this;
154157

155158
return Q().then(function() {
@@ -186,14 +189,20 @@ module.exports.start = function(emulator_ID) {
186189

187190
//wait for emulator to boot up
188191
process.stdout.write('Booting up emulator (this may take a while)...');
189-
return self.wait_for_boot(emulatorId)
190-
.then(function() {
191-
events.emit('log','BOOT COMPLETE');
192-
//unlock screen
193-
return Adb.shell(emulatorId, 'input keyevent 82');
194-
}).then(function() {
195-
//return the new emulator id for the started emulators
196-
return emulatorId;
192+
return self.wait_for_boot(emulatorId, boot_timeout)
193+
.then(function(success) {
194+
if (success) {
195+
events.emit('log','BOOT COMPLETE');
196+
//unlock screen
197+
return Adb.shell(emulatorId, 'input keyevent 82')
198+
.then(function() {
199+
//return the new emulator id for the started emulators
200+
return emulatorId;
201+
});
202+
} else {
203+
// We timed out waiting for the boot to happen
204+
return null;
205+
}
197206
});
198207
});
199208
};
@@ -227,18 +236,25 @@ module.exports.wait_for_emulator = function(uuid) {
227236
};
228237

229238
/*
230-
* Waits for the core android process of the emulator to start
239+
* Waits for the core android process of the emulator to start. Returns a
240+
* promise that resolves to a boolean indicating success. Not specifying a
241+
* time_remaining or passing a negative value will cause it to wait forever
231242
*/
232-
module.exports.wait_for_boot = function(emulator_id) {
243+
module.exports.wait_for_boot = function(emulator_id, time_remaining) {
233244
var self = this;
234245
return Adb.shell(emulator_id, 'ps')
235246
.then(function(output) {
236247
if (output.match(/android\.process\.acore/)) {
237-
return;
248+
return true;
249+
} else if (time_remaining === 0) {
250+
return false;
238251
} else {
239252
process.stdout.write('.');
240-
return Q.delay(3000).then(function() {
241-
return self.wait_for_boot(emulator_id);
253+
254+
// Check at regular intervals
255+
return Q.delay(time_remaining < CHECK_BOOTED_INTERVAL ? time_remaining : CHECK_BOOTED_INTERVAL).then(function() {
256+
var updated_time = time_remaining >= 0 ? Math.max(time_remaining - CHECK_BOOTED_INTERVAL, 0) : time_remaining;
257+
return self.wait_for_boot(emulator_id, updated_time);
242258
});
243259
}
244260
});

0 commit comments

Comments
 (0)