mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-22 13:46:13 +08:00
Rename ComponentScope.plus
to .withFallback
This commit is contained in:
parent
c8def70594
commit
65a3ffc147
@ -18,7 +18,7 @@ import org.jetbrains.annotations.TestOnly
|
||||
*
|
||||
* @see MutableComponentStorage
|
||||
* @see ConcurrentComponentStorage
|
||||
* @see plus
|
||||
* @see withFallback
|
||||
*/
|
||||
internal interface ComponentStorage {
|
||||
@get:TestOnly
|
||||
@ -37,7 +37,7 @@ internal interface ComponentStorage {
|
||||
}
|
||||
}
|
||||
|
||||
internal operator fun ComponentStorage?.plus(fallback: ComponentStorage?): ComponentStorage {
|
||||
internal fun ComponentStorage?.withFallback(fallback: ComponentStorage?): ComponentStorage {
|
||||
if (this == null) return fallback ?: return ComponentStorage.EMPTY
|
||||
if (fallback == null) return this
|
||||
return CombinedComponentStorage(this, fallback)
|
||||
|
@ -204,6 +204,12 @@ internal open class NettyNetworkHandler(
|
||||
// states
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
init {
|
||||
coroutineContext.job.invokeOnCompletion { e ->
|
||||
setState { StateClosed(e?.unwrapCancellationException()) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When state is initialized, it must be set to [_state]. (inside [setState])
|
||||
*
|
||||
@ -214,14 +220,6 @@ internal open class NettyNetworkHandler(
|
||||
protected abstract inner class NettyState(
|
||||
correspondingState: State
|
||||
) : BaseStateImpl(correspondingState) {
|
||||
init {
|
||||
coroutineContext.job.invokeOnCompletion { e ->
|
||||
if (correspondingState != State.CLOSED) {
|
||||
if (e != null) setState { StateClosed(e.unwrapCancellationException()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return `true` if packet has been sent, `false` if state is not ready for send.
|
||||
* @throws IllegalStateException if is [StateClosed].
|
||||
|
@ -20,15 +20,15 @@ internal class CombinedStorageTest : AbstractTest() {
|
||||
fun `can get from main`() {
|
||||
val storage = ConcurrentComponentStorage().apply {
|
||||
set(TestComponent2, TestComponent2(1))
|
||||
} + ConcurrentComponentStorage()
|
||||
}.withFallback(ConcurrentComponentStorage())
|
||||
assertEquals(TestComponent2(1), storage[TestComponent2])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can get from fallback`() {
|
||||
val storage = ConcurrentComponentStorage() + ConcurrentComponentStorage().apply {
|
||||
val storage = ConcurrentComponentStorage().withFallback(ConcurrentComponentStorage().apply {
|
||||
set(TestComponent2, TestComponent2(1))
|
||||
}
|
||||
})
|
||||
assertEquals(TestComponent2(1), storage[TestComponent2])
|
||||
}
|
||||
|
||||
@ -36,15 +36,15 @@ internal class CombinedStorageTest : AbstractTest() {
|
||||
fun `size is sum of sizes of two storages`() {
|
||||
val storage = ConcurrentComponentStorage().apply {
|
||||
set(TestComponent3, TestComponent3(1))
|
||||
} + ConcurrentComponentStorage().apply {
|
||||
}.withFallback(ConcurrentComponentStorage().apply {
|
||||
set(TestComponent2, TestComponent2(1))
|
||||
}
|
||||
})
|
||||
assertEquals(2, storage.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `size can be zero`() {
|
||||
val storage = ConcurrentComponentStorage() + ConcurrentComponentStorage()
|
||||
val storage = ConcurrentComponentStorage().withFallback(ConcurrentComponentStorage())
|
||||
assertEquals(0, storage.size)
|
||||
}
|
||||
|
||||
@ -52,27 +52,27 @@ internal class CombinedStorageTest : AbstractTest() {
|
||||
fun `main prevails than fallback`() {
|
||||
val storage = ConcurrentComponentStorage().apply {
|
||||
set(TestComponent2, TestComponent2(1))
|
||||
} + ConcurrentComponentStorage().apply {
|
||||
}.withFallback(ConcurrentComponentStorage().apply {
|
||||
set(TestComponent2, TestComponent2(2))
|
||||
}
|
||||
})
|
||||
assertEquals(TestComponent2(1), storage[TestComponent2])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns empty if both are null`() {
|
||||
val x: ComponentStorage = null + null
|
||||
val x: ComponentStorage = null.withFallback(null)
|
||||
assertEquals(ComponentStorage.EMPTY, x)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns first if second if null`() {
|
||||
val x = ConcurrentComponentStorage().apply { set(TestComponent2, TestComponent2(1)) }
|
||||
assertSame(x, x + null)
|
||||
assertSame(x, x.withFallback(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns second if first if null`() {
|
||||
val x = ConcurrentComponentStorage().apply { set(TestComponent2, TestComponent2(1)) }
|
||||
assertSame(x, null + x)
|
||||
assertSame(x, null.withFallback(x))
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ import net.mamoe.mirai.internal.MockBot
|
||||
import net.mamoe.mirai.internal.QQAndroidBot
|
||||
import net.mamoe.mirai.internal.network.component.ComponentStorage
|
||||
import net.mamoe.mirai.internal.network.component.ConcurrentComponentStorage
|
||||
import net.mamoe.mirai.internal.network.component.plus
|
||||
import net.mamoe.mirai.internal.network.component.withFallback
|
||||
import net.mamoe.mirai.internal.network.components.*
|
||||
import net.mamoe.mirai.internal.network.context.SsoProcessorContext
|
||||
import net.mamoe.mirai.internal.network.context.SsoProcessorContextImpl
|
||||
@ -131,7 +131,8 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
|
||||
|
||||
open fun createContext(additionalComponents: ComponentStorage? = null): NetworkHandlerContextImpl {
|
||||
// StateObserver
|
||||
val components = additionalComponents + defaultComponents + bot.createDefaultComponents()
|
||||
val components =
|
||||
additionalComponents.withFallback(defaultComponents).withFallback(bot.createDefaultComponents())
|
||||
val observerComponents = if (
|
||||
additionalComponents?.getOrNull(StateObserver)
|
||||
?: defaultComponents.getOrNull(StateObserver)
|
||||
@ -145,7 +146,7 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
|
||||
return NetworkHandlerContextImpl(
|
||||
bot,
|
||||
networkLogger,
|
||||
observerComponents + components
|
||||
observerComponents.withFallback(components)
|
||||
)
|
||||
}
|
||||
|
||||
@ -157,5 +158,9 @@ internal abstract class AbstractRealNetworkHandlerTest<H : NetworkHandler> : Abs
|
||||
assertEquals(state, network.state)
|
||||
}
|
||||
|
||||
fun NetworkHandler.assertState(state: State) {
|
||||
assertEquals(state, this.state)
|
||||
}
|
||||
|
||||
val eventDispatcher get() = bot.components[EventDispatcher]
|
||||
}
|
Loading…
Reference in New Issue
Block a user