aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/test/kotlin')
-rw-r--r--core/src/test/kotlin/ca/allanwang/kau/kotlin/CoroutineTest.kt58
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