From 63fc45edd6ca81e9f09e9d0d9e756b96e8722c38 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 12 Sep 2021 22:24:55 +0800 Subject: [PATCH] Try fix EventLaunchUndispatchedTest --- .../event/EventLaunchUndispatchedTest.kt | 25 +++++++++++++------ mirai-core/src/commonMain/kotlin/MiraiImpl.kt | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/mirai-core-api/src/jvmTest/kotlin/event/EventLaunchUndispatchedTest.kt b/mirai-core-api/src/jvmTest/kotlin/event/EventLaunchUndispatchedTest.kt index e936eb6a1..f3cb173b5 100644 --- a/mirai-core-api/src/jvmTest/kotlin/event/EventLaunchUndispatchedTest.kt +++ b/mirai-core-api/src/jvmTest/kotlin/event/EventLaunchUndispatchedTest.kt @@ -14,8 +14,10 @@ package net.mamoe.mirai.event import kotlinx.coroutines.* import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.internal.event.EVENT_LAUNCH_UNDISPATCHED +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import java.util.concurrent.ConcurrentLinkedQueue +import java.util.concurrent.Executors import kotlin.test.assertEquals import kotlin.test.assertFails import kotlin.test.assertSame @@ -24,10 +26,11 @@ import kotlin.test.assertSame internal class EventLaunchUndispatchedTest : AbstractEventTest() { internal class TestEvent : AbstractEvent() - val originalValue = EVENT_LAUNCH_UNDISPATCHED + var originalValue = EVENT_LAUNCH_UNDISPATCHED @Test suspend fun `event runs undispatched`() { + originalValue = EVENT_LAUNCH_UNDISPATCHED EVENT_LAUNCH_UNDISPATCHED = true doTest() EVENT_LAUNCH_UNDISPATCHED = originalValue @@ -35,26 +38,34 @@ internal class EventLaunchUndispatchedTest : AbstractEventTest() { @Test suspend fun `event runs undispatched fail`() { + originalValue = EVENT_LAUNCH_UNDISPATCHED EVENT_LAUNCH_UNDISPATCHED = false assertFails { doTest() } EVENT_LAUNCH_UNDISPATCHED = originalValue } + private val dispatcher = Executors.newFixedThreadPool(4).asCoroutineDispatcher() + + @AfterEach + fun cleanup() { + dispatcher.close() + } + private suspend fun doTest() = coroutineScope { val invoked = ConcurrentLinkedQueue() val thread = Thread.currentThread() val job = SupervisorJob() - globalEventChannel().parentJob(job).exceptionHandler { } // printing exception to stdout is very slow + globalEventChannel(dispatcher).parentJob(job).exceptionHandler {} // printing exception to stdout is very slow .run { - subscribeAlways(priority = EventPriority.MONITOR) { + subscribeAlways(dispatcher, priority = EventPriority.MONITOR) { assertSame(thread, Thread.currentThread()) invoked.add(1) awaitCancellation() } - repeat(10000) { i -> - subscribeAlways(priority = EventPriority.MONITOR) { + repeat(1000) { i -> + subscribeAlways(dispatcher, priority = EventPriority.MONITOR) { assertSame(thread, Thread.currentThread()) invoked.add(i + 2) awaitCancellation() @@ -62,13 +73,13 @@ internal class EventLaunchUndispatchedTest : AbstractEventTest() { } } - launch(start = CoroutineStart.UNDISPATCHED) { TestEvent().broadcast() } + launch(dispatcher, start = CoroutineStart.UNDISPATCHED) { TestEvent().broadcast() } // `launch` returns on first suspension point of `broadcast` // if EVENT_LAUNCH_UNDISPATCHED is `true`, all listeners run to `awaitCancellation` when `broadcast` is suspended // otherwise, they are put into tasks queue to be scheduled. 10000 tasks wont complete very quickly, so the following `invoked.size` check works. assertSame(thread, Thread.currentThread()) assertEquals(invoked.toList(), invoked.sorted()) - assertEquals(10000 + 1, invoked.size) + assertEquals(1000 + 1, invoked.size) job.cancel() } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt index e0372b3c2..ad6ff82cd 100644 --- a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt +++ b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt @@ -310,7 +310,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor { } if (event is BotEvent) { val bot = event.bot - if (bot is QQAndroidBot) { + if (bot is AbstractBot) { bot.components[EventDispatcher].broadcast(event) } } else {