Fix tests

This commit is contained in:
Him188 2021-06-06 13:57:59 +08:00
parent 38d0b124f2
commit e7521028d1
4 changed files with 11 additions and 47 deletions

View File

@ -152,4 +152,5 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
assertEquals(state, network.state)
}
val eventDispatcher get() = bot.components[EventDispatcher]
}

View File

@ -60,6 +60,15 @@ internal class SelectorNetworkHandlerTest : AbstractRealNetworkHandlerTest<Selec
*/
@Test
fun `can recover on heartbeat failure`() = runBlockingUnit {
testReceiver { HeartbeatFailedException("test", null) } // NetworkException
}
@Test
fun `cannot recover on other failures`() = runBlockingUnit {
testReceiver { IllegalStateException() }
}
private suspend fun testReceiver(exception: () -> Exception) {
val heartbeatScheduler = object : HeartbeatScheduler {
lateinit var onHeartFailure: HeartbeatFailureHandler
override fun launchJobsIn(
@ -77,7 +86,7 @@ internal class SelectorNetworkHandlerTest : AbstractRealNetworkHandlerTest<Selec
bot.network.context[EventDispatcher].joinBroadcast()
assertState(NetworkHandler.State.OK)
heartbeatScheduler.onHeartFailure("Test", HeartbeatFailedException("test", null))
heartbeatScheduler.onHeartFailure("Test", exception())
assertState(NetworkHandler.State.CLOSED)
bot.network.resumeConnection() // in real, this is called by BotOnlineWatchdog in SelectorNetworkHandler

View File

@ -10,19 +10,10 @@
package net.mamoe.mirai.internal.network.impl.netty
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.launch
import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.events.BotOfflineEvent
import net.mamoe.mirai.event.events.BotReloginEvent
import net.mamoe.mirai.event.nextEvent
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 net.mamoe.mirai.utils.firstIsInstanceOrNull
import org.junit.jupiter.api.Test
import java.io.IOException
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
internal class NettyBotNormalLoginTest : AbstractNettyNHTest() {
class CusLoginException(message: String?) : RuntimeException(message)
@ -44,24 +35,4 @@ internal class NettyBotNormalLoginTest : AbstractNettyNHTest() {
bot.login()
}
}
@Test
fun `test errors after logon`() = runBlockingUnit {
bot.login()
eventDispatcher.joinBroadcast()
assertEventBroadcasts<BotEvent>(-1) {
launch {
eventDispatcher.joinBroadcast()
channel.pipeline().fireExceptionCaught(CusLoginException("Net error"))
}
assertNotNull(
nextEvent<BotReloginEvent>(5000) { it.bot === bot }
)
}.let { events ->
assertFailsWith<CusLoginException>("Net error") {
throw events.firstIsInstanceOrNull<BotOfflineEvent.Dropped>()!!.cause!!
}
}
assertState(OK)
}
}

View File

@ -10,7 +10,6 @@
package net.mamoe.mirai.internal.network.impl.netty
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.awaitCancellation
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.BotOfflineEvent
@ -72,22 +71,6 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() {
}
@Test
fun `from OK TO CONNECTING`() = runBlockingUnit {
setSsoProcessor {
awaitCancellation()
}
assertState(INITIALIZED)
network.setStateOK(channel)
eventDispatcher.joinBroadcast() // ignore events
assertEventBroadcasts<Event>(1) {
network.setStateConnecting()
eventDispatcher.joinBroadcast()
}.let { event ->
assertEquals(BotOfflineEvent.Dropped::class, event[0]::class)
}
}
@Test
fun `from CONNECTING TO OK the first time`() = runBlockingUnit {
val ok = CompletableDeferred<Unit>()