diff options
author | Allan Wang <me@allanwang.ca> | 2019-01-04 23:03:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-04 23:03:50 -0500 |
commit | d61ff7cb4f43d71d2170cdd25ceab2e3edcb81fc (patch) | |
tree | b1fcd17ad4782ef1cb5cd3aba8539669fc2aac3c /core/src/test/kotlin | |
parent | ab349293e52d77f6fc4b44446e19dc80aa8e789f (diff) | |
download | kau-d61ff7cb4f43d71d2170cdd25ceab2e3edcb81fc.tar.gz kau-d61ff7cb4f43d71d2170cdd25ceab2e3edcb81fc.tar.bz2 kau-d61ff7cb4f43d71d2170cdd25ceab2e3edcb81fc.zip |
Coroutine tests (#185)
* Add some coroutine tests for implicit cancellation
* Create util test and new helper methods
* Remove coroutinescope extension from withcontext
* Update dependencies
Diffstat (limited to 'core/src/test/kotlin')
-rw-r--r-- | core/src/test/kotlin/ca/allanwang/kau/kotlin/CoroutineTest.kt | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/core/src/test/kotlin/ca/allanwang/kau/kotlin/CoroutineTest.kt b/core/src/test/kotlin/ca/allanwang/kau/kotlin/CoroutineTest.kt new file mode 100644 index 0000000..91d0174 --- /dev/null +++ b/core/src/test/kotlin/ca/allanwang/kau/kotlin/CoroutineTest.kt @@ -0,0 +1,58 @@ +package ca.allanwang.kau.kotlin + +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.fail + +/** + * Tests geared towards coroutines + */ +class CoroutineTest { + + /** + * If a job is cancelled, then a switch to a new context will not run + */ + @Test + fun implicitCancellationBefore() { + val job = Job() + var id = 0 + try { + runBlocking(job) { + id++ + job.cancel() + withContext(Dispatchers.IO) { + fail("Context switch should not be reached") + } + } + } catch (ignore: CancellationException) { + } finally { + assertEquals(1, id, "Launcher never executed") + } + } + + /** + * If a job is cancelled, then a switch from a new context will not run + */ + @Test + fun implicitCancellationAfter() { + val job = Job() + var id = 0 + try { + runBlocking(job) { + withContext(Dispatchers.IO) { + id++ + job.cancel() + } + fail("Post context switch should not be reached") + } + } catch (ignore: CancellationException) { + } finally { + assertEquals(1, id, "Context switch never executed") + } + } +}
\ No newline at end of file |