mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-25 03:30:15 +08:00
Tentatively fix tests regarding initialization of multiple Mirai Console instances
This commit is contained in:
parent
66367c893c
commit
0675ccbbd8
@ -246,8 +246,9 @@ public interface MiraiConsoleImplementation : CoroutineScope {
|
||||
override val resolvedPlugins: MutableList<Plugin> get() = PluginManagerImpl.resolvedPlugins
|
||||
}
|
||||
|
||||
internal lateinit var instance: MiraiConsoleImplementation
|
||||
internal val instanceInitialized: Boolean get() = ::instance.isInitialized
|
||||
@Volatile
|
||||
internal var instance: MiraiConsoleImplementation? = null
|
||||
internal val instanceInitialized: Boolean get() = instance != null
|
||||
private val initLock = ReentrantLock()
|
||||
|
||||
/**
|
||||
@ -257,14 +258,15 @@ public interface MiraiConsoleImplementation : CoroutineScope {
|
||||
*/
|
||||
@JvmStatic
|
||||
@ConsoleFrontEndImplementation
|
||||
public fun getInstance(): MiraiConsoleImplementation = instance
|
||||
public fun getInstance(): MiraiConsoleImplementation = instance ?: throw UninitializedPropertyAccessException()
|
||||
|
||||
/** 由前端调用, 初始化 [MiraiConsole] 实例并启动 */
|
||||
@JvmStatic
|
||||
@ConsoleFrontEndImplementation
|
||||
@Throws(MalformedMiraiConsoleImplementationError::class)
|
||||
public fun MiraiConsoleImplementation.start(): Unit = initLock.withLock {
|
||||
if (::instance.isInitialized && instance.isActive) {
|
||||
val instance = instance
|
||||
if (instance != null && instance.isActive) {
|
||||
error(
|
||||
"Mirai Console is already initialized and is currently running. " +
|
||||
"Run MiraiConsole.cancel to kill old instance before starting another instance."
|
||||
|
@ -70,7 +70,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
|
||||
MiraiConsole {
|
||||
override val pluginCenter: PluginCenter get() = throw UnsupportedOperationException("PluginCenter is not supported yet")
|
||||
|
||||
private val instance: MiraiConsoleImplementation by MiraiConsoleImplementation.Companion::instance
|
||||
private val instance: MiraiConsoleImplementation get() = MiraiConsoleImplementation.getInstance()
|
||||
override val buildDate: Instant by MiraiConsoleBuildConstants::buildDate
|
||||
override val version: SemVersion by MiraiConsoleBuildConstants::version
|
||||
override val rootPath: Path by instance::rootPath
|
||||
|
@ -11,9 +11,7 @@
|
||||
|
||||
package net.mamoe.mirai.console.command
|
||||
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.Testing
|
||||
import net.mamoe.mirai.console.Testing.withTesting
|
||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.getRegisteredCommands
|
||||
@ -24,12 +22,10 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterCommand
|
||||
import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser
|
||||
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
||||
import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext
|
||||
import net.mamoe.mirai.console.initTestEnvironment
|
||||
import net.mamoe.mirai.console.framework.AbstractConsoleTest
|
||||
import net.mamoe.mirai.console.internal.command.CommandManagerImpl
|
||||
import net.mamoe.mirai.console.internal.command.flattenCommandComponents
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.*
|
||||
|
||||
@ -102,21 +98,7 @@ internal val owner by lazy { TestUnitCommandOwner }
|
||||
|
||||
|
||||
@OptIn(ExperimentalCommandDescriptors::class)
|
||||
internal class TestCommand {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
@BeforeAll
|
||||
fun init() {
|
||||
initTestEnvironment()
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
@JvmStatic
|
||||
fun destroy() {
|
||||
MiraiConsole.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
internal class TestCommand : AbstractConsoleTest() {
|
||||
@Test
|
||||
fun testRegister() {
|
||||
try {
|
||||
@ -352,8 +334,26 @@ internal class TestCommand {
|
||||
println(composite.overloads.joinToString())
|
||||
|
||||
composite.withRegistration {
|
||||
assertEquals(1, withTesting { assertSuccess(composite.execute(sender, "mute 123")) }) // one arg, resolves to mute(Int)
|
||||
assertEquals(2, withTesting { assertSuccess(composite.execute(sender, "mute 123 1")) }) // two arg, resolved to mute(Int, Int)
|
||||
assertEquals(
|
||||
1,
|
||||
withTesting {
|
||||
assertSuccess(
|
||||
composite.execute(
|
||||
sender,
|
||||
"mute 123"
|
||||
)
|
||||
)
|
||||
}) // one arg, resolves to mute(Int)
|
||||
assertEquals(
|
||||
2,
|
||||
withTesting {
|
||||
assertSuccess(
|
||||
composite.execute(
|
||||
sender,
|
||||
"mute 123 1"
|
||||
)
|
||||
)
|
||||
}) // two arg, resolved to mute(Int, Int)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,8 @@ abstract class AbstractConsoleTest {
|
||||
// ignored
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
MiraiConsoleImplementation.instance = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user