From 083e410719c55dd5e39a7e0acfd88ca07f4bc1c5 Mon Sep 17 00:00:00 2001 From: Colin White Date: Thu, 19 Dec 2024 20:40:26 -0500 Subject: [PATCH] Improve tests. --- ...rwardingUnconfinedCoroutineDispatcherTest.kt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/coil-compose-core/src/commonTest/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineDispatcherTest.kt b/coil-compose-core/src/commonTest/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineDispatcherTest.kt index 2f29255f54..31b0a70a7d 100644 --- a/coil-compose-core/src/commonTest/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineDispatcherTest.kt +++ b/coil-compose-core/src/commonTest/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineDispatcherTest.kt @@ -22,6 +22,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertIs import kotlin.time.Duration.Companion.milliseconds +import kotlinx.atomicfu.atomic import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -35,23 +36,17 @@ class ForwardingUnconfinedCoroutineDispatcherTest : RobolectricTest() { private val testDispatcher = TestCoroutineDispatcher() private val forwardingDispatcher = ForwardingUnconfinedCoroutineDispatcher(testDispatcher) - @Test - fun `does not dispatch when suspended by default`() = runTestWithForwardingDispatcher { - delay(100.milliseconds) - assertEquals(0, testDispatcher.dispatchCount) - } - @Test fun `does not dispatch when unconfined=true`() = runTestWithForwardingDispatcher { forwardingDispatcher.unconfined = true - withContext(Dispatchers.Default) {} + delay(100.milliseconds) assertEquals(0, testDispatcher.dispatchCount) } @Test fun `does dispatch when unconfined=false`() = runTestWithForwardingDispatcher { forwardingDispatcher.unconfined = false - withContext(Dispatchers.Default) {} + delay(100.milliseconds) assertEquals(1, testDispatcher.dispatchCount) } @@ -96,11 +91,11 @@ class ForwardingUnconfinedCoroutineDispatcherTest : RobolectricTest() { ) = runTest { withContext(forwardingDispatcher, testBody) } private class TestCoroutineDispatcher : CoroutineDispatcher() { - var dispatchCount = 0 - private set + private var _dispatchCount by atomic(0) + val dispatchCount get() = _dispatchCount override fun dispatch(context: CoroutineContext, block: Runnable) { - dispatchCount++ + _dispatchCount++ block.run() } }