Skip to content

Commit

Permalink
One more example
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 16, 2024
1 parent ca20f78 commit fcd8548
Showing 1 changed file with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ class AsyncMathTest {
}

@Test
fun `should find coordinates asynchronously because of non-blocking with explicit Dispatchers_IO`(): Unit = runBlocking {
withContext(Dispatchers.Default) {
val x = async {
findX()
}
val y = async {
findY()
}
val z = async {
findZ()
fun `should find coordinates asynchronously because of non-blocking with explicit Dispatchers_IO`(): Unit =
runBlocking {
withContext(Dispatchers.Default) {
val x = async {
findX()
}
val y = async {
findY()
}
val z = async {
findZ()
}
showCoordinates(x, y, z)
println(coroutineContext)
}
showCoordinates(x, y, z)
println(coroutineContext)
}
}

@Test
fun `should find coordinates asynchronously because of non-blocking with coroutineScope`(): Unit = runBlocking {
Expand All @@ -59,6 +60,22 @@ class AsyncMathTest {

}

@Test
fun `should find coordinates asynchronously respecting structured concurrency`(): Unit =
runBlocking(Dispatchers.Default) {
val x = async {
findX()
}
val y = async {
findY()
}
val z = async {
findZ()
}
showCoordinates(x, y, z)
println(coroutineContext)
}

private fun findZ(): Long {
println("z")
return Random.nextLong(1000)
Expand Down

0 comments on commit fcd8548

Please sign in to comment.