Cancel TestEventDispatcher jobs after test

This commit is contained in:
Him188 2022-06-05 18:02:06 +01:00
parent a964f7ee87
commit c260abb6f3
2 changed files with 18 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import net.mamoe.mirai.utils.MiraiLogger
import network.framework.components.TestEventDispatcherImpl import network.framework.components.TestEventDispatcherImpl
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
import kotlin.random.Random import kotlin.random.Random
import kotlin.test.AfterTest
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -46,6 +47,14 @@ internal abstract class AbstractMockNetworkHandlerTest : AbstractNetworkHandlerT
} }
} }
protected val logger = MiraiLogger.Factory.create(Bot::class, "test") protected val logger = MiraiLogger.Factory.create(Bot::class, "test")
private val eventDispatcherJob = SupervisorJob()
@AfterTest
private fun cancelJob() {
eventDispatcherJob.cancel()
}
protected val components = ConcurrentComponentStorage().apply { protected val components = ConcurrentComponentStorage().apply {
set(SsoProcessor, TestSsoProcessor(bot)) set(SsoProcessor, TestSsoProcessor(bot))
set( set(
@ -53,7 +62,7 @@ internal abstract class AbstractMockNetworkHandlerTest : AbstractNetworkHandlerT
// Note that in real we use 'bot.coroutineContext', but here we override with a new, independent job // Note that in real we use 'bot.coroutineContext', but here we override with a new, independent job
// to allow BotOfflineEvent.Active to be broadcast and joinBroadcast works even if bot coroutineScope is closed. // to allow BotOfflineEvent.Active to be broadcast and joinBroadcast works even if bot coroutineScope is closed.
TestEventDispatcherImpl( TestEventDispatcherImpl(
bot.coroutineContext + SupervisorJob(), bot.coroutineContext + eventDispatcherJob,
bot.logger.subLogger("TestEventDispatcherImpl") bot.logger.subLogger("TestEventDispatcherImpl")
) )
) )

View File

@ -89,6 +89,13 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
val nhEvents = ConcurrentLinkedQueue<NHEvent>() val nhEvents = ConcurrentLinkedQueue<NHEvent>()
private val eventDispatcherJob = SupervisorJob()
@AfterTest
private fun cancelJob() {
eventDispatcherJob.cancel()
}
/** /**
* This overrides [QQAndroidBot.components] * This overrides [QQAndroidBot.components]
*/ */
@ -149,7 +156,7 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
// Note that in real we use 'bot.coroutineContext', but here we override with a new, independent job // Note that in real we use 'bot.coroutineContext', but here we override with a new, independent job
// to allow BotOfflineEvent.Active to be broadcast and joinBroadcast works even if bot coroutineScope is closed. // to allow BotOfflineEvent.Active to be broadcast and joinBroadcast works even if bot coroutineScope is closed.
TestEventDispatcherImpl( TestEventDispatcherImpl(
bot.coroutineContext + SupervisorJob(), bot.coroutineContext + eventDispatcherJob,
bot.logger.subLogger("TestEventDispatcherImpl") bot.logger.subLogger("TestEventDispatcherImpl")
) )
) )