Skip to content

Commit

Permalink
Revert "[JENKINS-11952] Remove redundant (dis)connect calls while wai…
Browse files Browse the repository at this point in the history
…ting for startup."

This reverts commit f2fcd2f.

This is required as, per the previous commit, we're using the "localhost" style
of emulator connection string once again.
  • Loading branch information
orrc committed Feb 13, 2015
1 parent 7949052 commit 5bdae99
Showing 1 changed file with 39 additions and 1 deletion.
Expand Up @@ -419,6 +419,9 @@ private Environment doSetUp(final AbstractBuild<?, ?> build, final Launcher laun
// The delay here is a function of boot time, i.e. relative to the slowness of the host
Thread.sleep(bootDuration / 4);

// Make sure we're still connected
connectEmulator(emu);

log(logger, Messages.UNLOCKING_SCREEN());
final long adbTimeout = bootTimeout / 16;
final String keyEventArgs = String.format("-s %s shell input keyevent %%d", emu.serial());
Expand All @@ -442,6 +445,9 @@ private Environment doSetUp(final AbstractBuild<?, ?> build, final Launcher laun
log(logger, Messages.WAITING_INITIAL_SNAPSHOT());
Thread.sleep((long) (bootDuration * 0.8));

// Make sure we're still connected
connectEmulator(emu);

// Clear main log before creating snapshot
final String clearArgs = String.format("-s %s logcat -c", emu.serial());
ArgumentListBuilder adbCmd = emu.getToolCommand(Tool.ADB, clearArgs);
Expand Down Expand Up @@ -473,6 +479,9 @@ private Environment doSetUp(final AbstractBuild<?, ?> build, final Launcher laun
}
}

// Make sure we're still connected
connectEmulator(emu);

// Done!
final long bootCompleteTime = System.currentTimeMillis();
log(logger, Messages.EMULATOR_IS_READY((bootCompleteTime - bootTime) / 1000));
Expand Down Expand Up @@ -517,6 +526,20 @@ public boolean tearDown(AbstractBuild build, BuildListener listener)
};
}

private static void connectEmulator(AndroidEmulatorContext emu)
throws IOException, InterruptedException {
ArgumentListBuilder adbConnectCmd = emu.getToolCommand(Tool.ADB, "connect " + emu.serial());
emu.getProcStarter(adbConnectCmd).start().joinWithTimeout(5L, TimeUnit.SECONDS, emu.launcher().getListener());
}

private static void disconnectEmulator(AndroidEmulatorContext emu)
throws IOException, InterruptedException {
final String args = "disconnect "+ emu.serial();
ArgumentListBuilder adbDisconnectCmd = emu.getToolCommand(Tool.ADB, args);
emu.getProcStarter(adbDisconnectCmd).start().joinWithTimeout(5L, TimeUnit.SECONDS, emu.launcher().getListener());
}


/** Helper method for writing to the build log in a consistent manner. */
public synchronized static void log(final PrintStream logger, final String message) {
log(logger, message, false);
Expand Down Expand Up @@ -564,6 +587,9 @@ private void cleanUp(EmulatorConfig emulatorConfig, AndroidEmulatorContext emu,
// Launcher.kill(EnvVars) does not appear to help either.
// This is (a) inconsistent; (b) very annoying.

// Disconnect emulator from adb
disconnectEmulator(emu);

// Stop emulator process
log(emu.logger(), Messages.STOPPING_EMULATOR());
boolean killed = emu.sendCommand("kill");
Expand Down Expand Up @@ -702,6 +728,7 @@ private boolean waitForBootCompletion(final boolean ignoreProcess,

try {
final long adbTimeout = timeout / 8;
int iterations = 0;
while (System.currentTimeMillis() < start + timeout && (ignoreProcess || emu.process().isAlive())) {
ByteArrayOutputStream stream = new ByteArrayOutputStream(16);

Expand All @@ -716,7 +743,18 @@ private boolean waitForBootCompletion(final boolean ignoreProcess,
}
}

// "getprop" failed, so sleep and try again later
// Otherwise continue...

/* Ensure the emulator is connected to adb, in case it had crashed.
* We also disconnect it every 3 tries, in case it's stuck in an offline state.
*/
if (++iterations % 3 == 0) {
try {
disconnectEmulator(emu);
} catch (Exception ignore) {}
}
connectEmulator(emu);

Thread.sleep(sleep);
}
} catch (InterruptedException ex) {
Expand Down

0 comments on commit 5bdae99

Please sign in to comment.