Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Him188 2021-01-14 18:00:25 +08:00
commit b49894a4f1
5 changed files with 34 additions and 9 deletions

View File

@ -10,7 +10,7 @@
仅 IntelliJ IDEA 和 Android Studio 支持这个功能。
把光标放在一个错误中间(或者按 F2 自动跳转到错误),使用 `Alt + Enter` 快捷键,会得到弹窗如下图。
把光标放在一个错误中间(或者按 F2 自动跳转到错误),在 Windows 使用 `Alt + Enter` 快捷键macOS 使用 `Option + Enter`,会得到弹窗如下图。
![YBP47V_Z640J_YU5WZ_JVPW.png](https://i.loli.net/2020/12/18/CiX9qApu5BnVPch.png)

View File

@ -12,8 +12,10 @@ package net.mamoe.mirai.event
import kotlinx.coroutines.*
import net.mamoe.mirai.internal.event.GlobalEventListeners
import net.mamoe.mirai.utils.StepUtil
import org.junit.jupiter.api.AfterEach
import java.util.concurrent.Executor
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.Test
import kotlin.test.assertTrue
@ -22,10 +24,16 @@ class TestEvent : AbstractEvent() {
}
class EventTests {
var scope = CoroutineScope(EmptyCoroutineContext)
@AfterEach
fun finiallyReset() {
resetEventListeners()
}
@Test
fun testSubscribeInplace() {
resetEventListeners()
runBlocking {
runBlocking(scope.coroutineContext) {
val subscriber = globalEventChannel().subscribeAlways<TestEvent> {
triggered = true
}
@ -39,11 +47,12 @@ class EventTests {
fun testSubscribeGlobalScope() {
resetEventListeners()
runBlocking {
GlobalScope.globalEventChannel().subscribeAlways<TestEvent> {
val listener = GlobalScope.globalEventChannel().subscribeAlways<TestEvent> {
triggered = true
}
assertTrue(TestEvent().broadcast().triggered)
listener.complete()
}
}
@ -55,7 +64,7 @@ class EventTests {
for (p in EventPriority.values()) {
repeat(2333) {
listeners++
GlobalScope.globalEventChannel().subscribeAlways<ParentEvent> {
scope.globalEventChannel().subscribeAlways<ParentEvent> {
counter.getAndIncrement()
}
}
@ -82,7 +91,7 @@ class EventTests {
launch {
repeat(5000) {
registered.getAndIncrement()
GlobalScope.globalEventChannel().subscribeAlways<ParentEvent>(
scope.globalEventChannel().subscribeAlways<ParentEvent>(
priority = priority
) {
called.getAndIncrement()
@ -135,6 +144,7 @@ class EventTests {
val calledCount = called.get()
val shouldCalled = registered.get() * postCount
supervisor.cancel()
println("Should call $shouldCalled times and $called called")
if (shouldCalled != calledCount) {
@ -188,6 +198,7 @@ class EventTests {
kotlinx.coroutines.runBlocking {
job.join()
}
scope.cancel()
step.throws()
}
@ -216,9 +227,9 @@ class EventTests {
}
*/
fun resetEventListeners() {
for (p in EventPriority.values()) {
GlobalEventListeners[p].clear()
}
scope.cancel()
runBlocking { scope.coroutineContext[Job]?.join() }
scope = CoroutineScope(EmptyCoroutineContext)
}
@Test
@ -236,6 +247,7 @@ class EventTests {
}
ParentEvent().broadcast()
}
resetEventListeners()
}
@Test

View File

@ -11,7 +11,9 @@
package net.mamoe.mirai.event
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.internal.event.GlobalEventListeners
import org.jetbrains.annotations.NotNull
import org.junit.jupiter.api.Test
import java.util.concurrent.atomic.AtomicInteger
@ -97,6 +99,7 @@ internal class JvmMethodEventsTest {
}
assertEquals(9, this.getCalled())
cancel() // reset listeners
}
}
@ -124,6 +127,7 @@ internal class JvmMethodEventsTest {
runBlocking {
TestEvent().broadcast()
}
cancel() // reset listeners
}
}
@ -155,6 +159,7 @@ internal class JvmMethodEventsTest {
runBlocking {
TestEvent().broadcast()
}
cancel() // reset listeners
assertEquals(1, this.getCalled())
}

View File

@ -9,6 +9,7 @@
package net.mamoe.mirai.event
import kotlinx.coroutines.cancel
import net.mamoe.mirai.utils.JavaFriendlyAPI
import net.mamoe.mirai.utils.EventListenerLikeJava
import org.junit.jupiter.api.Test
@ -43,5 +44,6 @@ internal class JvmMethodEventsTestJava : SimpleListenerHost() {
this.globalEventChannel().registerListenerHost(this)
TestEvent().__broadcastJava()
assertEquals(3, called.get(), null)
cancel() // reset listeners
}
}

View File

@ -427,7 +427,13 @@ private fun MessageChain.cleanupRubbishMessageElements(): MessageChain {
}
}
}
add(element)
if (element is PlainText) { // 处理分片消息
append(element.content)
} else {
add(element)
}
previousLast = last
last = element
}