mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-07 23:49:21 +08:00
Migrate use of GlobalEventChannel
s in tests to use scoped ones
This commit is contained in:
parent
c61a79e299
commit
4c28913a27
@ -13,14 +13,11 @@ import kotlinx.coroutines.*
|
|||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.receiveAsFlow
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
import kotlinx.coroutines.sync.Semaphore
|
|
||||||
import net.mamoe.mirai.event.*
|
import net.mamoe.mirai.event.*
|
||||||
import net.mamoe.mirai.event.events.FriendEvent
|
import net.mamoe.mirai.event.events.FriendEvent
|
||||||
import net.mamoe.mirai.event.events.GroupEvent
|
import net.mamoe.mirai.event.events.GroupEvent
|
||||||
import net.mamoe.mirai.event.events.GroupMessageEvent
|
import net.mamoe.mirai.event.events.GroupMessageEvent
|
||||||
import net.mamoe.mirai.event.events.MessageEvent
|
import net.mamoe.mirai.event.events.MessageEvent
|
||||||
import org.junit.jupiter.api.AfterEach
|
|
||||||
import org.junit.jupiter.api.BeforeEach
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import kotlin.coroutines.coroutineContext
|
import kotlin.coroutines.coroutineContext
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
@ -38,23 +35,11 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
val y: Int = 1,
|
val y: Int = 1,
|
||||||
) : AbstractEvent()
|
) : AbstractEvent()
|
||||||
|
|
||||||
val semaphore = Semaphore(1)
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
fun x() {
|
|
||||||
runBlocking { semaphore.acquire() }
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
fun s() {
|
|
||||||
EventChannelToEventDispatcherAdapter.instance.eventListeners.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun singleFilter() {
|
fun singleFilter() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val received = suspendCoroutine<Int> { cont ->
|
val received = suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.filterIsInstance<TE>()
|
.filterIsInstance<TE>()
|
||||||
.filter {
|
.filter {
|
||||||
true
|
true
|
||||||
@ -86,7 +71,7 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
fun multipleFilters() {
|
fun multipleFilters() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val received = suspendCoroutine<Int> { cont ->
|
val received = suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.filterIsInstance<TE>()
|
.filterIsInstance<TE>()
|
||||||
.filter {
|
.filter {
|
||||||
true
|
true
|
||||||
@ -124,9 +109,9 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
@Test
|
@Test
|
||||||
fun multipleContexts1() {
|
fun multipleContexts1() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
withContext(CoroutineName("1")) {
|
||||||
val received = suspendCoroutine<Int> { cont ->
|
val received = suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.parentScope(CoroutineScope(CoroutineName("1")))
|
|
||||||
.context(CoroutineName("2"))
|
.context(CoroutineName("2"))
|
||||||
.context(CoroutineName("3"))
|
.context(CoroutineName("3"))
|
||||||
.subscribeOnce<TE>(CoroutineName("4")) {
|
.subscribeOnce<TE>(CoroutineName("4")) {
|
||||||
@ -142,13 +127,14 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
assertEquals(2, received)
|
assertEquals(2, received)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun multipleContexts2() {
|
fun multipleContexts2() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
withContext(CoroutineName("1")) {
|
||||||
val received = suspendCoroutine<Int> { cont ->
|
val received = suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.parentScope(CoroutineScope(CoroutineName("1")))
|
|
||||||
.context(CoroutineName("2"))
|
.context(CoroutineName("2"))
|
||||||
.context(CoroutineName("3"))
|
.context(CoroutineName("3"))
|
||||||
.subscribeOnce<TE> {
|
.subscribeOnce<TE> {
|
||||||
@ -164,14 +150,15 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
assertEquals(2, received)
|
assertEquals(2, received)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun multipleContexts3() {
|
fun multipleContexts3() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
withContext(CoroutineName("1")) {
|
||||||
val received = suspendCoroutine<Int> { cont ->
|
val received = suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.parentScope(CoroutineScope(CoroutineName("1")))
|
|
||||||
.context(CoroutineName("2"))
|
.context(CoroutineName("2"))
|
||||||
.subscribeOnce<TE> {
|
.subscribeOnce<TE> {
|
||||||
assertEquals("2", currentCoroutineContext()[CoroutineName]!!.name)
|
assertEquals("2", currentCoroutineContext()[CoroutineName]!!.name)
|
||||||
@ -186,13 +173,14 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
assertEquals(2, received)
|
assertEquals(2, received)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun multipleContexts4() {
|
fun multipleContexts4() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
withContext(CoroutineName("1")) {
|
||||||
val received = suspendCoroutine<Int> { cont ->
|
val received = suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.parentScope(CoroutineScope(CoroutineName("1")))
|
|
||||||
.subscribeOnce<TE> {
|
.subscribeOnce<TE> {
|
||||||
assertEquals("1", currentCoroutineContext()[CoroutineName]!!.name)
|
assertEquals("1", currentCoroutineContext()[CoroutineName]!!.name)
|
||||||
cont.resume(it.x)
|
cont.resume(it.x)
|
||||||
@ -206,37 +194,13 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
assertEquals(2, received)
|
assertEquals(2, received)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAsChannel() {
|
|
||||||
runBlocking {
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
val channel = GlobalEventChannel
|
|
||||||
.filterIsInstance<TE>()
|
|
||||||
.filter { true }
|
|
||||||
.filter { it.x == 2 }
|
|
||||||
.filter { true }
|
|
||||||
.asChannel(Channel.BUFFERED)
|
|
||||||
|
|
||||||
println("Broadcast 1")
|
|
||||||
TE(1).broadcast()
|
|
||||||
println("Broadcast 2")
|
|
||||||
TE(2).broadcast()
|
|
||||||
println("Broadcast done")
|
|
||||||
channel.close()
|
|
||||||
|
|
||||||
val list = channel.receiveAsFlow().toList()
|
|
||||||
|
|
||||||
assertEquals(1, list.size)
|
|
||||||
assertEquals(TE(2), list.single())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `test forwardToChannel`() {
|
fun `test forwardToChannel`() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val channel = Channel<TE>(Channel.BUFFERED)
|
val channel = Channel<TE>(Channel.BUFFERED)
|
||||||
val listener = GlobalEventChannel
|
val listener = globalEventChannel()
|
||||||
.filterIsInstance<TE>()
|
.filterIsInstance<TE>()
|
||||||
.filter { true }
|
.filter { true }
|
||||||
.filter { it.x == 2 }
|
.filter { it.x == 2 }
|
||||||
@ -261,7 +225,7 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
fun `test forwardToChannel listener completes if channel closed`() {
|
fun `test forwardToChannel listener completes if channel closed`() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val channel = Channel<TE>(Channel.BUFFERED)
|
val channel = Channel<TE>(Channel.BUFFERED)
|
||||||
val listener = GlobalEventChannel
|
val listener = globalEventChannel()
|
||||||
.filterIsInstance<TE>()
|
.filterIsInstance<TE>()
|
||||||
.filter { true }
|
.filter { true }
|
||||||
.filter { it.x == 2 }
|
.filter { it.x == 2 }
|
||||||
@ -285,10 +249,10 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testExceptionInFilter() {
|
fun testExceptionInFilter() {
|
||||||
runBlocking {
|
|
||||||
assertFailsWith<ExceptionInEventChannelFilterException> {
|
assertFailsWith<ExceptionInEventChannelFilterException> {
|
||||||
|
runBlocking {
|
||||||
suspendCoroutine<Int> { cont ->
|
suspendCoroutine<Int> { cont ->
|
||||||
GlobalEventChannel
|
globalEventChannel()
|
||||||
.exceptionHandler {
|
.exceptionHandler {
|
||||||
cont.resumeWithException(it)
|
cont.resumeWithException(it)
|
||||||
}
|
}
|
||||||
@ -305,11 +269,11 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
println("Broadcast done")
|
println("Broadcast done")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}.run {
|
}.run {
|
||||||
assertEquals("test error", cause.message)
|
assertEquals("test error", cause.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testExceptionInSubscribe() {
|
fun testExceptionInSubscribe() {
|
||||||
@ -320,7 +284,7 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
cont.resumeWithException(throwable)
|
cont.resumeWithException(throwable)
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalEventChannel
|
val listener = globalEventChannel()
|
||||||
.context(handler)
|
.context(handler)
|
||||||
.subscribeOnce<TE> {
|
.subscribeOnce<TE> {
|
||||||
assertSame(handler, currentCoroutineContext()[CoroutineExceptionHandler])
|
assertSame(handler, currentCoroutineContext()[CoroutineExceptionHandler])
|
||||||
@ -331,6 +295,7 @@ internal class EventChannelTest : AbstractEventTest() {
|
|||||||
println("Broadcast 1")
|
println("Broadcast 1")
|
||||||
TE(1).broadcast()
|
TE(1).broadcast()
|
||||||
println("Broadcast done")
|
println("Broadcast done")
|
||||||
|
listener.complete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.run {
|
}.run {
|
||||||
|
@ -143,7 +143,7 @@ internal class JvmMethodEventsTest : AbstractEventTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestClass().run {
|
TestClass().run {
|
||||||
GlobalEventChannel.registerListenerHost(this)
|
globalEventChannel().registerListenerHost(this)
|
||||||
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
TestEvent().broadcast()
|
TestEvent().broadcast()
|
||||||
@ -215,7 +215,7 @@ internal class JvmMethodEventsTest : AbstractEventTest() {
|
|||||||
}
|
}
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
|
||||||
TestingListener().runTesting {
|
TestingListener().runTesting(this.globalEventChannel()) {
|
||||||
TestEvent().broadcast()
|
TestEvent().broadcast()
|
||||||
assertTrue { handled }
|
assertTrue { handled }
|
||||||
}
|
}
|
||||||
@ -245,7 +245,7 @@ internal class JvmMethodEventsTest : AbstractEventTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <T> T.runTesting(
|
private inline fun <T> T.runTesting(
|
||||||
channel: EventChannel<*> = GlobalEventChannel,
|
channel: EventChannel<*>,
|
||||||
block: T.(T) -> Unit
|
block: T.(T) -> Unit
|
||||||
) where T : SimpleListenerHost {
|
) where T : SimpleListenerHost {
|
||||||
contract {
|
contract {
|
||||||
|
@ -12,7 +12,6 @@ package net.mamoe.mirai.internal.event
|
|||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import net.mamoe.mirai.event.*
|
import net.mamoe.mirai.event.*
|
||||||
import net.mamoe.mirai.utils.JavaFriendlyAPI
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
@ -20,11 +19,10 @@ import kotlin.test.assertEquals
|
|||||||
internal class JvmMethodEventsTestJava : AbstractEventTest() {
|
internal class JvmMethodEventsTestJava : AbstractEventTest() {
|
||||||
private val called = AtomicInteger(0)
|
private val called = AtomicInteger(0)
|
||||||
|
|
||||||
@OptIn(JavaFriendlyAPI::class)
|
|
||||||
@Test
|
@Test
|
||||||
fun test() {
|
fun test() = runBlocking {
|
||||||
val host = TestHost(called)
|
val host = TestHost(called)
|
||||||
GlobalEventChannel.registerListenerHost(host)
|
globalEventChannel().registerListenerHost(host)
|
||||||
runBlocking { TestEvent().broadcast() }
|
runBlocking { TestEvent().broadcast() }
|
||||||
assertEquals(3, called.get(), null)
|
assertEquals(3, called.get(), null)
|
||||||
host.cancel() // reset listeners
|
host.cancel() // reset listeners
|
||||||
|
Loading…
Reference in New Issue
Block a user