Created
October 24, 2018 16:05
-
-
Save rafaeltoledo/ac2d5a5ed56a7e272c6280041f65266a to your computer and use it in GitHub Desktop.
Desenvolvido no Kotlin Meetup SP em 23/10/2018
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
import junit.framework.Assert.assertTrue | |
import kotlinx.coroutines.* | |
import org.junit.Assert.assertEquals | |
import org.junit.Assert.assertFalse | |
import org.junit.Test | |
class MainTest { | |
@Test | |
fun `Assert launch coroutine works`() { | |
var hello = "" | |
GlobalScope.launch { | |
// launch new coroutine in background and continue | |
delay(1000L) // non-blocking delay for 1 second (default time unit is ms) | |
hello += "World!" // print after delay | |
} | |
hello += "Hello," // main thread continues while coroutine is delayed | |
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive | |
assertEquals("Hello,World!", hello) | |
} | |
@Test | |
fun `Assert launch job is active`() { | |
val job = GlobalScope.launch { | |
// launch new coroutine in background and continue | |
delay(1000L) // non-blocking delay for 1 second (default time unit is ms) | |
} | |
assertTrue(job.isActive) | |
} | |
@Test | |
fun `Assert launch job is cancelled`() { | |
val job = GlobalScope.launch { | |
// launch new coroutine in background and continue | |
delay(1000L) // non-blocking delay for 1 second (default time unit is ms) | |
} | |
job.cancel() | |
assertTrue(job.isCancelled) | |
} | |
@Test | |
fun `Assert coroutines with async await`() { | |
runBlocking { | |
val job1 = GlobalScope.async { | |
return@async "Hello," | |
} | |
val job2 = GlobalScope.async { | |
return@async "World!" | |
} | |
val result1 = job1.await() + job2.await() | |
assertEquals("Hello,World!", result1) | |
} | |
} | |
@Test | |
fun `Assert coroutines with join`() { | |
runBlocking { | |
var hello ="" | |
val job1 = GlobalScope.launch { | |
delay(2000) | |
hello += "Hello," | |
} | |
val job2 = GlobalScope.launch(job1) { | |
delay(2000) | |
hello += "World!" | |
} | |
job2.join() | |
assertEquals("Hello,World!", hello) | |
} | |
} | |
@InternalCoroutinesApi | |
@Test() | |
fun `Assert coroutine has children`() { | |
runBlocking { | |
val job1 = GlobalScope.launch { | |
delay(2000) | |
} | |
val job2 = GlobalScope.launch(job1) { | |
delay(2000) | |
} | |
assertTrue(job1.children.count() == 1) | |
} | |
} | |
@Test(expected = Exception::class) | |
fun `Assert coroutine throws exception`() { | |
runBlocking { | |
val job1 = async { | |
delay(2000) | |
throw Exception("Error") | |
} | |
job1.await() | |
} | |
} | |
@Test | |
fun `Assert that Coroutines are more efficient than Threads`() { | |
for (i in 0..1_000_000) { | |
GlobalScope.launch { | |
val job = async { | |
println("Opa! $i") | |
delay(1_000) | |
} | |
} | |
} | |
} | |
@Test(expected = OutOfMemoryError::class) | |
fun `Assert that Threads are less efficient than Coroutines`() { | |
for (i in 0..1_000_000) { | |
Thread { | |
println("Opa! $i") | |
Thread.sleep(1_000) | |
}.start() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment