aboutsummaryrefslogtreecommitdiff
path: root/app/src/test/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-27 14:45:08 -0500
committerAllan Wang <me@allanwang.ca>2018-12-27 14:45:08 -0500
commit0c3eb798345874b608776e9aab15278b33f996b5 (patch)
tree8f1946f50826c372877b49816db4c5503a845a91 /app/src/test/kotlin
parentf9e3a324e47a81a30aade003cf6f829d03c81414 (diff)
downloadfrost-0c3eb798345874b608776e9aab15278b33f996b5.tar.gz
frost-0c3eb798345874b608776e9aab15278b33f996b5.tar.bz2
frost-0c3eb798345874b608776e9aab15278b33f996b5.zip
Add tests for channel context switching
Diffstat (limited to 'app/src/test/kotlin')
-rw-r--r--app/src/test/kotlin/com/pitchedapps/frost/views/FrostContentViewAsyncTest.kt62
1 files changed, 41 insertions, 21 deletions
diff --git a/app/src/test/kotlin/com/pitchedapps/frost/views/FrostContentViewAsyncTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/views/FrostContentViewAsyncTest.kt
index a179fb98..13c40aa3 100644
--- a/app/src/test/kotlin/com/pitchedapps/frost/views/FrostContentViewAsyncTest.kt
+++ b/app/src/test/kotlin/com/pitchedapps/frost/views/FrostContentViewAsyncTest.kt
@@ -1,19 +1,22 @@
package com.pitchedapps.frost.views
import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.ExecutorCoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.ReceiveChannel
+import kotlinx.coroutines.channels.count
+import kotlinx.coroutines.joinAll
+import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors
-import kotlin.test.AfterTest
-import kotlin.test.BeforeTest
+import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.Test
import kotlin.test.assertEquals
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
/**
* Collection of tests around the view thread logic
@@ -22,24 +25,6 @@ import kotlin.test.assertEquals
class FrostContentViewAsyncTest {
/**
- * Single threaded dispatcher with thread name "main"
- * Mimics the usage of Android's main dispatcher
- */
- private lateinit var mainDispatcher: ExecutorCoroutineDispatcher
-
- @BeforeTest
- fun before() {
- mainDispatcher = Executors.newSingleThreadExecutor { r ->
- Thread(r, "main")
- }.asCoroutineDispatcher()
- }
-
- @AfterTest
- fun after() {
- mainDispatcher.close()
- }
-
- /**
* Hooks onto the refresh channel for one true -> false cycle.
* Returns the list of event ids that were emitted
*/
@@ -107,4 +92,39 @@ class FrostContentViewAsyncTest {
)
}
}
+
+ /**
+ * Sanity check to ensure that contexts are being honoured
+ */
+ @Test
+ fun contextSwitching() {
+ val mainTag = "main-test"
+ val mainDispatcher = Executors.newSingleThreadExecutor { r ->
+ Thread(r, mainTag)
+ }.asCoroutineDispatcher()
+
+ val channel = BroadcastChannel<String>(100)
+
+ runBlocking(Dispatchers.IO) {
+ val receiver1 = channel.openSubscription()
+ val receiver2 = channel.openSubscription()
+ launch(mainDispatcher) {
+ for (thread in receiver1) {
+ assertTrue(
+ Thread.currentThread().name.startsWith(mainTag),
+ "Channel should be received in main thread"
+ )
+ assertFalse(
+ thread.startsWith(mainTag),
+ "Channel execution should not be in main thread"
+ )
+ }
+ }
+ listOf(EmptyCoroutineContext, Dispatchers.IO, Dispatchers.Default, Dispatchers.IO).map {
+ async(it) { channel.send(Thread.currentThread().name) }
+ }.joinAll()
+ channel.close()
+ assertEquals(4, receiver2.count(), "Not all events received")
+ }
+ }
} \ No newline at end of file