From 192ff3053ae2f76b61d16af3d79e7469e44e4166 Mon Sep 17 00:00:00 2001 From: Dmitry Yunitsky Date: Fri, 20 Oct 2017 00:16:42 +0300 Subject: [PATCH] Added options to save ADB output. (#34) --- dependencies.gradle | 2 +- .../kotlin/com/gojuno/swarmer/Emulators.kt | 43 +++++++++++++------ .../com/gojuno/swarmer/EmulatorsSpec.kt | 8 ++-- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 1e9d0af..e78bfd2 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -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', diff --git a/swarmer/src/main/kotlin/com/gojuno/swarmer/Emulators.kt b/swarmer/src/main/kotlin/com/gojuno/swarmer/Emulators.kt index b9511c1..4ca96b4 100644 --- a/swarmer/src/main/kotlin/com/gojuno/swarmer/Emulators.kt +++ b/swarmer/src/main/kotlin/com/gojuno/swarmer/Emulators.kt @@ -35,9 +35,9 @@ fun startEmulators( applyConfig: (args: Commands.Start) -> Observable = ::applyConfig, emulator: (args: Commands.Start) -> String = ::emulatorBinary, findAvailablePortsForNewEmulator: () -> Observable> = ::findAvailablePortsForNewEmulator, - startEmulatorProcess: (List, File?) -> Observable = ::startEmulatorProcess, + startEmulatorProcess: (List, Commands.Start) -> Observable = ::startEmulatorProcess, waitForEmulatorToStart: (Commands.Start, () -> Observable>, Observable, Pair) -> Observable = ::waitForEmulatorToStart, - waitForEmulatorToFinishBoot: (Emulator) -> Observable = ::waitForEmulatorToFinishBoot + waitForEmulatorToFinishBoot: (Emulator, Commands.Start) -> Observable = ::waitForEmulatorToFinishBoot ) { val startTime = System.nanoTime() @@ -75,8 +75,13 @@ fun startEmulators( log("Swarmer: - \"My job is done here, took ${(System.nanoTime() - startTime).nanosAsSeconds()} seconds, startedEmulators: $startedEmulators, bye bye.\"") } -private fun startEmulatorProcess(args: List, redirectOutputTo: File?) = - process(args, null, redirectOutputTo) +private fun startEmulatorProcess(args: List, command: Commands.Start) = + process( + commandAndArgs = args, + timeout = null, + redirectOutputTo = outputFileForEmulator(command), + keepOutputOnExit = command.keepOutputOnExit + ) private fun startEmulator( args: Commands.Start, @@ -84,11 +89,11 @@ private fun startEmulator( applyConfig: (args: Commands.Start) -> Observable, availablePortsSemaphore: Semaphore, findAvailablePortsForNewEmulator: () -> Observable>, - startEmulatorProcess: (List, File?) -> Observable, + startEmulatorProcess: (List, Commands.Start) -> Observable, waitForEmulatorToStart: (Commands.Start, () -> Observable>, Observable, Pair) -> Observable, connectedAdbDevices: () -> Observable> = ::connectedAdbDevices, emulator: (Commands.Start) -> String, - waitForEmulatorToFinishBoot: (Emulator) -> Observable + waitForEmulatorToFinishBoot: (Emulator, Commands.Start) -> Observable ): Observable = createAvd(args) .flatMap { applyConfig(args) } @@ -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) } @@ -121,7 +126,9 @@ private fun startEmulator( } } } - .flatMap(waitForEmulatorToFinishBoot) + .flatMap { emulator -> + waitForEmulatorToFinishBoot(emulator, args) + } .timeout(args.emulatorStartTimeoutSeconds, SECONDS) .doOnError { when (it) { @@ -170,7 +177,9 @@ private fun createAvd(args: Commands.Start): Observable { "--package", args.pakage, "--abi", args.androidAbi ), - timeout = 60 to SECONDS + timeout = 60 to SECONDS, + redirectOutputTo = outputDirectory(args), + keepOutputOnExit = args.keepOutputOnExit ).share() val iDontWishToCreateCustomHardwareProfile = Observable @@ -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 { +private fun waitForEmulatorToFinishBoot( + targetEmulator: Emulator, + args: Commands.Start +): Observable { val startTime = AtomicLong() return Observable @@ -286,7 +298,9 @@ private fun waitForEmulatorToFinishBoot(targetEmulator: Emulator): Observable> = connectedAdbDevices().take(1).toSingle().map { it.filter { it.isEmulator }.toSet() } diff --git a/swarmer/src/test/kotlin/com/gojuno/swarmer/EmulatorsSpec.kt b/swarmer/src/test/kotlin/com/gojuno/swarmer/EmulatorsSpec.kt index 5290016..4f42a96 100644 --- a/swarmer/src/test/kotlin/com/gojuno/swarmer/EmulatorsSpec.kt +++ b/swarmer/src/test/kotlin/com/gojuno/swarmer/EmulatorsSpec.kt @@ -121,7 +121,7 @@ class EmulatorsSpec : Spek({ } val startEmulatorsProcess by memoized { - mock<(List, File?) -> Observable>().apply { + mock<(List, Commands.Start) -> Observable>().apply { whenever(invoke(any(), any())).thenReturn(Observable.just( Notification.Start(process, outputFile), Notification.Exit(outputFile) @@ -144,8 +144,8 @@ class EmulatorsSpec : Spek({ val waitForEmulatorToFinishBoot by memoized { val emulatorCaptor = argumentCaptor() - mock<(Emulator) -> Observable>().apply { - whenever(invoke(emulatorCaptor.capture())).thenAnswer { + mock<(Emulator, Commands.Start) -> Observable>().apply { + whenever(invoke(emulatorCaptor.capture(), any())).thenAnswer { Observable.just(emulatorCaptor.firstValue) } } @@ -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 ) } }