Add more tests for EventChannel:

- testFilterException
- testExceptionInSubscribe
This commit is contained in:
Him188 2021-02-03 11:42:18 +08:00
parent 0bbea5706f
commit b5d5f3c922
2 changed files with 78 additions and 0 deletions

View File

@ -76,6 +76,12 @@ internal class ListenerRegistry(
internal object GlobalEventListeners {
private val ALL_LEVEL_REGISTRIES: Map<EventPriority, ConcurrentLinkedQueue<ListenerRegistry>>
fun clear() {
ALL_LEVEL_REGISTRIES.forEach { (_, u) ->
u.clear()
}
}
init {
val map =
EnumMap<EventPriority, ConcurrentLinkedQueue<ListenerRegistry>>(EventPriority::class.java)

View File

@ -9,13 +9,20 @@
package net.mamoe.mirai.event
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore
import net.mamoe.mirai.event.events.FriendEvent
import net.mamoe.mirai.event.events.GroupEvent
import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.internal.event.GlobalEventListeners
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.platform.commons.annotation.Testable
import java.lang.IllegalStateException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
@ -32,6 +39,19 @@ internal class EventChannelTest {
val x: Int
) : AbstractEvent()
val semaphore = Semaphore(1)
@BeforeEach
fun x() {
runBlocking { semaphore.acquire() }
}
@AfterEach
fun s(){
GlobalEventListeners.clear()
runBlocking { semaphore.release() }
}
@Test
fun testFilter() {
runBlocking {
@ -64,6 +84,58 @@ internal class EventChannelTest {
}
}
@Test
fun testExceptionInFilter() {
runBlocking {
assertFailsWith<ExceptionInEventChannelFilterException> {
suspendCoroutine<Int> { cont ->
GlobalEventChannel
.exceptionHandler {
cont.resumeWithException(it)
}
.filter {
error("test error")
}
.subscribeOnce<TE> {
cont.resume(it.x)
}
launch {
println("Broadcast 1")
TE(1).broadcast()
println("Broadcast done")
}
}
}.run {
assertEquals("test error", cause.message)
}
}
}
@Test
fun testExceptionInSubscribe() {
runBlocking {
assertFailsWith<IllegalStateException> {
suspendCoroutine<Int> { cont ->
GlobalEventChannel
.exceptionHandler {
cont.resumeWithException(it)
}
.subscribeOnce<TE> {
error("test error")
}
launch {
println("Broadcast 1")
TE(1).broadcast()
println("Broadcast done")
}
}
}.run {
assertEquals("test error", message)
}
}
}
@Suppress("UNUSED_VARIABLE")
@Test