From 891100c95f9d0f353b131044aba76bc14b887d36 Mon Sep 17 00:00:00 2001 From: luki Date: Mon, 27 May 2024 15:24:39 +0200 Subject: [PATCH] adding test Signed-off-by: Luki --- multicast/build.gradle.kts | 9 +++ .../multicast5/StoreChannelManagerTests.kt | 73 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 multicast/src/commonTest/kotlin/org/mobilenativefoundation/store/multicast5/StoreChannelManagerTests.kt diff --git a/multicast/build.gradle.kts b/multicast/build.gradle.kts index 2b602120f..89dd0cfb5 100644 --- a/multicast/build.gradle.kts +++ b/multicast/build.gradle.kts @@ -47,6 +47,15 @@ kotlin { implementation(libs.kotlinx.coroutines.core) } } + + val commonTest by getting { + dependencies { + implementation(kotlin("test")) + implementation(libs.junit) + implementation(libs.kotlinx.coroutines.test) + } + } + val jvmMain by getting val androidMain by getting val nativeMain by creating { diff --git a/multicast/src/commonTest/kotlin/org/mobilenativefoundation/store/multicast5/StoreChannelManagerTests.kt b/multicast/src/commonTest/kotlin/org/mobilenativefoundation/store/multicast5/StoreChannelManagerTests.kt new file mode 100644 index 000000000..7659d8599 --- /dev/null +++ b/multicast/src/commonTest/kotlin/org/mobilenativefoundation/store/multicast5/StoreChannelManagerTests.kt @@ -0,0 +1,73 @@ +package org.mobilenativefoundation.store.multicast5 + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.cancel +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.consumeAsFlow +import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.take +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals + +class StoreChannelManagerTests { + + @Test + fun cancelledDownstreamChannelShouldNotCancelOtherChannels() { + val messages = listOf(1, 2, 3) + val scope = CoroutineScope(Dispatchers.Default) + val lockUpstream = Mutex(true) + val upstreamFlow = flow { + lockUpstream.withLock { + messages.onEach { emit(it) } + } + } + val channelManager = StoreChannelManager( + scope = scope, + bufferSize = 0, + upstream = upstreamFlow, + piggybackingDownstream = false, + keepUpstreamAlive = false, + onEach = { } + ) + val channels = + (1..20).map { Channel>(Channel.UNLIMITED) } + + val cancelledChannel = + Channel>(Channel.UNLIMITED).also { + scope.launch { + it.consumeAsFlow().first() + } + } + + scope.launch { + channels.forEach { channelManager.addDownstream(it) } + lockUpstream.unlock() + } + scope.launch { channelManager.addDownstream(cancelledChannel) } + + runTest { + channels.forEach { channel -> + val messagesFlow = channel.consumeAsFlow() + .filterIsInstance>() + .onEach { it.delivered.complete(Unit) } + .map { it.value } + + assertEquals( + messages, + messagesFlow.take(3).toList(), + ) + } + } + scope.cancel() + } +}