[core] Add test rethrow exception caught during Bot_login, helps #2338

This commit is contained in:
Him188 2022-12-27 16:54:05 +00:00
parent 2accabef9e
commit 38162db477
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
2 changed files with 55 additions and 1 deletions

View File

@ -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))

View File

@ -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<SelectorNetworkHandler<TestCommonNetworkHandler>>() {
// does not use selector
override val factory: NetworkHandlerFactory<SelectorNetworkHandler<TestCommonNetworkHandler>> =
NetworkHandlerFactory<SelectorNetworkHandler<TestCommonNetworkHandler>> { 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<IllegalStateException> {
bot.login()
}.let { e ->
assertEquals(exceptionMessage, e.message)
}
}
}