Skip to content

Commit

Permalink
adding test
Browse files Browse the repository at this point in the history
Signed-off-by: Luki <[email protected]>
  • Loading branch information
lukisk committed May 27, 2024
1 parent 6d9ca47 commit 891100c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
9 changes: 9 additions & 0 deletions multicast/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ChannelManager.Message.Dispatch<Int>>(Channel.UNLIMITED) }

val cancelledChannel =
Channel<ChannelManager.Message.Dispatch<Int>>(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<ChannelManager.Message.Dispatch.Value<Int>>()
.onEach { it.delivered.complete(Unit) }
.map { it.value }

assertEquals(
messages,
messagesFlow.take(3).toList(),
)
}
}
scope.cancel()
}
}

0 comments on commit 891100c

Please sign in to comment.