单元测试Kotlin';s合并的DBRoadCastChannel行为
在我目前正在进行的新项目中,我根本没有RxJava依赖关系,因为直到现在我还不需要它——协同程序非常优雅地解决了线程问题 在这一点上,我偶然发现了一个类似行为的要求,即可以订阅数据流并在订阅时接收最新的值。据我所知,s在Kotlin中提供了非常相似的行为,所以我决定尝试一下 从文章中我了解到,这是一种模仿行为主体的频道类型,因此我声明如下:单元测试Kotlin';s合并的DBRoadCastChannel行为,kotlin,listener,channel,kotlin-coroutines,behaviorsubject,Kotlin,Listener,Channel,Kotlin Coroutines,Behaviorsubject,在我目前正在进行的新项目中,我根本没有RxJava依赖关系,因为直到现在我还不需要它——协同程序非常优雅地解决了线程问题 在这一点上,我偶然发现了一个类似行为的要求,即可以订阅数据流并在订阅时接收最新的值。据我所知,s在Kotlin中提供了非常相似的行为,所以我决定尝试一下 从文章中我了解到,这是一种模仿行为主体的频道类型,因此我声明如下: class ChannelSender { val channel = ConflatedBroadcastChannel<String&g
class ChannelSender {
val channel = ConflatedBroadcastChannel<String>()
fun sendToChannel(someString: String) {
GlobalScope.launch(Dispatchers.Main) { channel.send(someString) }
}
}
这正如预期的那样工作,但此时我很难理解如何进行单元测试ChannelListener
我试图找到一些相关的内容,但是示例频道-**.kt
类都没有帮助
任何与我的错误假设相关的帮助、建议或纠正,我们都将不胜感激。谢谢。使用,我可以得到以下代码,这些代码回答了问题:
class ChannelListenerTest {
private val val channelSender: ChannelSender = mock()
private val sut = ChannelListener(channelSender)
private val broadcastChannel = ConflatedBroadcastChannel<String>()
private val timeLimit = 1_000L
private val endMarker = "end"
@Test
fun `some description here`() = runBlocking {
whenever(channelSender.channel).thenReturn(broadcastChannel)
val sender = launch(Dispatchers.Default) {
broadcastChannel.offer("A")
yield()
}
val receiver = launch(Dispatchers.Default) {
while (isActive) {
val i = waitForEvent()
if (i == endMarker) break
yield()
}
}
try {
withTimeout(timeLimit) {
sut.listenToChannel()
sender.join()
broadcastChannel.offer(endMarker) // last event to signal receivers termination
receiver.join()
}
verify(foo).perform()
} catch (e: CancellationException) {
println("Test timed out $e")
}
}
private suspend fun waitForEvent(): String =
with(broadcastChannel.openSubscription()) {
val value = receive()
cancel()
value
}
}
类ChannelListenerTest{
private val channelSender:channelSender=mock()
private val sut=ChannelListener(channelSender)
private val broadcastChannel=ConflatedBroadcastChannel()
专用val时间限制=1_000L
private val endMarker=“结束”
@试验
fun`这里有一些描述'()=运行阻塞{
无论何时(channelSender.channel)。然后返回(broadcastChannel)
val sender=launch(Dispatchers.Default){
广播频道报价(“A”)
收益率()
}
val receiver=launch(Dispatchers.Default){
while(isActive){
val i=waitForEvent()
如果(i==endMarker)中断
收益率()
}
}
试一试{
withTimeout(时间限制){
sut.listenToChannel()
sender.join()
broadcastChannel.offer(endMarker)//信号接收器终止的最后一个事件
receiver.join()
}
验证(foo.perform())
}捕获(e:取消异常){
println(“测试超时$e”)
}
}
private suspend fun waitForEvent():字符串=
使用(broadcastChannel.openSubscription()){
val值=接收()
取消
价值
}
}
那(特别是)呢?@AlexeyRomanov,那里有很多代码,让我来分析一下。谢谢你的提示@阿列克谢·罗曼诺夫,谢谢你的帮助,它确实指引了我正确的方向。
class ChannelListenerTest {
private val val channelSender: ChannelSender = mock()
private val sut = ChannelListener(channelSender)
private val broadcastChannel = ConflatedBroadcastChannel<String>()
private val timeLimit = 1_000L
private val endMarker = "end"
@Test
fun `some description here`() = runBlocking {
whenever(channelSender.channel).thenReturn(broadcastChannel)
val sender = launch(Dispatchers.Default) {
broadcastChannel.offer("A")
yield()
}
val receiver = launch(Dispatchers.Default) {
while (isActive) {
val i = waitForEvent()
if (i == endMarker) break
yield()
}
}
try {
withTimeout(timeLimit) {
sut.listenToChannel()
sender.join()
broadcastChannel.offer(endMarker) // last event to signal receivers termination
receiver.join()
}
verify(foo).perform()
} catch (e: CancellationException) {
println("Test timed out $e")
}
}
private suspend fun waitForEvent(): String =
with(broadcastChannel.openSubscription()) {
val value = receive()
cancel()
value
}
}