Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Added options to save ADB output. (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunikkk authored Oct 19, 2017
1 parent b7b719e commit 192ff30
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ext.versions = [

rxJava : '1.2.9',
jCommander : '1.71',
commander : '0.1.1',
commander : '0.1.2',

junit : '4.12',
junitPlatform: '1.0.0-M4',
Expand Down
43 changes: 31 additions & 12 deletions swarmer/src/main/kotlin/com/gojuno/swarmer/Emulators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ fun startEmulators(
applyConfig: (args: Commands.Start) -> Observable<Unit> = ::applyConfig,
emulator: (args: Commands.Start) -> String = ::emulatorBinary,
findAvailablePortsForNewEmulator: () -> Observable<Pair<Int, Int>> = ::findAvailablePortsForNewEmulator,
startEmulatorProcess: (List<String>, File?) -> Observable<Notification> = ::startEmulatorProcess,
startEmulatorProcess: (List<String>, Commands.Start) -> Observable<Notification> = ::startEmulatorProcess,
waitForEmulatorToStart: (Commands.Start, () -> Observable<Set<AdbDevice>>, Observable<Notification>, Pair<Int, Int>) -> Observable<Emulator> = ::waitForEmulatorToStart,
waitForEmulatorToFinishBoot: (Emulator) -> Observable<Emulator> = ::waitForEmulatorToFinishBoot
waitForEmulatorToFinishBoot: (Emulator, Commands.Start) -> Observable<Emulator> = ::waitForEmulatorToFinishBoot
) {
val startTime = System.nanoTime()

Expand Down Expand Up @@ -75,20 +75,25 @@ fun startEmulators(
log("Swarmer: - \"My job is done here, took ${(System.nanoTime() - startTime).nanosAsSeconds()} seconds, startedEmulators: $startedEmulators, bye bye.\"")
}

private fun startEmulatorProcess(args: List<String>, redirectOutputTo: File?) =
process(args, null, redirectOutputTo)
private fun startEmulatorProcess(args: List<String>, command: Commands.Start) =
process(
commandAndArgs = args,
timeout = null,
redirectOutputTo = outputFileForEmulator(command),
keepOutputOnExit = command.keepOutputOnExit
)

private fun startEmulator(
args: Commands.Start,
createAvd: (args: Commands.Start) -> Observable<Unit>,
applyConfig: (args: Commands.Start) -> Observable<Unit>,
availablePortsSemaphore: Semaphore,
findAvailablePortsForNewEmulator: () -> Observable<Pair<Int, Int>>,
startEmulatorProcess: (List<String>, File?) -> Observable<Notification>,
startEmulatorProcess: (List<String>, Commands.Start) -> Observable<Notification>,
waitForEmulatorToStart: (Commands.Start, () -> Observable<Set<AdbDevice>>, Observable<Notification>, Pair<Int, Int>) -> Observable<Emulator>,
connectedAdbDevices: () -> Observable<Set<AdbDevice>> = ::connectedAdbDevices,
emulator: (Commands.Start) -> String,
waitForEmulatorToFinishBoot: (Emulator) -> Observable<Emulator>
waitForEmulatorToFinishBoot: (Emulator, Commands.Start) -> Observable<Emulator>
): Observable<Emulator> =
createAvd(args)
.flatMap { applyConfig(args) }
Expand All @@ -99,7 +104,7 @@ private fun startEmulator(
startEmulatorProcess(
// Unix only, PR welcome.
listOf(sh, "-c", "${emulator(args)} ${if (args.verbose) "-verbose" else ""} -avd ${args.emulatorName} -ports ${ports.first},${ports.second} ${args.emulatorStartOptions.joinToString(" ")} &"),
outputFileForEmulator(args)
args
).let { process ->
waitForEmulatorToStart(args, connectedAdbDevices, process, ports)
}
Expand All @@ -121,7 +126,9 @@ private fun startEmulator(
}
}
}
.flatMap(waitForEmulatorToFinishBoot)
.flatMap { emulator ->
waitForEmulatorToFinishBoot(emulator, args)
}
.timeout(args.emulatorStartTimeoutSeconds, SECONDS)
.doOnError {
when (it) {
Expand Down Expand Up @@ -170,7 +177,9 @@ private fun createAvd(args: Commands.Start): Observable<Unit> {
"--package", args.pakage,
"--abi", args.androidAbi
),
timeout = 60 to SECONDS
timeout = 60 to SECONDS,
redirectOutputTo = outputDirectory(args),
keepOutputOnExit = args.keepOutputOnExit
).share()

val iDontWishToCreateCustomHardwareProfile = Observable
Expand Down Expand Up @@ -267,7 +276,10 @@ private fun waitForEmulatorToStart(
.doOnError { log("Error during start of the emulator ${args.emulatorName}, error = $it") }
}

private fun waitForEmulatorToFinishBoot(targetEmulator: Emulator): Observable<Emulator> {
private fun waitForEmulatorToFinishBoot(
targetEmulator: Emulator,
args: Commands.Start
): Observable<Emulator> {
val startTime = AtomicLong()

return Observable
Expand All @@ -286,7 +298,9 @@ private fun waitForEmulatorToFinishBoot(targetEmulator: Emulator): Observable<Em
"shell",
"getprop", "init.svc.bootanim"
),
timeout = 10 to SECONDS
timeout = 10 to SECONDS,
redirectOutputTo = outputDirectory(args),
keepOutputOnExit = args.keepOutputOnExit
)
.filter { it is Notification.Exit }
.cast(Notification.Exit::class.java)
Expand All @@ -309,9 +323,14 @@ private fun waitForEmulatorToFinishBoot(targetEmulator: Emulator): Observable<Em
private fun Long.nanosAsSeconds(): Float = NANOSECONDS.toMillis(this) / 1000f

private fun outputFileForEmulator(args: Commands.Start) =
File(args.redirectOutputTo ?: "", "${args.emulatorName}.output").apply {
File(outputDirectory(args), "${args.emulatorName}.output").apply {
if (!args.keepOutputOnExit) deleteOnExit()
}

private fun outputDirectory(args: Commands.Start) =
args.redirectOutputTo?.run {
File(this).apply { mkdirs() }
}

private fun connectedEmulators(): Single<Set<AdbDevice>> =
connectedAdbDevices().take(1).toSingle().map { it.filter { it.isEmulator }.toSet() }
8 changes: 4 additions & 4 deletions swarmer/src/test/kotlin/com/gojuno/swarmer/EmulatorsSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class EmulatorsSpec : Spek({
}

val startEmulatorsProcess by memoized {
mock<(List<String>, File?) -> Observable<Notification>>().apply {
mock<(List<String>, Commands.Start) -> Observable<Notification>>().apply {
whenever(invoke(any(), any())).thenReturn(Observable.just(
Notification.Start(process, outputFile),
Notification.Exit(outputFile)
Expand All @@ -144,8 +144,8 @@ class EmulatorsSpec : Spek({
val waitForEmulatorToFinishBoot by memoized {
val emulatorCaptor = argumentCaptor<Emulator>()

mock<(Emulator) -> Observable<Emulator>>().apply {
whenever(invoke(emulatorCaptor.capture())).thenAnswer {
mock<(Emulator, Commands.Start) -> Observable<Emulator>>().apply {
whenever(invoke(emulatorCaptor.capture(), any())).thenAnswer {
Observable.just(emulatorCaptor.firstValue)
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ class EmulatorsSpec : Spek({
"/bin/sh", "-c",
"${emulator(command)} ${if (command.verbose) "-verbose" else ""} -avd ${command.emulatorName} -ports ${EMULATOR_PORTS.first},${EMULATOR_PORTS.second} ${command.emulatorStartOptions.joinToString(" ")} &"
),
File(command.redirectOutputTo ?: "", "${command.emulatorName}.output")
command
)
}
}
Expand Down

0 comments on commit 192ff30

Please sign in to comment.