Repository navigation
refactor(test): use unified TestStaticUtil for setup and teardown. #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Serialize tests that share this state, or replace the process-wide static test state with isolated state per test. Prompt for AI agentsIn `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 | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.