mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-07 16:40:43 +08:00
Add AbstractNettyNHTest.setSsoProcessor
This commit is contained in:
parent
0584b44823
commit
e60dd20d7b
@ -66,7 +66,7 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
|
||||
|
||||
protected val nhEvents = ConcurrentLinkedQueue<NHEvent>()
|
||||
|
||||
protected open val defaultComponents = ConcurrentComponentStorage().apply {
|
||||
open val defaultComponents = ConcurrentComponentStorage().apply {
|
||||
val components = this
|
||||
val configuration = bot.configuration
|
||||
set(SsoProcessorContext, SsoProcessorContextImpl(bot))
|
||||
|
@ -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<Tes
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun AbstractNettyNHTest.setSsoProcessor(action: suspend SsoProcessor.(handler: NetworkHandler) -> Unit) {
|
||||
defaultComponents[SsoProcessor] = object : SsoProcessor by defaultComponents[SsoProcessor] {
|
||||
override suspend fun login(handler: NetworkHandler) = action(handler)
|
||||
}
|
||||
}
|
@ -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<CusLoginException>("A") { bot.login() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test network broken`() = runBlockingUnit {
|
||||
withSsoProcessor {
|
||||
setSsoProcessor {
|
||||
delay(1000)
|
||||
channel.pipeline().fireExceptionCaught(IOException("TestNetworkBroken"))
|
||||
delay(100000) // receive bits from "network"
|
||||
|
@ -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<Unit>()
|
||||
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<Unit>())
|
||||
defaultComponents[SsoProcessor] = object : SsoProcessor by defaultComponents[SsoProcessor] {
|
||||
override suspend fun login(handler: NetworkHandler) = ok.get().join()
|
||||
setSsoProcessor {
|
||||
ok.get().join()
|
||||
}
|
||||
|
||||
assertState(INITIALIZED)
|
||||
|
Loading…
Reference in New Issue
Block a user