Browse Source

Cancel TestEventDispatcher jobs after test

Him188 3 years ago
parent
commit
c260abb6f3

+ 10 - 1
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")
             )
         )

+ 8 - 1
mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt

@@ -89,6 +89,13 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
 
     val nhEvents = ConcurrentLinkedQueue<NHEvent>()
 
+    private val eventDispatcherJob = SupervisorJob()
+
+    @AfterTest
+    private fun cancelJob() {
+        eventDispatcherJob.cancel()
+    }
+
     /**
      * 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
             // 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")
             )
         )