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