mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-01 03:50:18 +08:00
Move implementation of EventDispatcher.joinBroadcast
to tests and mark it @TestOnly
This commit is contained in:
parent
cf82f96672
commit
684b844b44
@ -14,6 +14,7 @@ import net.mamoe.mirai.event.Event
|
||||
import net.mamoe.mirai.event.broadcast
|
||||
import net.mamoe.mirai.internal.network.component.ComponentKey
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.TestOnly
|
||||
import net.mamoe.mirai.utils.addNameHierarchically
|
||||
import net.mamoe.mirai.utils.childScope
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
@ -36,7 +37,10 @@ internal interface EventDispatcher {
|
||||
/**
|
||||
* Join all jobs. Joins also jobs launched during this call.
|
||||
*/
|
||||
suspend fun joinBroadcast()
|
||||
@TestOnly
|
||||
suspend fun joinBroadcast() {
|
||||
throw UnsupportedOperationException("joinBroadcast is only supported in TestEventDispatcherImpl")
|
||||
}
|
||||
|
||||
companion object : ComponentKey<EventDispatcher>
|
||||
}
|
||||
@ -47,7 +51,7 @@ internal object EventDispatcherScopeFlag : CoroutineContext.Element, CoroutineCo
|
||||
|
||||
@JvmInline
|
||||
internal value class EventBroadcastJob(
|
||||
private val job: Job
|
||||
val job: Job
|
||||
) {
|
||||
inline fun onSuccess(crossinline action: () -> Unit) {
|
||||
job.invokeOnCompletion {
|
||||
@ -57,9 +61,9 @@ internal value class EventBroadcastJob(
|
||||
}
|
||||
|
||||
|
||||
internal class EventDispatcherImpl(
|
||||
private val lifecycleContext: CoroutineContext,
|
||||
private val logger: MiraiLogger,
|
||||
internal open class EventDispatcherImpl(
|
||||
lifecycleContext: CoroutineContext,
|
||||
protected val logger: MiraiLogger,
|
||||
) : EventDispatcher,
|
||||
CoroutineScope by lifecycleContext
|
||||
.addNameHierarchically("EventDispatcher")
|
||||
@ -88,14 +92,8 @@ internal class EventDispatcherImpl(
|
||||
return EventBroadcastJob(job)
|
||||
}
|
||||
|
||||
private fun optimizeEventToString(event: Event): String {
|
||||
protected fun optimizeEventToString(event: Event): String {
|
||||
val qualified = event::class.java.canonicalName ?: return event.toString()
|
||||
return qualified.substringAfter("net.mamoe.mirai.event.events.", "").ifEmpty { event.toString() }
|
||||
}
|
||||
|
||||
override suspend fun joinBroadcast() {
|
||||
for (child in coroutineContext.job.children) {
|
||||
child.join()
|
||||
}
|
||||
}
|
||||
}
|
@ -7,23 +7,26 @@
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:OptIn(TestOnly::class)
|
||||
|
||||
package net.mamoe.mirai.internal.network.component
|
||||
|
||||
import kotlinx.coroutines.CompletableJob
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.job
|
||||
import net.mamoe.mirai.event.AbstractEvent
|
||||
import net.mamoe.mirai.internal.network.components.EventDispatcherImpl
|
||||
import net.mamoe.mirai.internal.test.AbstractTest
|
||||
import net.mamoe.mirai.internal.test.assertEventBroadcasts
|
||||
import net.mamoe.mirai.internal.test.runBlockingUnit
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.TestOnly
|
||||
import network.framework.components.TestEventDispatcherImpl
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class EventDispatcherTest : AbstractTest() {
|
||||
private class Ev : AbstractEvent()
|
||||
|
||||
val dispatcher = EventDispatcherImpl(SupervisorJob(), MiraiLogger.TopLevel)
|
||||
private val dispatcher = TestEventDispatcherImpl(SupervisorJob(), MiraiLogger.TopLevel)
|
||||
|
||||
@Test
|
||||
fun `can broadcast`() = runBlockingUnit {
|
||||
|
@ -15,7 +15,6 @@ import net.mamoe.mirai.internal.MockBot
|
||||
import net.mamoe.mirai.internal.QQAndroidBot
|
||||
import net.mamoe.mirai.internal.network.component.ConcurrentComponentStorage
|
||||
import net.mamoe.mirai.internal.network.components.EventDispatcher
|
||||
import net.mamoe.mirai.internal.network.components.EventDispatcherImpl
|
||||
import net.mamoe.mirai.internal.network.components.SsoProcessor
|
||||
import net.mamoe.mirai.internal.network.framework.components.TestSsoProcessor
|
||||
import net.mamoe.mirai.internal.network.handler.NetworkHandler
|
||||
@ -24,8 +23,8 @@ import net.mamoe.mirai.internal.network.handler.state.SafeStateObserver
|
||||
import net.mamoe.mirai.internal.network.handler.state.StateObserver
|
||||
import net.mamoe.mirai.internal.test.AbstractTest
|
||||
import net.mamoe.mirai.internal.utils.subLogger
|
||||
import net.mamoe.mirai.supervisorJob
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import network.framework.components.TestEventDispatcherImpl
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@ -41,7 +40,10 @@ internal abstract class AbstractMockNetworkHandlerTest : AbstractTest() {
|
||||
protected val logger = MiraiLogger.create("test")
|
||||
protected val components = ConcurrentComponentStorage().apply {
|
||||
set(SsoProcessor, TestSsoProcessor(bot))
|
||||
set(EventDispatcher, EventDispatcherImpl(bot.supervisorJob, bot.logger.subLogger("ED")))
|
||||
set(
|
||||
EventDispatcher,
|
||||
TestEventDispatcherImpl(bot.coroutineContext, bot.logger.subLogger("TestEventDispatcherImpl"))
|
||||
)
|
||||
set(
|
||||
StateObserver,
|
||||
SafeStateObserver(
|
||||
|
@ -26,9 +26,11 @@ import net.mamoe.mirai.internal.network.handler.NetworkHandlerContextImpl
|
||||
import net.mamoe.mirai.internal.network.handler.NetworkHandlerFactory
|
||||
import net.mamoe.mirai.internal.network.handler.state.StateObserver
|
||||
import net.mamoe.mirai.internal.test.AbstractTest
|
||||
import net.mamoe.mirai.internal.utils.subLogger
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.debug
|
||||
import net.mamoe.mirai.utils.lateinitMutableProperty
|
||||
import network.framework.components.TestEventDispatcherImpl
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.net.InetSocketAddress
|
||||
import java.util.concurrent.ConcurrentLinkedQueue
|
||||
@ -107,6 +109,11 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
|
||||
override fun attachJob(bot: AbstractBot, scope: CoroutineScope) {
|
||||
}
|
||||
})
|
||||
|
||||
set(
|
||||
EventDispatcher,
|
||||
TestEventDispatcherImpl(bot.coroutineContext, bot.logger.subLogger("TestEventDispatcherImpl"))
|
||||
)
|
||||
// set(StateObserver, bot.run { stateObserverChain() })
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2019-2021 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/master/LICENSE
|
||||
*/
|
||||
|
||||
package network.framework.components
|
||||
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.job
|
||||
import kotlinx.coroutines.launch
|
||||
import net.mamoe.mirai.event.Event
|
||||
import net.mamoe.mirai.internal.network.components.EventDispatcherImpl
|
||||
import net.mamoe.mirai.internal.network.components.EventDispatcherScopeFlag
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.TestOnly
|
||||
import net.mamoe.mirai.utils.runUnwrapCancellationException
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
internal open class TestEventDispatcherImpl(
|
||||
lifecycleContext: CoroutineContext,
|
||||
logger: MiraiLogger,
|
||||
) : EventDispatcherImpl(lifecycleContext, logger) {
|
||||
override suspend fun broadcast(event: Event) {
|
||||
runUnwrapCancellationException {
|
||||
launch(
|
||||
EventDispatcherScopeFlag,
|
||||
start = CoroutineStart.UNDISPATCHED
|
||||
) {
|
||||
super.broadcast(event)
|
||||
}.join()
|
||||
} // so that [joinBroadcast] works.
|
||||
}
|
||||
|
||||
@OptIn(TestOnly::class)
|
||||
override suspend fun joinBroadcast() {
|
||||
for (child in coroutineContext.job.children) {
|
||||
child.join()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user