Do not listen BotOfflineEvent to close network

This commit is contained in:
Him188 2021-06-06 13:50:50 +08:00
parent 44ef95a4d0
commit 0d28772275
2 changed files with 24 additions and 25 deletions

View File

@ -42,7 +42,7 @@ private data class BotClosedByEvent(val event: BotOfflineEvent) : RuntimeExcepti
internal class BotOfflineEventMonitorImpl : BotOfflineEventMonitor { internal class BotOfflineEventMonitorImpl : BotOfflineEventMonitor {
override fun attachJob(bot: AbstractBot, scope: CoroutineScope) { override fun attachJob(bot: AbstractBot, scope: CoroutineScope) {
return return // leave it until 2.7-RC
bot.eventChannel.parentScope(scope).subscribeAlways( bot.eventChannel.parentScope(scope).subscribeAlways(
::onEvent, ::onEvent,
priority = EventPriority.MONITOR, priority = EventPriority.MONITOR,

View File

@ -12,11 +12,7 @@ package net.mamoe.mirai.internal.network.impl.netty
import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.BotOfflineEvent
import net.mamoe.mirai.internal.MockBot import net.mamoe.mirai.internal.MockBot
import net.mamoe.mirai.internal.network.components.BotOfflineEventMonitor
import net.mamoe.mirai.internal.network.components.BotOfflineEventMonitorImpl
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.* import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.*
import net.mamoe.mirai.internal.test.runBlockingUnit import net.mamoe.mirai.internal.test.runBlockingUnit
import net.mamoe.mirai.supervisorJob import net.mamoe.mirai.supervisorJob
@ -27,27 +23,30 @@ import kotlin.test.assertTrue
internal class NettyBotLifecycleTest : AbstractNettyNHTest() { internal class NettyBotLifecycleTest : AbstractNettyNHTest() {
@Test
fun `closed on Force offline with BotOfflineEventMonitor`() = runBlockingUnit {
defaultComponents[BotOfflineEventMonitor] = BotOfflineEventMonitorImpl()
bot.login()
assertState(OK)
BotOfflineEvent.Force(bot, "test", "test").broadcast()
assertState(CLOSED)
assertFalse { network.isActive }
assertTrue { bot.isActive }
}
@Test // not allowed anymore
fun `closed on Active offline with BotOfflineEventMonitor`() = runBlockingUnit {
defaultComponents[BotOfflineEventMonitor] = BotOfflineEventMonitorImpl() // @Test
bot.login() // fun `closed on Force offline with BotOfflineEventMonitor`() = runBlockingUnit {
assertState(OK) // defaultComponents[BotOfflineEventMonitor] = BotOfflineEventMonitorImpl()
BotOfflineEvent.Active(bot, null).broadcast() // bot.login()
assertState(CLOSED) // assertState(OK)
assertFalse { network.isActive } // BotOfflineEvent.Force(bot, "test", "test").broadcast()
assertTrue { bot.isActive } // assertState(CLOSED)
} // assertFalse { network.isActive }
// assertTrue { bot.isActive }
// }
// @Test
// fun `closed on Active offline with BotOfflineEventMonitor`() = runBlockingUnit {
// defaultComponents[BotOfflineEventMonitor] = BotOfflineEventMonitorImpl()
// bot.login()
// assertState(OK)
// BotOfflineEvent.Active(bot, null).broadcast()
// assertState(CLOSED)
// assertFalse { network.isActive }
// assertTrue { bot.isActive }
// }
@Test @Test