From 65a3ffc1474214a1297aa3650e30fd0fda5765bd Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 8 Jun 2021 13:17:36 +0800 Subject: [PATCH] Rename `ComponentScope.plus` to `.withFallback` --- .../network/component/ComponentStorage.kt | 4 ++-- .../network/impl/netty/NettyNetworkHandler.kt | 14 +++++------- .../network/component/CombinedStorageTest.kt | 22 +++++++++---------- .../AbstractRealNetworkHandlerTest.kt | 11 +++++++--- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/network/component/ComponentStorage.kt b/mirai-core/src/commonMain/kotlin/network/component/ComponentStorage.kt index b2c973846..fedd9e9b2 100644 --- a/mirai-core/src/commonMain/kotlin/network/component/ComponentStorage.kt +++ b/mirai-core/src/commonMain/kotlin/network/component/ComponentStorage.kt @@ -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) diff --git a/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt index e76b792da..350dafd6c 100644 --- a/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt @@ -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]. diff --git a/mirai-core/src/commonTest/kotlin/network/component/CombinedStorageTest.kt b/mirai-core/src/commonTest/kotlin/network/component/CombinedStorageTest.kt index 0ea710a41..a815f8492 100644 --- a/mirai-core/src/commonTest/kotlin/network/component/CombinedStorageTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/component/CombinedStorageTest.kt @@ -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)) } } \ No newline at end of file diff --git a/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt b/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt index 2b19408ea..1c1d4367d 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/AbstractRealNetworkHandlerTest.kt @@ -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 : 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 : Abs return NetworkHandlerContextImpl( bot, networkLogger, - observerComponents + components + observerComponents.withFallback(components) ) } @@ -157,5 +158,9 @@ internal abstract class AbstractRealNetworkHandlerTest : Abs assertEquals(state, network.state) } + fun NetworkHandler.assertState(state: State) { + assertEquals(state, this.state) + } + val eventDispatcher get() = bot.components[EventDispatcher] } \ No newline at end of file