Skip to content

Commit

Permalink
Auto-reconnect to ADB while checking for boot completion, in case ADB…
Browse files Browse the repository at this point in the history
… crashed.

As suggested in JENKINS-7693, this prevents the build from failing due to an ADB crash.
Such ADB crashes can sometimes be triggered by other emulators starting up in parallel.
  • Loading branch information
orrc committed Apr 6, 2011
1 parent fb9e2bd commit 7198923
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/hudson/plugins/android_emulator/AndroidEmulator.java
Expand Up @@ -343,7 +343,8 @@ private Environment doSetUp(final AbstractBuild<?, ?> build, final Launcher laun
if (!emulatorAlreadyExists || emuConfig.shouldWipeData() || snapshotState == SnapshotState.INITIALISE) {
bootTimeout *= 4;
}
boolean bootSucceeded = waitForBootCompletion(logger, launcher, androidSdk, emulatorProcess, serial, bootTimeout);
boolean bootSucceeded = waitForBootCompletion(logger, launcher, androidSdk, emulatorProcess,
serial, adbConnectArgs, bootTimeout);
if (!bootSucceeded) {
if ((System.currentTimeMillis() - bootTime) < bootTimeout) {
log(logger, Messages.EMULATOR_STOPPED_DURING_BOOT());
Expand Down Expand Up @@ -612,19 +613,21 @@ private boolean waitForSocket(Launcher launcher, int port, int timeout) {
* @return <code>true</code> if the emulator has booted, <code>false</code> if we timed-out.
*/
private boolean waitForBootCompletion(final PrintStream logger, final Launcher launcher,
final AndroidSdk androidSdk, final Proc emulatorProcess, final String serial, final int timeout) {
final AndroidSdk androidSdk, final Proc emulatorProcess, final String serial,
final String adbConnectArgs, final int timeout) {
long start = System.currentTimeMillis();
int sleep = timeout / (int) Math.sqrt(timeout / 1000);

final String args = String.format("-s %s shell getprop dev.bootcomplete", serial);
ArgumentListBuilder cmd = Utils.getToolCommand(androidSdk, launcher.isUnix(), Tool.ADB, args);
ArgumentListBuilder bootCheckCmd = Utils.getToolCommand(androidSdk, launcher.isUnix(), Tool.ADB, args);
ArgumentListBuilder connectCmd = Utils.getToolCommand(androidSdk, launcher.isUnix(), Tool.ADB, adbConnectArgs);

try {
while (System.currentTimeMillis() < start + timeout && emulatorProcess.isAlive()) {
ByteArrayOutputStream stream = new ByteArrayOutputStream(4);

// Run "getprop"
launcher.launch().cmds(cmd).stdout(stream).start().join();
launcher.launch().cmds(bootCheckCmd).stdout(stream).start().join();

// Check output
String result = stream.toString().trim();
Expand All @@ -634,6 +637,9 @@ private boolean waitForBootCompletion(final PrintStream logger, final Launcher l

// Otherwise continue...
Thread.sleep(sleep);

// Ensure the emulator is connected to adb, in case it had crashed
launcher.launch().cmds(connectCmd).start().join();
}
} catch (InterruptedException ex) {
log(logger, Messages.INTERRUPTED_DURING_BOOT_COMPLETION());
Expand Down

0 comments on commit 7198923

Please sign in to comment.