Test: state tests

This commit is contained in:
Him188 2021-04-25 13:18:05 +08:00
parent ae55908839
commit e584bc8ee8
4 changed files with 88 additions and 32 deletions

View File

@ -1,24 +0,0 @@
/*
* 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.handler
import net.mamoe.mirai.internal.network.framework.AbstractMockNetworkHandlerTest
import kotlin.test.Test
/**
* @see NetworkHandler.resumeConnection
*/
internal class ResumeConnectionTest : AbstractMockNetworkHandlerTest() {
@Test
fun `resumeConnection switches a state that can send packet`() {
createNetworkHandler().sendPacket
}
}

View File

@ -30,6 +30,10 @@ internal open class TestNettyNH(
setState { StateConnecting(ExceptionCollector(exception), false) }
}
fun setStateLoading(channel: Channel) {
setState { StateLoading(channel) }
}
}
internal abstract class AbstractNettyNHTest : AbstractRealNetworkHandlerTest<TestNettyNH>() {

View File

@ -10,10 +10,13 @@
package net.mamoe.mirai.internal.network.impl.netty
import kotlinx.coroutines.delay
import net.mamoe.mirai.event.Event
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.handler.NetworkHandler.State
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.INITIALIZED
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.OK
import net.mamoe.mirai.internal.test.assertEventBroadcasts
import net.mamoe.mirai.internal.test.runBlockingUnit
import kotlin.test.Test
@ -25,46 +28,47 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() {
@Test
fun `BotOnlineEvent after successful logon`() = runBlockingUnit {
assertEventBroadcasts<BotOnlineEvent> {
assertEquals(State.INITIALIZED, network.state)
assertEquals(INITIALIZED, network.state)
bot.login() // launches a job which broadcasts the event
delay(3.seconds)
assertEquals(State.OK, network.state)
assertEquals(OK, network.state)
}
}
@Test
fun `BotReloginEvent after successful reconnection`() = runBlockingUnit {
assertEventBroadcasts<BotReloginEvent> {
assertEquals(State.INITIALIZED, network.state)
assertEquals(INITIALIZED, network.state)
bot.login()
bot.firstLoginSucceed = true
network.setStateConnecting()
network.resumeConnection()
delay(3.seconds) // `login` launches a job which broadcasts the event
assertEquals(State.OK, network.state)
assertEquals(OK, network.state)
}
}
@Test
fun `BotOnlineEvent after successful reconnection`() = runBlockingUnit {
assertEquals(State.INITIALIZED, network.state)
assertEquals(INITIALIZED, network.state)
bot.login()
bot.firstLoginSucceed = true
assertEquals(OK, network.state)
delay(3.seconds) // `login` launches a job which broadcasts the event
assertEventBroadcasts<BotOnlineEvent>(1) {
network.setStateConnecting()
network.resumeConnection()
delay(3.seconds)
assertEquals(State.OK, network.state)
assertEquals(OK, network.state)
}
}
@Test
fun `BotOfflineEvent after successful reconnection`() = runBlockingUnit {
assertEquals(State.INITIALIZED, network.state)
assertEquals(INITIALIZED, network.state)
bot.login()
bot.firstLoginSucceed = true
assertEquals(State.OK, network.state)
assertEquals(OK, network.state)
delay(3.seconds) // `login` launches a job which broadcasts the event
assertEventBroadcasts<BotOfflineEvent>(1) {
network.setStateClosed()
@ -72,4 +76,27 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() {
assertEquals(State.CLOSED, network.state)
}
}
private fun noEventOn(setState: () -> Unit) = runBlockingUnit {
assertState(INITIALIZED)
bot.login()
bot.firstLoginSucceed = true
assertState(OK)
network.setStateConnecting()
delay(3.seconds) // `login` launches a job which broadcasts the event
assertEventBroadcasts<Event>(0) {
setState()
delay(3.seconds)
}
}
@Test
fun `no event from CONNECTING TO CLOSED`() = noEventOn { network.setStateConnecting() }
@Test
fun `no event from CLOSED TO CLOSED`() = noEventOn { network.setStateClosed() }
@Test
fun `no event from INITIALIZED TO CLOSED`() = noEventOn { }
}

View File

@ -0,0 +1,49 @@
/*
* 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 kotlinx.io.core.ByteReadPacket
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.internal.test.runBlockingUnit
import kotlin.test.Test
import kotlin.test.assertFailsWith
internal class NettyResumeConnectionTest : AbstractNettyNHTest() {
private val packet = OutgoingPacket("", "", 1, ByteReadPacket.Empty)
@Test
fun `cannot resume on CLOSED`() = runBlockingUnit {
network.setStateClosed()
assertFailsWith<IllegalStateException> {
network.sendWithoutExpect(packet)
}
}
@Test
fun `resumeConnection switches a state that can send packet on INITIALIZED`() = runBlockingUnit {
network.resumeConnection()
network.sendWithoutExpect(packet)
}
@Test
fun `resumeConnection switches a state that can send packet on CONNECTING`() = runBlockingUnit {
network.setStateConnecting()
network.resumeConnection()
network.sendWithoutExpect(packet)
}
@Test
fun `resumeConnection switches a state that can send packet on LOADING`() = runBlockingUnit {
network.setStateLoading(channel)
network.resumeConnection()
network.sendWithoutExpect(packet)
}
}