From 38162db4774795dc1d10ad56cf26976c6b530b18 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 27 Dec 2022 16:54:05 +0000 Subject: [PATCH] [core] Add test `rethrow exception caught during Bot_login`, helps #2338 --- ...st.kt => SelectorHeartbeatRecoveryTest.kt} | 2 +- .../handler/SelectorLoginRecoveryTest.kt | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) rename mirai-core/src/commonTest/kotlin/network/handler/{SelectorRecoveryTest.kt => SelectorHeartbeatRecoveryTest.kt} (98%) create mode 100644 mirai-core/src/commonTest/kotlin/network/handler/SelectorLoginRecoveryTest.kt diff --git a/mirai-core/src/commonTest/kotlin/network/handler/SelectorRecoveryTest.kt b/mirai-core/src/commonTest/kotlin/network/handler/SelectorHeartbeatRecoveryTest.kt similarity index 98% rename from mirai-core/src/commonTest/kotlin/network/handler/SelectorRecoveryTest.kt rename to mirai-core/src/commonTest/kotlin/network/handler/SelectorHeartbeatRecoveryTest.kt index c0c264cdc..1103a522d 100644 --- a/mirai-core/src/commonTest/kotlin/network/handler/SelectorRecoveryTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/handler/SelectorHeartbeatRecoveryTest.kt @@ -29,7 +29,7 @@ import kotlin.test.* /** * Test whether the selector can recover the connection after first successful login. */ -internal class SelectorRecoveryTest : AbstractCommonNHTestWithSelector() { +internal class SelectorHeartbeatRecoveryTest : AbstractCommonNHTestWithSelector() { // @BeforeTest // fun beforeTest(info: TestInfo) { // println("=".repeat(30) + "BEGIN: ${info.displayName}" + "=".repeat(30)) diff --git a/mirai-core/src/commonTest/kotlin/network/handler/SelectorLoginRecoveryTest.kt b/mirai-core/src/commonTest/kotlin/network/handler/SelectorLoginRecoveryTest.kt new file mode 100644 index 000000000..d009b2897 --- /dev/null +++ b/mirai-core/src/commonTest/kotlin/network/handler/SelectorLoginRecoveryTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2019-2022 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/dev/LICENSE + */ + +package net.mamoe.mirai.internal.network.handler + +import kotlinx.coroutines.test.runTest +import net.mamoe.mirai.internal.network.components.SsoProcessor +import net.mamoe.mirai.internal.network.framework.AbstractRealNetworkHandlerTest +import net.mamoe.mirai.internal.network.framework.TestCommonNetworkHandler +import net.mamoe.mirai.internal.network.framework.components.TestSsoProcessor +import net.mamoe.mirai.internal.network.handler.selector.SelectorNetworkHandler +import net.mamoe.mirai.internal.network.protocol.packet.login.WtLogin +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +internal class SelectorLoginRecoveryTest : + AbstractRealNetworkHandlerTest>() { + + // does not use selector + override val factory: NetworkHandlerFactory> = + NetworkHandlerFactory> { context, address -> + SelectorNetworkHandler(TestSelector { + object : TestCommonNetworkHandler(bot, context, address) { + } + }) + } + + /** + * 登录时遇到未知错误, [WtLogin] 会抛 [IllegalStateException] (即抛不可挽救的异常), + * selector 应该停止登录, 不要重试, 重新抛出捕获的异常. + */ + @Test + fun `rethrow exception caught during Bot_login`() = runTest { + val exceptionMessage = "Login failed!" + setComponent(SsoProcessor, object : TestSsoProcessor(bot) { + override suspend fun login(handler: NetworkHandler) { + throw IllegalStateException(exceptionMessage) + } + }) + + assertFailsWith { + bot.login() + }.let { e -> + assertEquals(exceptionMessage, e.message) + } + } +} \ No newline at end of file