Skip to content

Commit

Permalink
调整一些测试
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jan 26, 2024
1 parent 7514e21 commit d555fc5
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public inline fun EventListenerRegistrar.process(
* @param typeMismatchResult 事件类型与 [E] 不匹配时的默认返回值
*/
public inline fun <reified E : Event> EventListenerRegistrar.process(
crossinline defaultResult: () -> EventResult = { EventResult.empty() },
propertiesConsumer: ConfigurerFunction<EventListenerRegistrationProperties>? = null,
crossinline defaultResult: () -> EventResult = { EventResult.empty() },
crossinline typeMismatchResult: EventListenerContext.() -> EventResult = { invalid() },
crossinline listenerFunction: suspend EventListenerContext.(E) -> Unit,
): EventListenerRegistrationHandle = register(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ class AtomicTests {
@Test
fun compareAsyncTest() = runTest {
val times = 1000
coroutineScope {
launch(Dispatchers.Default) { checkAtomicInt(times) }
launch(Dispatchers.Default) { checkAtomicLong(times) }
launch(Dispatchers.Default) { checkAtomicUInt(times) }
launch(Dispatchers.Default) { checkAtomicULong(times) }
launch(Dispatchers.Default) { checkAtomicRef(times) }
withContext(Dispatchers.Default) {
coroutineScope {
launch(Dispatchers.Default) { checkAtomicInt(times) }
launch(Dispatchers.Default) { checkAtomicLong(times) }
launch(Dispatchers.Default) { checkAtomicUInt(times) }
launch(Dispatchers.Default) { checkAtomicULong(times) }
launch(Dispatchers.Default) { checkAtomicRef(times) }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

package love.forte.simbot.common.stageloop

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
Expand All @@ -37,26 +39,30 @@ class StageLoopTests {

@Test
fun stageLoopTest() = runTest {
val loop = DefaultStageLoop<TestStage>()
var l1 = 0
var l2 = 0
var d1Done = false
var d2Done = false
val st = Start(onLoop1 = { l1 = it }, onLoop2 = { l2 = it })
loop.appendStage(st)
loop.loop(condition = {
if (it is Done1) {
d1Done = true
}
if (it is Done2) {
d2Done = true
}
it != null
})
assertEquals(l1, 3)
assertEquals(l2, 3)
assertTrue(d1Done)
assertTrue(d2Done)
withContext(Dispatchers.Default) {
val loop = DefaultStageLoop<TestStage>()
var l1 = 0
var l2 = 0
var d1Done = false
var d2Done = false
val st = Start(onLoop1 = { l1 = it }, onLoop2 = { l2 = it })
loop.appendStage(st)

loop.loop(condition = {
if (it is Done1) {
d1Done = true
}
if (it is Done2) {
d2Done = true
}
it != null
})

assertEquals(l1, 3)
assertEquals(l2, 3)
assertTrue(d1Done)
assertTrue(d2Done)
}
}

sealed class TestStage : Stage<TestStage>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

package love.forte.simbot.common.stageloop

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.test.assertEquals

Expand All @@ -35,16 +37,18 @@ import kotlin.test.assertEquals
class StateTests {
@Test
fun stateLoopTest() = runTest {
var time = 0
val last = Start.loop(onEach = {
if (it is Loop) {
time = it.time
}
true
})
withContext(Dispatchers.Default) {
var time = 0
val last = Start.loop(onEach = {
if (it is Loop) {
time = it.time
}
true
})

assertEquals(3, time)
assertEquals(Done, last)
assertEquals(3, time)
assertEquals(Done, last)
}
}

sealed class TestState : State<TestState>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

package love.forte.simbot.suspendrunner

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import love.forte.simbot.annotations.InternalSimbotAPI
import love.forte.simbot.suspendrunner.reserve.deferred
import love.forte.simbot.suspendrunner.reserve.suspendReserve
Expand All @@ -42,9 +44,11 @@ class CommonReserveTests {
@OptIn(InternalSimbotAPI::class)
@Test
fun commonReserveTest() = runTest {
val reserve = suspendReserve(this, EmptyCoroutineContext) { run() }
val value = reserve.transform(deferred()).await()
assertEquals(1, value)
withContext(Dispatchers.Default) {
val reserve = suspendReserve(this, EmptyCoroutineContext) { run() }
val value = reserve.transform(deferred()).await()
assertEquals(1, value)
}
}

private suspend fun run(): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2024. ForteScarlet.
*
* Project https://github.com/simple-robot/simpler-robot
* Email [email protected]
*
* This file is part of the Simple Robot Library.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package love.forte.simbot.core.application

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.test.runTest
import love.forte.simbot.annotations.ExperimentalSimbotAPI
import love.forte.simbot.application.listeners
import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.UUID
import love.forte.simbot.common.time.Timestamp
import love.forte.simbot.event.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNull

/**
*
* @author ForteScarlet
*/
class ApplicationEventDispatcherTests {

@Test
fun launchTest() = runTest {
val app = launchSimpleApplication {
config {
coroutineContext = Dispatchers.Default
}
}

app.listeners {
// first
register({
priority = 0
}) {
assertIs<MyEvent>(event)
EventResult.of(1)
}
// second
listen<MyEvent>({
priority = 1
}) { e ->
assertIs<MyEvent>(event)
assertEquals(event, e)
EventResult.of(2)
}
// third
process<MyEvent>({
priority = 2
}) { e ->
assertIs<MyEvent>(event)
assertEquals(event, e)
}
// forth error
process<MyEvent>({
priority = 3
}) { e ->
assertIs<MyEvent>(event)
assertEquals(event, e)
throw TestErr()
}
}

with(app.eventDispatcher.push(MyEvent()).toList()) {
assertEquals(4, size)
with(get(0)) {
assertIs<StandardEventResult.Simple>(this)
assertEquals(1, content)
}
with(get(1)) {
assertIs<StandardEventResult.Simple>(this)
assertEquals(2, content)
}
with(get(2)) {
assertIs<StandardEventResult.Empty>(this)
assertNull(content)
}
with(get(3)) {
assertIs<StandardEventResult.Error>(this)
assertIs<TestErr>(content)
}

}


}

private class MyEvent : Event {
override val id: ID = UUID.random()

@OptIn(ExperimentalSimbotAPI::class)
override val time: Timestamp = Timestamp.now()
}

private class TestErr : RuntimeException()
}

This file was deleted.

0 comments on commit d555fc5

Please sign in to comment.