-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1795600
commit ca20f78
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...routines/coroutines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/StreamsStrings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.withContext | ||
import java.io.InputStream | ||
|
||
class StreamsStrings { | ||
suspend fun executeCommand(commandArgs: List<String>): ProcessResult = withContext( | ||
Dispatchers.IO | ||
) { | ||
runCatching { | ||
val process = ProcessBuilder(commandArgs).start() | ||
val outputStream = async { | ||
println("Context for output stream -> $coroutineContext -> Thread -> ${Thread.currentThread()}") | ||
readStream(process.inputStream) } | ||
val errorStream = async { | ||
println("Context for error stream -> $coroutineContext -> Thread -> ${Thread.currentThread()}") | ||
readStream(process.errorStream) | ||
} | ||
println("Context for exit code -> $coroutineContext -> Thread -> ${Thread.currentThread()}") | ||
val exitCode = process.waitFor() | ||
ProcessResult( | ||
exitCode = exitCode, | ||
message = outputStream.await(), | ||
errorMessage = errorStream.await() | ||
) | ||
}.onFailure{ | ||
ProcessResult( | ||
exitCode = -1, | ||
message = "", | ||
errorMessage = it.localizedMessage | ||
) | ||
}.getOrThrow() | ||
} | ||
|
||
private fun readStream(inputStream: InputStream) = | ||
inputStream.bufferedReader().use { reader -> reader.readText() } | ||
|
||
data class ProcessResult(val exitCode: Int, val message: String, val errorMessage: String) | ||
} |
19 changes: 19 additions & 0 deletions
19
...ines/coroutines-crums-group-1/src/test/kotlin/org/jesperancinha/ktd/StreamsStringsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import io.kotest.common.runBlocking | ||
import io.kotest.matchers.string.shouldContain | ||
import kotlin.test.Test | ||
|
||
class StreamsStringsTest { | ||
|
||
private val streamsStrings by lazy { StreamsStrings() } | ||
|
||
@Test | ||
fun `should run command correctly`(): Unit = runBlocking { | ||
|
||
val executeCommand = streamsStrings.executeCommand(listOf("java", "-version")) | ||
println(executeCommand) | ||
executeCommand.errorMessage shouldContain "JDK" | ||
|
||
} | ||
} |