Use empty internalIds for all OnlineMessageSourceToGroupImpl to speed up tests.

This commit is contained in:
Him188 2022-05-23 22:26:48 +01:00
parent ab3280f6b7
commit c4a2f19db6
3 changed files with 72 additions and 19 deletions

View File

@ -30,6 +30,7 @@ import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.OnlineMessageSource
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.loadService
import net.mamoe.mirai.utils.toLongUnsigned
import java.util.concurrent.atomic.AtomicBoolean
@ -192,26 +193,11 @@ internal class OnlineMessageSourceToGroupImpl(
get() = sender
override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false)
/**
* Note that in tests result of this Deferred is always `null`. See TestMessageSourceSequenceIdAwaiter.
*/
private val sequenceIdDeferred: Deferred<IntArray?> = providedSequenceIds?.let { CompletableDeferred(it) } ?: run {
val multi = mutableMapOf<Int, Int>()
coroutineScope.async {
withTimeoutOrNull(
timeMillis = 3000L * this@OnlineMessageSourceToGroupImpl.internalIds.size
) {
GlobalEventChannel.parentScope(this)
.syncFromEvent<SendGroupMessageReceipt, IntArray>(EventPriority.MONITOR) { receipt ->
if (receipt.bot !== bot) return@syncFromEvent null
if (receipt.messageRandom in this@OnlineMessageSourceToGroupImpl.internalIds) {
multi[receipt.messageRandom] = receipt.sequenceId
if (multi.size == this@OnlineMessageSourceToGroupImpl.internalIds.size) {
IntArray(multi.size) { index ->
multi[this@OnlineMessageSourceToGroupImpl.internalIds[index]]!!
}
} else null
} else null
}
}
}
MessageSourceSequenceIdAwaiter.instance.getSequenceIdAsync(this, coroutineScope)
}
@OptIn(ExperimentalCoroutinesApi::class)
@ -266,4 +252,36 @@ internal class OnlineMessageSourceToGroupImpl(
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<OutgoingMessageSourceInternal>.accept(visitor, data)
}
}
internal open class MessageSourceSequenceIdAwaiter {
open fun getSequenceIdAsync(
sourceToGroupImpl: OnlineMessageSourceToGroupImpl,
coroutineScope: CoroutineScope
): Deferred<IntArray?> {
val multi = mutableMapOf<Int, Int>()
return coroutineScope.async {
withTimeoutOrNull(
timeMillis = 3000L * sourceToGroupImpl.internalIds.size
) {
GlobalEventChannel.parentScope(this)
.syncFromEvent<SendGroupMessageReceipt, IntArray>(EventPriority.MONITOR) { receipt ->
if (receipt.bot !== sourceToGroupImpl.bot) return@syncFromEvent null
if (receipt.messageRandom in sourceToGroupImpl.internalIds) {
multi[receipt.messageRandom] = receipt.sequenceId
if (multi.size == sourceToGroupImpl.internalIds.size) {
IntArray(multi.size) { index ->
multi[sourceToGroupImpl.internalIds[index]]!!
}
} else null
} else null
}
}
}
}
companion object {
val instance =
loadService(MessageSourceSequenceIdAwaiter::class) { MessageSourceSequenceIdAwaiter() }
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.internal.testFramework.message
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import net.mamoe.mirai.internal.message.source.MessageSourceSequenceIdAwaiter
import net.mamoe.mirai.internal.message.source.OnlineMessageSourceToGroupImpl
internal class TestMessageSourceSequenceIdAwaiter : MessageSourceSequenceIdAwaiter() {
override fun getSequenceIdAsync(
sourceToGroupImpl: OnlineMessageSourceToGroupImpl,
coroutineScope: CoroutineScope
): Deferred<IntArray?> {
return CompletableDeferred(value = null) // assuming server didn't provide response, just to be simpler
}
}

View File

@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#
net.mamoe.mirai.internal.testFramework.message.TestMessageSourceSequenceIdAwaiter