mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-01 03:50:18 +08:00
Add SetStateTest
This commit is contained in:
parent
02681428c9
commit
02c9fd2fb0
@ -14,14 +14,15 @@ import net.mamoe.mirai.internal.QQAndroidBot
|
||||
import net.mamoe.mirai.internal.network.components.EventDispatcher
|
||||
import net.mamoe.mirai.internal.network.components.SsoProcessor
|
||||
import net.mamoe.mirai.internal.network.handler.NetworkHandler
|
||||
import net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport
|
||||
|
||||
internal interface ITestNetworkHandler : NetworkHandler {
|
||||
val bot: QQAndroidBot
|
||||
|
||||
fun setStateClosed(exception: Throwable? = null)
|
||||
fun setStateConnecting(exception: Throwable? = null)
|
||||
fun setStateOK(channel: Channel, exception: Throwable? = null)
|
||||
fun setStateLoading(channel: Channel)
|
||||
fun setStateClosed(exception: Throwable? = null): NetworkHandlerSupport.BaseStateImpl?
|
||||
fun setStateConnecting(exception: Throwable? = null): NetworkHandlerSupport.BaseStateImpl?
|
||||
fun setStateOK(channel: Channel, exception: Throwable? = null): NetworkHandlerSupport.BaseStateImpl?
|
||||
fun setStateLoading(channel: Channel): NetworkHandlerSupport.BaseStateImpl?
|
||||
}
|
||||
|
||||
internal val ITestNetworkHandler.eventDispatcher get() = bot.components[EventDispatcher]
|
||||
|
@ -69,20 +69,20 @@ internal open class TestNetworkHandler(
|
||||
}
|
||||
|
||||
|
||||
override fun setStateClosed(exception: Throwable?) {
|
||||
setState(NetworkHandler.State.CLOSED)
|
||||
override fun setStateClosed(exception: Throwable?): TestState? {
|
||||
return setState(NetworkHandler.State.CLOSED)
|
||||
}
|
||||
|
||||
override fun setStateConnecting(exception: Throwable?) {
|
||||
setState(NetworkHandler.State.CONNECTING)
|
||||
override fun setStateConnecting(exception: Throwable?): TestState? {
|
||||
return setState(NetworkHandler.State.CONNECTING)
|
||||
}
|
||||
|
||||
override fun setStateOK(channel: Channel, exception: Throwable?) {
|
||||
setState(NetworkHandler.State.OK)
|
||||
override fun setStateOK(channel: Channel, exception: Throwable?): TestState? {
|
||||
exception?.printStackTrace()
|
||||
return setState(NetworkHandler.State.OK)
|
||||
}
|
||||
|
||||
override fun setStateLoading(channel: Channel) {
|
||||
setState(NetworkHandler.State.LOADING)
|
||||
override fun setStateLoading(channel: Channel): TestState? {
|
||||
return setState(NetworkHandler.State.LOADING)
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ import net.mamoe.mirai.internal.network.framework.ITestNetworkHandler
|
||||
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.internal.network.handler.NetworkHandlerSupport
|
||||
import net.mamoe.mirai.utils.ExceptionCollector
|
||||
import java.net.SocketAddress
|
||||
|
||||
@ -32,21 +33,21 @@ internal open class TestNettyNH(
|
||||
address: SocketAddress,
|
||||
) : NettyNetworkHandler(context, address), ITestNetworkHandler {
|
||||
|
||||
override fun setStateClosed(exception: Throwable?) {
|
||||
setState { StateClosed(exception) }
|
||||
override fun setStateClosed(exception: Throwable?): NetworkHandlerSupport.BaseStateImpl? {
|
||||
return setState { StateClosed(exception) }
|
||||
}
|
||||
|
||||
override fun setStateConnecting(exception: Throwable?) {
|
||||
setState { StateConnecting(ExceptionCollector(exception)) }
|
||||
override fun setStateConnecting(exception: Throwable?): NetworkHandlerSupport.BaseStateImpl? {
|
||||
return setState { StateConnecting(ExceptionCollector(exception)) }
|
||||
}
|
||||
|
||||
override fun setStateOK(channel: Channel, exception: Throwable?) {
|
||||
override fun setStateOK(channel: Channel, exception: Throwable?): NetworkHandlerSupport.BaseStateImpl? {
|
||||
exception?.printStackTrace()
|
||||
setState { StateOK(channel, CompletableDeferred(Unit)) }
|
||||
return setState { StateOK(channel, CompletableDeferred(Unit)) }
|
||||
}
|
||||
|
||||
override fun setStateLoading(channel: Channel) {
|
||||
setState { StateLoading(channel) }
|
||||
override fun setStateLoading(channel: Channel): NetworkHandlerSupport.BaseStateImpl? {
|
||||
return setState { StateLoading(channel) }
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2019-2021 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/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.internal.network.impl.netty
|
||||
|
||||
import net.mamoe.mirai.event.Event
|
||||
import net.mamoe.mirai.event.events.BotOfflineEvent
|
||||
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.*
|
||||
import net.mamoe.mirai.internal.test.assertEventBroadcasts
|
||||
import net.mamoe.mirai.internal.test.runBlockingUnit
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
|
||||
internal class SetStateTest : AbstractNettyNHTest() {
|
||||
@Test
|
||||
fun `setState should ignore duplications INITIALIZED to CLOSED to CLOSED`() {
|
||||
assertState(INITIALIZED)
|
||||
assertNotNull(network.setStateClosed(IllegalStateException("1")))
|
||||
assertState(CLOSED)
|
||||
assertNull(network.setStateClosed(IllegalStateException("2")))
|
||||
assertState(CLOSED)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `setState should ignore duplications CONNECTIN to CLOSED to CLOSED`() {
|
||||
assertNotNull(network.setStateConnecting())
|
||||
assertState(CONNECTING)
|
||||
assertNotNull(network.setStateClosed(IllegalStateException("1")))
|
||||
assertState(CLOSED)
|
||||
assertNull(network.setStateClosed(IllegalStateException("2")))
|
||||
assertState(CLOSED)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `setState should ignore duplications LOADING to CLOSED to CLOSED`() {
|
||||
assertNotNull(network.setStateLoading(channel))
|
||||
assertState(LOADING)
|
||||
assertNotNull(network.setStateClosed(IllegalStateException("1")))
|
||||
assertState(CLOSED)
|
||||
assertNull(network.setStateClosed(IllegalStateException("2")))
|
||||
assertState(CLOSED)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `setState should ignore duplications OK to CLOSED to CLOSED`() {
|
||||
assertNotNull(network.setStateOK(channel))
|
||||
assertState(OK)
|
||||
assertNotNull(network.setStateClosed(IllegalStateException("1")))
|
||||
assertState(CLOSED)
|
||||
assertNull(network.setStateClosed(IllegalStateException("2")))
|
||||
assertState(CLOSED)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `setState should ignore duplications 2 OK to CLOSED to CLOSED`() = runBlockingUnit {
|
||||
assertNotNull(network.setStateOK(channel))
|
||||
assertState(OK)
|
||||
assertEventBroadcasts<Event> {
|
||||
assertNotNull(network.setStateClosed(IllegalStateException("1")))
|
||||
assertState(CLOSED)
|
||||
assertNull(network.setStateClosed(IllegalStateException("2")))
|
||||
assertState(CLOSED)
|
||||
eventDispatcher.joinBroadcast()
|
||||
}.let { list ->
|
||||
assertEquals(1, list.size)
|
||||
assertIs<BotOfflineEvent.Active>(list[0])
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user