Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions velocity/src/main/kotlin/icu/h2l/login/HyperZoneLoginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ class HyperZoneLoginMain(

companion object {
private lateinit var instance: HyperZoneLoginMain
// 用内置默认值兜底初始化:测试或其他未执行完整 onEnable 的上下文里,
// 访问配置不再抛 UninitializedPropertyAccessException,而是返回安全默认配置。
private var coreConfig: CoreConfig = CoreConfig()
private var startConfig: StartConfig = StartConfig()
private lateinit var coreConfig: CoreConfig
Comment thread
qyl27 marked this conversation as resolved.
Resolved

@JvmStatic
fun getCoreConfig(): CoreConfig = coreConfig
Expand All @@ -157,7 +154,7 @@ class HyperZoneLoginMain(
fun onEnable(event: ProxyInitializeEvent) {
registerApiLogger()
// ── 第一步:加载启动前置配置(不受 i18n 影响,硬编码提示)──────────────────────
startConfig = StartConfigLoader.load(dataDirectory)
val startConfig = StartConfigLoader.load(dataDirectory)
if (!startConfig.ready) {
logger.error("================================================================")
logger.error(" 你看起来似乎还没准备好,请先配置 start.conf 再继续使用 HyperZoneLogin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,32 @@ import icu.h2l.api.profile.HyperZoneCredential
import icu.h2l.api.profile.InGameProfileConflictException
import icu.h2l.api.profile.UserMergeHandler
import icu.h2l.api.profile.UserMergeHandlerRegistry
import util.MockStaticUtil
import icu.h2l.login.config.CoreConfig
import icu.h2l.login.database.helper.BindingCodeDatabaseHelper
import icu.h2l.login.database.helper.InGameProfileDatabaseHelper
import icu.h2l.login.database.helper.UserDatabaseHelper
import icu.h2l.login.database.DatabaseManager
import icu.h2l.login.profile.VelocityHyperZoneUserService
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.nio.file.Files
import java.util.*

class InGameProfileDatabaseTest {
@BeforeEach
fun setUp() {
MockStaticUtil.setCoreConfig(CoreConfig())
}

@AfterEach
fun tearDown() {
MockStaticUtil.clear()
}

@Test
fun `create profile writes active in-game identity and resolves through it`() {
withDatabase { context ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import icu.h2l.api.db.User
import icu.h2l.api.event.profile.VerifyInitialGameProfileEvent
import icu.h2l.api.vServer.HyperZoneVServerAdapter
import icu.h2l.login.HyperZoneLoginMain
import util.MockStaticUtil
import icu.h2l.login.manager.LoginManager
import icu.h2l.login.manager.HyperZonePlayerManager
import icu.h2l.login.player.VelocityHyperZonePlayer
Expand Down Expand Up @@ -126,6 +127,7 @@ class AttachedUserInitialGameProfileListenerTest {
sessions.clear()
transientChannels.forEach(EmbeddedChannel::finishAndReleaseAll)
transientChannels.clear()
MockStaticUtil.clear()
}

private fun bootstrapMain(): HyperZoneLoginMain {
Expand Down
42 changes: 27 additions & 15 deletions velocity/src/test/kotlin/icu/h2l/login/manager/LoginManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package icu.h2l.login.manager
import com.velocitypowered.api.event.EventManager
import com.velocitypowered.api.proxy.Player
import com.velocitypowered.api.proxy.ProxyServer
import icu.h2l.api.HyperZoneApi
import icu.h2l.api.db.InGameProfile
import icu.h2l.api.db.InGameProfileCandidate
import icu.h2l.api.db.User
Expand All @@ -32,21 +33,46 @@ import icu.h2l.api.event.auth.LoginHandleResult
import icu.h2l.api.player.getChannel
import icu.h2l.api.profile.HyperZoneCredential
import icu.h2l.api.profile.HyperZoneUserService
import icu.h2l.api.profile.HyperZoneUserServiceProvider
import icu.h2l.login.HyperZoneLoginMain
import util.MockStaticUtil
import icu.h2l.login.config.CoreConfig
import icu.h2l.login.player.VelocityHyperZonePlayer
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import io.netty.channel.embedded.EmbeddedChannel
import net.kyori.adventure.text.logger.slf4j.ComponentLogger
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.nio.file.Paths
import java.util.UUID
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicBoolean

class LoginManagerTest {
@BeforeEach
fun setUp() {
MockStaticUtil.setCoreConfig(CoreConfig())
HyperZoneLoginMain(
server = mockk<ProxyServer>(relaxed = true),
logger = mockk<ComponentLogger>(relaxed = true),
dataDirectory = Paths.get("build", "tmp", "login-manager-test"),
plugin = mockk<HyperZoneApi>(relaxed = true),
)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
}

@AfterEach
fun tearDown() {
unmockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
MockStaticUtil.clear()
}

@Test
fun `dispatch returns no claims when no module applies`() {
val eventManager = mockk<EventManager>()
Expand All @@ -56,8 +82,6 @@ class LoginManagerTest {
val hyperPlayer = mockk<VelocityHyperZonePlayer>(relaxed = true)
val channel = EmbeddedChannel()
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { hyperPlayer.hasAttachedUser() } returns false
Expand All @@ -79,8 +103,6 @@ class LoginManagerTest {
val hyperPlayer = mockk<VelocityHyperZonePlayer>(relaxed = true)
val channel = EmbeddedChannel()
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { hyperPlayer.hasAttachedUser() } returns false
Expand Down Expand Up @@ -116,8 +138,6 @@ class LoginManagerTest {
id = UUID.randomUUID(),
)
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { hyperPlayer.hasAttachedUser() } answers { attached.get() }
Expand Down Expand Up @@ -158,8 +178,6 @@ class LoginManagerTest {
val hyperPlayer = mockk<VelocityHyperZonePlayer>(relaxed = true)
val channel = EmbeddedChannel()
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { hyperPlayer.hasAttachedUser() } returns false
Expand All @@ -185,8 +203,6 @@ class LoginManagerTest {
val hyperPlayer = mockk<VelocityHyperZonePlayer>(relaxed = true)
val channel = EmbeddedChannel()
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { hyperPlayer.hasAttachedUser() } returns false
Expand Down Expand Up @@ -231,8 +247,6 @@ class LoginManagerTest {
)
val inGameProfile = mockk<InGameProfile>(relaxed = true)
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { hyperPlayer.hasAttachedUser() } answers { attached.get() }
Expand Down Expand Up @@ -292,8 +306,6 @@ class LoginManagerTest {
id = userUUID,
)
every { proxyServer.eventManager } returns eventManager
HyperZoneUserServiceProvider.bind(userService)
mockkStatic("icu.h2l.api.player.VelocityPlayerExtensionsKt")
every { player.getChannel() } returns channel
every { hyperPlayer.getProxyPlayerOrNull() } returns player
every { credential.channelId } returns "test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package icu.h2l.login.profile

import com.velocitypowered.api.proxy.ProxyServer
import icu.h2l.api.HyperZoneApi
import icu.h2l.api.db.InGameProfile
import icu.h2l.api.db.User
import icu.h2l.api.player.ClientLoginIdentity
Expand All @@ -30,24 +31,43 @@ import icu.h2l.api.profile.HyperZoneCredential
import icu.h2l.api.profile.HyperZoneUserService
import icu.h2l.api.util.IPropertiesHolder
import icu.h2l.api.util.PropertiesHolder
import icu.h2l.login.HyperZoneLoginMain
import util.MockStaticUtil
import icu.h2l.login.config.MessagesConfig
import icu.h2l.login.database.helper.BindingCodeStore
import icu.h2l.login.manager.LoginManager
import icu.h2l.login.message.MessageService
import io.mockk.mockk
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.logger.slf4j.ComponentLogger
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
import java.util.*

class UserBindingCodeServiceTest {
@TempDir
lateinit var dataDirectory: Path

private lateinit var repository: FakeBindingCodeStore
private lateinit var userService: FakeProfileService
private lateinit var bindingCodeService: UserBindingCodeService
private lateinit var hyperZonePlayer: FakeHyperZonePlayer

@BeforeEach
fun setUp() {
val logger = ComponentLogger.logger("UserBindingCodeServiceTest")
val main = HyperZoneLoginMain(
server = mockk<ProxyServer>(relaxed = true),
logger = logger,
dataDirectory = dataDirectory,
plugin = mockk<HyperZoneApi>(relaxed = true),
)
main.messageService = MessageService(dataDirectory, logger).also { it.load(MessagesConfig()) }
repository = FakeBindingCodeStore()
userService = FakeProfileService()
hyperZonePlayer = FakeHyperZonePlayer()
Expand All @@ -58,6 +78,11 @@ class UserBindingCodeServiceTest {
)
}

@AfterEach
fun tearDown() {
MockStaticUtil.clear()
}

@Test
fun `generate requires attached profile`() {
val result = bindingCodeService.generate(hyperZonePlayer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer
import icu.h2l.api.HyperZoneApi
import icu.h2l.api.event.auth.LoginHandleRequestEvent
import icu.h2l.login.HyperZoneLoginMain
import util.MockStaticUtil
import icu.h2l.login.config.CoreConfig
import icu.h2l.login.database.DatabaseConfig
import icu.h2l.login.database.helper.BindingCodeDatabaseHelper
import icu.h2l.login.database.helper.InGameProfileDatabaseHelper
Expand Down Expand Up @@ -135,6 +137,7 @@ class OutPreVServerAuthTest {
session.channel.finishAndReleaseAll()
}
sessions.clear()
MockStaticUtil.clear()
}

private fun bootstrapMain(proxyServer: ProxyServer, outPre: OutPreVServerAuth): HyperZoneLoginMain {
Expand All @@ -148,7 +151,7 @@ class OutPreVServerAuthTest {
main.userService = createProfileService(proxyServer)
main.messageService = MessageService(main.dataDirectory, main.logger)
main.loginManager = LoginManager(proxyServer, main.userService)
setCoreConfig(icu.h2l.login.config.CoreConfig())
MockStaticUtil.setCoreConfig(CoreConfig())
}
}

Expand Down Expand Up @@ -203,13 +206,6 @@ class OutPreVServerAuthTest {
return field.getBoolean(this)
}

private fun setCoreConfig(value: Any) {
val field = HyperZoneLoginMain::class.java.getDeclaredField("coreConfig").apply {
isAccessible = true
}
field.set(null, value)
}

private data class TestSession(
val channel: EmbeddedChannel,
val player: ConnectedPlayer,
Expand Down
43 changes: 43 additions & 0 deletions velocity/src/test/kotlin/util/MockStaticUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of HyperZoneLogin, licensed under the GNU Affero General Public License v3.0 or later.
*
* Copyright (C) ksqeib (庆灵) <ksqeib@qq.com>
* Copyright (C) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package util

import icu.h2l.login.HyperZoneLoginMain
import icu.h2l.login.config.CoreConfig

internal object MockStaticUtil {
private val instanceField = staticField("instance")
private val coreConfigField = staticField("coreConfig")

fun setCoreConfig(value: CoreConfig) {
coreConfigField.set(null, value)
}

fun clear() {
instanceField.set(null, null)
coreConfigField.set(null, null)
Comment on lines +31 to +37

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Parallel tests clear each other’s state

When the test runner executes these test classes concurrently and one test tears down while another is using the static fields, one test’s clear() nulls the shared HyperZoneLoginMain fields while another test is using them; the second test then gets an uninitialized-property exception or observes the wrong main instance.

Serialize tests that share this state, or replace the process-wide static test state with isolated state per test.

Prompt for AI agents
In `velocity/src/test/kotlin/util/MockStaticUtil.kt` at lines 31-37:

**Parallel tests clear each other’s state**

When the test runner executes these test classes concurrently and one test tears down while another is using the static fields, one test’s `clear()` nulls the shared `HyperZoneLoginMain` fields while another test is using them; the second test then gets an uninitialized-property exception or observes the wrong main instance.

Serialize tests that share this state, or replace the process-wide static test state with isolated state per test.

}

private fun staticField(name: String) = HyperZoneLoginMain::class.java.getDeclaredField(name).apply {
isAccessible = true
}
}
Loading