mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-21 17:24:14 +08:00
Unregister listeners after test completed
This commit is contained in:
parent
b9635404a5
commit
70ffca5138
@ -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
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user