-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update ChannelManager.kt Signed-off-by: Luki <[email protected]> * adding test Signed-off-by: Luki <[email protected]> * Update StoreChannelManagerTests.kt Minor changes for readability, consistency, and scope management * Update build.gradle.kts Using Turbine --------- Signed-off-by: Luki <[email protected]> Co-authored-by: Matt Ramotar <[email protected]>
- Loading branch information
1 parent
63928aa
commit cc39941
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...commonTest/kotlin/org/mobilenativefoundation/store/multicast5/StoreChannelManagerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.mobilenativefoundation.store.multicast5 | ||
|
||
|
||
import app.cash.turbine.test | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.consumeAsFlow | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.onEach | ||
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() = runTest { | ||
val coroutineScope = CoroutineScope(Dispatchers.Default) | ||
val lockUpstream = Mutex(true) | ||
val testMessages = listOf(1, 2, 3) | ||
val numChannels = 20 | ||
val upstreamFlow = flow { | ||
lockUpstream.withLock { | ||
testMessages.onEach { emit(it) } | ||
} | ||
} | ||
val channelManager = StoreChannelManager( | ||
scope = coroutineScope, | ||
bufferSize = 0, | ||
upstream = upstreamFlow, | ||
piggybackingDownstream = false, | ||
keepUpstreamAlive = false, | ||
onEach = { } | ||
) | ||
val channels = createChannels(numChannels) | ||
val channelToBeCancelled = Channel<ChannelManager.Message.Dispatch<Int>>(Channel.UNLIMITED) | ||
.also { channel -> | ||
coroutineScope.launch { | ||
channel.consumeAsFlow().test { | ||
cancelAndIgnoreRemainingEvents() | ||
} | ||
} | ||
} | ||
coroutineScope.launch { | ||
channels.forEach { channelManager.addDownstream(it) } | ||
lockUpstream.unlock() | ||
} | ||
coroutineScope.launch { | ||
channelManager.addDownstream(channelToBeCancelled) | ||
} | ||
|
||
channels.forEach { channel -> | ||
val messagesFlow = channel.consumeAsFlow() | ||
.filterIsInstance<ChannelManager.Message.Dispatch.Value<Int>>() | ||
.onEach { it.delivered.complete(Unit) } | ||
|
||
messagesFlow.test { | ||
for (message in testMessages) { | ||
val dispatchValue = awaitItem() | ||
assertEquals(message, dispatchValue.value) | ||
} | ||
awaitComplete() | ||
} | ||
} | ||
} | ||
|
||
private fun createChannels(count: Int): List<Channel<ChannelManager.Message.Dispatch<Int>>> { | ||
return (1..count).map { Channel(Channel.UNLIMITED) } | ||
} | ||
} |