Fix tests

tests

Fix StateObserver error
This commit is contained in:
Karlatemp 2021-05-20 20:07:48 +08:00 committed by Him188
parent 6f8d15ed37
commit 76eaa34917
6 changed files with 50 additions and 35 deletions

View File

@ -36,7 +36,7 @@ internal abstract class AbstractMockNetworkHandlerTest : AbstractTest() {
protected val bot: QQAndroidBot = MockBot {
nhProvider = { createNetworkHandler() }
componentsProvider = { components }
componentsProvider = { this@AbstractMockNetworkHandlerTest.components }
}
protected val logger = MiraiLogger.create("test")
protected val components = ConcurrentComponentStorage().apply {

View File

@ -109,18 +109,29 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
override fun attachJob(bot: AbstractBot, scope: CoroutineScope) {
}
})
set(StateObserver, bot.run { stateObserverChain() })
// set(StateObserver, bot.run { stateObserverChain() })
}
/**
* [additionalComponents] overrides [defaultComponents] and [QQAndroidBot.components]
*/
open fun createHandler(additionalComponents: ComponentStorage? = null): H {
// StateObserver
val components = additionalComponents + defaultComponents + bot.createDefaultComponents()
val observerComponents = if (
additionalComponents?.getOrNull(StateObserver)
?: defaultComponents.getOrNull(StateObserver)
== null
) {
ConcurrentComponentStorage().apply {
set(StateObserver, bot.run { components.stateObserverChain() })
}
} else null
return factory.create(
NetworkHandlerContextImpl(
bot,
networkLogger,
additionalComponents + defaultComponents + bot.createDefaultComponents()
observerComponents + components
),
InetSocketAddress.createUnresolved("localhost", 123)
)

View File

@ -20,6 +20,9 @@ import net.mamoe.mirai.utils.TestOnly
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicInteger
/**
* States are manually set.
*/
internal open class TestNetworkHandler(
override val bot: QQAndroidBot,
context: NetworkHandlerContext,
@ -63,16 +66,19 @@ internal open class TestNetworkHandler(
override fun setStateClosed(exception: Throwable?) {
setState(NetworkHandler.State.CLOSED)
}
override fun setStateConnecting(exception: Throwable?) {
setState(NetworkHandler.State.CONNECTING)
}
override fun setStateOK(channel: Channel, exception: Throwable?) {
setState(NetworkHandler.State.OK)
exception?.printStackTrace()
}
override fun setStateLoading(channel: Channel) {
setState(NetworkHandler.State.LOADING)
}
}

View File

@ -9,17 +9,13 @@
package net.mamoe.mirai.internal.network.handler
import net.mamoe.mirai.event.events.BotReloginEvent
import net.mamoe.mirai.internal.network.framework.AbstractMockNetworkHandlerTest
import net.mamoe.mirai.internal.network.framework.eventDispatcher
import net.mamoe.mirai.internal.network.framework.ssoProcessor
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State
import net.mamoe.mirai.internal.network.handler.selector.AbstractKeepAliveNetworkHandlerSelector
import net.mamoe.mirai.internal.test.assertEventBroadcasts
import net.mamoe.mirai.internal.test.runBlockingUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.*
import kotlin.time.seconds
import kotlin.time.Duration
private class TestSelector :
AbstractKeepAliveNetworkHandlerSelector<NetworkHandler> {
@ -50,7 +46,7 @@ internal class KeepAliveNetworkHandlerSelectorTest : AbstractMockNetworkHandlerT
setState(State.OK)
}
}
runBlockingUnit(timeout = 1.seconds) { selector.awaitResumeInstance() }
runBlockingUnit(timeout = Duration.seconds(1)) { selector.awaitResumeInstance() }
assertNotNull(selector.getResumedInstance())
}
@ -73,7 +69,7 @@ internal class KeepAliveNetworkHandlerSelectorTest : AbstractMockNetworkHandlerT
selector.setCurrent(handler)
assertSame(handler, selector.getResumedInstance())
handler.setState(State.CLOSED)
runBlockingUnit(timeout = 3.seconds) { selector.awaitResumeInstance() }
runBlockingUnit(timeout = Duration.seconds(3)) { selector.awaitResumeInstance() }
assertEquals(1, selector.createInstanceCount.get())
}
@ -87,20 +83,4 @@ internal class KeepAliveNetworkHandlerSelectorTest : AbstractMockNetworkHandlerT
}
assertEquals(3, selector.createInstanceCount.get())
}
@Test
fun `BotReloginEvent after successful reconnection`() = runBlockingUnit {
val network = createNetworkHandler()
assertEventBroadcasts<BotReloginEvent> {
assertEquals(State.INITIALIZED, network.state)
bot.login()
network.ssoProcessor.firstLoginSucceed = true
network.setStateConnecting()
network.resumeConnection()
network.eventDispatcher.joinBroadcast() // `login` launches a job which broadcasts the event
assertEquals(State.OK, network.state)
}
}
}

View File

@ -17,9 +17,11 @@ 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.State
import net.mamoe.mirai.internal.network.framework.eventDispatcher
import net.mamoe.mirai.internal.network.framework.ssoProcessor
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State.*
import net.mamoe.mirai.internal.test.assertEventBroadcasts
import net.mamoe.mirai.internal.test.assertEventNotBroadcast
import net.mamoe.mirai.internal.test.runBlockingUnit
import org.junit.jupiter.api.TestInstance
import java.util.concurrent.atomic.AtomicReference
@ -65,7 +67,7 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() {
assertEventBroadcasts<BotOfflineEvent>(1) {
network.setStateClosed()
eventDispatcher.joinBroadcast()
assertEquals(State.CLOSED, network.state)
assertState(CLOSED)
}
}
@ -125,6 +127,8 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() {
ok.get().complete(Unit)
network.resumeConnection()
eventDispatcher.joinBroadcast()
delay(5000) // BotReloginEvent broadcast not started now
eventDispatcher.joinBroadcast()
}.let { event ->
assertEquals(BotOnlineEvent::class, event[0]::class)
assertEquals(BotReloginEvent::class, event[1]::class)
@ -152,17 +156,31 @@ internal class NettyHandlerEventTest : AbstractNettyNHTest() {
}
@Test
fun `BotOffline from CONNECTING TO CLOSED`() = runBlockingUnit {
fun `no BotOffline from CONNECTING TO CLOSED`() = runBlockingUnit {
network.setStateConnecting()
eventDispatcher.joinBroadcast() // `login` launches a job which broadcasts the event
assertEventBroadcasts<Event>(1) {
assertEventNotBroadcast {
network.setStateClosed()
network.resumeConnection()
eventDispatcher.joinBroadcast()
}.let { event ->
assertEquals(BotOfflineEvent.Active::class, event[0]::class)
}
}
@Test
fun `BotReloginEvent after successful reconnection`() = runBlockingUnit {
assertEventBroadcasts<BotReloginEvent> {
assertState(INITIALIZED)
bot.login()
assertState(OK)
network.ssoProcessor.firstLoginSucceed = true
network.setStateConnecting()
network.resumeConnection()
assertState(OK)
network.eventDispatcher.joinBroadcast()
assertState(OK)
}
}
//
// @Test
// fun `no event from INITIALIZED TO OK`() = runBlockingUnit {

View File

@ -55,6 +55,6 @@ internal inline fun <reified T : Event> assertEventBroadcasts(times: Int = 1, bl
}
@OptIn(ExperimentalCoroutinesApi::class)
internal inline fun <reified T : Event, R> assertEventNotBroadcast(block: () -> R): R {
return assertEventBroadcasts<T, R>(0, block)
internal inline fun <R> assertEventNotBroadcast(block: () -> R): R {
return assertEventBroadcasts<Event, R>(0, block)
}