From c260abb6f3b2a0edc954266aea0a8bb8dfadddfc Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 5 Jun 2022 18:02:06 +0100 Subject: [PATCH] Cancel TestEventDispatcher jobs after test --- .../framework/AbstractMockNetworkHandlerTest.kt | 11 ++++++++++- .../framework/AbstractRealNetworkHandlerTest.kt | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mirai-core/src/commonTest/kotlin/network/framework/AbstractMockNetworkHandlerTest.kt b/mirai-core/src/commonTest/kotlin/network/framework/AbstractMockNetworkHandlerTest.kt index 06a287401..3bccfaa34 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/AbstractMockNetworkHandlerTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/AbstractMockNetworkHandlerTest.kt @@ -30,6 +30,7 @@ import net.mamoe.mirai.utils.MiraiLogger import network.framework.components.TestEventDispatcherImpl import kotlin.math.absoluteValue import kotlin.random.Random +import kotlin.test.AfterTest import kotlin.test.assertEquals @@ -46,6 +47,14 @@ internal abstract class AbstractMockNetworkHandlerTest : AbstractNetworkHandlerT } } protected val logger = MiraiLogger.Factory.create(Bot::class, "test") + + private val eventDispatcherJob = SupervisorJob() + + @AfterTest + private fun cancelJob() { + eventDispatcherJob.cancel() + } + protected val components = ConcurrentComponentStorage().apply { set(SsoProcessor, TestSsoProcessor(bot)) 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 // to allow BotOfflineEvent.Active to be broadcast and joinBroadcast works even if bot coroutineScope is closed. TestEventDispatcherImpl( - bot.coroutineContext + SupervisorJob(), + bot.coroutineContext + eventDispatcherJob, bot.logger.subLogger("TestEventDispatcherImpl") ) ) diff --git a/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt b/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt index b85710329..5bd6b0cee 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt @@ -89,6 +89,13 @@ internal abstract class AbstractRealNetworkHandlerTest : Abs val nhEvents = ConcurrentLinkedQueue() + private val eventDispatcherJob = SupervisorJob() + + @AfterTest + private fun cancelJob() { + eventDispatcherJob.cancel() + } + /** * This overrides [QQAndroidBot.components] */ @@ -149,7 +156,7 @@ internal abstract class AbstractRealNetworkHandlerTest : Abs // 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. TestEventDispatcherImpl( - bot.coroutineContext + SupervisorJob(), + bot.coroutineContext + eventDispatcherJob, bot.logger.subLogger("TestEventDispatcherImpl") ) )