From e60dd20d7b40fe944e661c8efab03b4262c74cc1 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 6 May 2021 17:16:14 +0800 Subject: [PATCH] Add AbstractNettyNHTest.setSsoProcessor --- .../framework/AbstractRealNetworkHandlerTest.kt | 2 +- .../network/impl/netty/AbstractNettyNHTest.kt | 8 ++++++++ .../network/impl/netty/NettyBotNormalLoginTest.kt | 6 ++---- .../network/impl/netty/NettyHandlerEventTest.kt | 13 ++++++------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt b/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt index 7d1a18b44..bf42752f7 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt @@ -66,7 +66,7 @@ internal abstract class AbstractRealNetworkHandlerTest : Abs protected val nhEvents = ConcurrentLinkedQueue() - protected open val defaultComponents = ConcurrentComponentStorage().apply { + open val defaultComponents = ConcurrentComponentStorage().apply { val components = this val configuration = bot.configuration set(SsoProcessorContext, SsoProcessorContextImpl(bot)) diff --git a/mirai-core/src/commonTest/kotlin/network/impl/netty/AbstractNettyNHTest.kt b/mirai-core/src/commonTest/kotlin/network/impl/netty/AbstractNettyNHTest.kt index 8c8f703a4..3c45ead53 100644 --- a/mirai-core/src/commonTest/kotlin/network/impl/netty/AbstractNettyNHTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/impl/netty/AbstractNettyNHTest.kt @@ -13,7 +13,9 @@ import io.netty.channel.Channel import io.netty.channel.embedded.EmbeddedChannel import io.netty.util.ReferenceCountUtil import kotlinx.coroutines.CompletableDeferred +import net.mamoe.mirai.internal.network.components.SsoProcessor import net.mamoe.mirai.internal.network.framework.AbstractRealNetworkHandlerTest +import net.mamoe.mirai.internal.network.handler.NetworkHandler import net.mamoe.mirai.internal.network.handler.NetworkHandlerContext import net.mamoe.mirai.internal.network.handler.NetworkHandlerFactory import net.mamoe.mirai.utils.ExceptionCollector @@ -81,4 +83,10 @@ internal abstract class AbstractNettyNHTest : AbstractRealNetworkHandlerTest Unit) { + defaultComponents[SsoProcessor] = object : SsoProcessor by defaultComponents[SsoProcessor] { + override suspend fun login(handler: NetworkHandler) = action(handler) + } } \ No newline at end of file diff --git a/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotNormalLoginTest.kt b/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotNormalLoginTest.kt index b3648a8be..a005f1371 100644 --- a/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotNormalLoginTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotNormalLoginTest.kt @@ -20,24 +20,22 @@ import net.mamoe.mirai.internal.test.assertEventBroadcasts import net.mamoe.mirai.internal.test.runBlockingUnit import net.mamoe.mirai.utils.firstIsInstanceOrNull import org.junit.jupiter.api.Test -import org.junit.jupiter.api.TestInstance import java.io.IOException import kotlin.test.assertFailsWith import kotlin.test.assertNotNull -@TestInstance(TestInstance.Lifecycle.PER_METHOD) internal class NettyBotNormalLoginTest : AbstractNettyNHTest() { class CusLoginException(message: String?) : RuntimeException(message) @Test fun `test login fail`() = runBlockingUnit { - withSsoProcessor { throw CusLoginException("A") } + setSsoProcessor { throw CusLoginException("A") } assertFailsWith("A") { bot.login() } } @Test fun `test network broken`() = runBlockingUnit { - withSsoProcessor { + setSsoProcessor { delay(1000) channel.pipeline().fireExceptionCaught(IOException("TestNetworkBroken")) delay(100000) // receive bits from "network" diff --git a/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyHandlerEventTest.kt b/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyHandlerEventTest.kt index 5b762ee2c..d41091a12 100644 --- a/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyHandlerEventTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyHandlerEventTest.kt @@ -18,7 +18,6 @@ import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.event.events.BotOnlineEvent import net.mamoe.mirai.event.events.BotReloginEvent import net.mamoe.mirai.internal.network.components.SsoProcessor -import net.mamoe.mirai.internal.network.handler.NetworkHandler import net.mamoe.mirai.internal.network.handler.NetworkHandler.State import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.INITIALIZED import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.OK @@ -88,8 +87,8 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() { @Test fun `from OK TO CONNECTING`() = runBlockingUnit { - defaultComponents[SsoProcessor] = object : SsoProcessor by defaultComponents[SsoProcessor] { - override suspend fun login(handler: NetworkHandler) = awaitCancellation() // never ends + setSsoProcessor { + awaitCancellation() } assertState(INITIALIZED) network.setStateOK(channel) @@ -105,8 +104,8 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() { @Test fun `from CONNECTING TO OK the first time`() = runBlockingUnit { val ok = CompletableDeferred() - defaultComponents[SsoProcessor] = object : SsoProcessor by defaultComponents[SsoProcessor] { - override suspend fun login(handler: NetworkHandler) = ok.join() + setSsoProcessor { + ok.join() } assertState(INITIALIZED) network.setStateConnecting() @@ -122,8 +121,8 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() { @Test fun `from CONNECTING TO OK the second time`() = runBlockingUnit { val ok = AtomicReference(CompletableDeferred()) - defaultComponents[SsoProcessor] = object : SsoProcessor by defaultComponents[SsoProcessor] { - override suspend fun login(handler: NetworkHandler) = ok.get().join() + setSsoProcessor { + ok.get().join() } assertState(INITIALIZED)