Skip to content

fix: 测试案例编译问题#2653

Open
tangge233 wants to merge 1 commit intodevfrom
fix/test
Open

fix: 测试案例编译问题#2653
tangge233 wants to merge 1 commit intodevfrom
fix/test

Conversation

@tangge233
Copy link
Copy Markdown
Contributor

@tangge233 tangge233 commented Apr 1, 2026

Summary by Sourcery

使测试与更新后的网络、日志记录和语义化版本(SemVer)API 对齐,并为新的 SemVer 场景增加测试覆盖。

Bug 修复:

  • 更新 McPing 测试,通过工厂构造服务,并调整主机以匹配当前的 McPing API。
  • 调整 Logger 测试,使其适配简化后的 LoggerConfiguration API,并采用正确的异步释放(async disposal)语义以防止编译问题。

测试:

  • 移除使用旧 PingOldAsync API 的过时旧版 McPing ping 测试。
  • 扩展 SemVer 测试,增加一个用于解析和排序带前缀 v 的复杂预发布版本(prerelease)用例。
Original summary in English

Summary by Sourcery

Align tests with updated networking, logging, and semantic versioning APIs and add coverage for new SemVer scenarios.

Bug Fixes:

  • Update McPing tests to construct services via the factory and adjust hosts to match the current McPing API.
  • Adapt Logger tests to the simplified LoggerConfiguration API and proper async disposal semantics to prevent compilation issues.

Tests:

  • Remove obsolete legacy McPing ping test that uses the old PingOldAsync API.
  • Extend SemVer tests with a case for parsing and ordering complex prerelease versions prefixed with a leading 'v'.

@pcl-ce-automation pcl-ce-automation bot added 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查 size: M PR 大小评估:中型 labels Apr 1, 2026
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 1, 2026

审阅者指南(在小型 PR 中折叠显示)

审阅者指南

更新测试以使用新的 McPing 服务工厂,在记录器测试中现代化日志记录器的释放与配置方式,并在清理未使用代码/using 的同时,为 SemVer 解析测试新增一个预发布版本用例。

LoggerTest.TestHeavyWrite 的时序图:带异步释放和取消支持

sequenceDiagram
    actor MSTestRunner
    participant LoggerTest
    participant Logger
    participant TaskWorker1
    participant TaskWorker2

    MSTestRunner->>LoggerTest: TestHeavyWrite()
    activate LoggerTest
    LoggerTest->>LoggerTest: create LoggerConfiguration(tempPath)
    LoggerTest->>Logger: new Logger(loggerConfiguration)
    activate Logger

    loop create tasks
        LoggerTest->>TaskWorker1: start Task(Info loop, CancellationToken)
        activate TaskWorker1
        LoggerTest->>TaskWorker2: start Task(Info loop, CancellationToken)
        activate TaskWorker2
    end

    loop per message in TaskWorker1
        TaskWorker1->>Logger: Info(message)
    end

    loop per message in TaskWorker2
        TaskWorker2->>Logger: Info(message)
    end

    TaskWorker1-->>LoggerTest: task completed or canceled
    deactivate TaskWorker1
    TaskWorker2-->>LoggerTest: task completed or canceled
    deactivate TaskWorker2

    LoggerTest->>LoggerTest: Task.WhenAll(tasks)

    LoggerTest->>Logger: DisposeAsync()
    deactivate Logger

    Logger-->>LoggerTest: disposal complete
    deactivate LoggerTest
    LoggerTest-->>MSTestRunner: TestHeavyWrite completed
Loading

更新后 McPing、Logger 和 SemVer 测试用法的类图

classDiagram
    class McPingTest {
        +PingTest() Task
    }

    class McPingServiceFactory {
        +CreateService(host string, port int) McPingService
    }

    class McPingService {
        +PingAsync() Task
    }

    class LoggerTest {
        +TestSimpleWrite() void
        +TestHeavyWrite() Task
        +TestContext TestContext
    }

    class LoggerConfiguration {
        +LoggerConfiguration(path string)
        +LoggerConfiguration(path string, segmentMode LoggerSegmentMode, maxSizeBytes long)
        +Path string
        +SegmentMode LoggerSegmentMode
        +MaxSizeBytes long
    }

    class Logger {
        +Logger(configuration LoggerConfiguration)
        +Info(message string) void
        +Dispose() void
        +DisposeAsync() ValueTask
    }

    class TestContext {
        +CancellationToken CancellationToken
    }

    class SemVerTest {
        +TestParse() void
    }

    class SemVer {
        +Major int
        +Minor int
        +Patch int
        +Prerelease string
        +Parse(input string) SemVer
        +ToString() string
        +CompareTo(other SemVer) int
    }

    McPingTest ..> McPingServiceFactory : uses
    McPingServiceFactory ..> McPingService : creates
    McPingTest ..> McPingService : uses

    LoggerTest ..> LoggerConfiguration : creates
    LoggerTest ..> Logger : uses
    LoggerTest ..> TestContext : uses

    SemVerTest ..> SemVer : uses

    Logger --> LoggerConfiguration : has
    SemVerTest ..> SemVer : parses_and_compares
Loading

文件级变更

变更 详情 文件
更新 Minecraft ping 测试以使用新的 McPing 服务工厂,并移除已过时的测试覆盖。
  • 为 McPing 服务命名空间添加 using,以解析工厂类型
  • 将直接使用 McPing 构造函数的地方替换为对两个服务器都使用 McPingServiceFactory.CreateService
  • 将第二个服务器主机名从 mc233.cn 修正为 mc.233.cn
  • 移除使用 PingOldAsync 的旧版 PingTestOld 测试,包括其本地回环配置
PCL.Core.Test/Minecraft/McPing.cs
使记录器测试与当前的 LoggerConfiguration 和支持异步释放的 Logger 行为保持一致。
  • 在 TestSimpleWrite 中简化 LoggerConfiguration 的构造,只传入日志目录路径
  • 在 TestHeavyWrite 中对 Logger 使用 await using,以遵循 IAsyncDisposable 语义
  • 将测试框架的 CancellationToken 传入 Task.Run,以便对重负载日志写入进行取消
  • 移除现在不再需要的显式 logger.Dispose() 调用
  • 引入 TestContext 属性以访问测试的取消令牌
PCL.Core.Test/LoggerTest.cs
扩展语义化版本解析测试覆盖,包含一个带较长预发布标签的版本以及顺序比较。
  • 规范 MSTest 的 using 指令(移除 BOM 伪影)
  • 新增对版本字符串 v2.14.5-beta.1.2147483647 的解析并断言结果非空
  • 将解析后的版本写入控制台以便调试
  • 断言新版本比较结果大于之前的 alpha 版本,以验证 SemVer 比较逻辑
PCL.Core.Test/SemVerTest.cs

技巧与命令

与 Sourcery 交互

  • 触发新的审阅: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 通过回复某条审阅评论,请求 Sourcery 从该评论创建一个 issue。你也可以直接回复审阅评论 @sourcery-ai issue 来从中创建 issue。
  • 生成拉取请求标题: 在拉取请求标题中的任意位置写上 @sourcery-ai 即可随时生成标题。你也可以在拉取请求中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中的任意位置写上 @sourcery-ai summary,即可在指定位置生成 PR 摘要。你也可以在拉取请求中评论 @sourcery-ai summary 来(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求中评论 @sourcery-ai guide,即可在任意时间(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。适用于你已经处理完所有评论且不想再看到它们的情况。
  • 撤销所有 Sourcery 审阅: 在拉取请求中评论 @sourcery-ai dismiss,即可撤销所有现有的 Sourcery 审阅。特别适用于你想从一个全新的审阅开始——别忘了评论 @sourcery-ai review 来触发新的审阅!

自定义你的使用体验

访问你的控制面板以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates tests to use the new McPing service factory, modernizes logger disposal and configuration in logger tests, and extends SemVer parsing tests with an additional prerelease version case while cleaning up unused code/usings.

Sequence diagram for LoggerTest.TestHeavyWrite with async disposal and cancellation

sequenceDiagram
    actor MSTestRunner
    participant LoggerTest
    participant Logger
    participant TaskWorker1
    participant TaskWorker2

    MSTestRunner->>LoggerTest: TestHeavyWrite()
    activate LoggerTest
    LoggerTest->>LoggerTest: create LoggerConfiguration(tempPath)
    LoggerTest->>Logger: new Logger(loggerConfiguration)
    activate Logger

    loop create tasks
        LoggerTest->>TaskWorker1: start Task(Info loop, CancellationToken)
        activate TaskWorker1
        LoggerTest->>TaskWorker2: start Task(Info loop, CancellationToken)
        activate TaskWorker2
    end

    loop per message in TaskWorker1
        TaskWorker1->>Logger: Info(message)
    end

    loop per message in TaskWorker2
        TaskWorker2->>Logger: Info(message)
    end

    TaskWorker1-->>LoggerTest: task completed or canceled
    deactivate TaskWorker1
    TaskWorker2-->>LoggerTest: task completed or canceled
    deactivate TaskWorker2

    LoggerTest->>LoggerTest: Task.WhenAll(tasks)

    LoggerTest->>Logger: DisposeAsync()
    deactivate Logger

    Logger-->>LoggerTest: disposal complete
    deactivate LoggerTest
    LoggerTest-->>MSTestRunner: TestHeavyWrite completed
Loading

Class diagram for updated test usage of McPing, Logger, and SemVer

classDiagram
    class McPingTest {
        +PingTest() Task
    }

    class McPingServiceFactory {
        +CreateService(host string, port int) McPingService
    }

    class McPingService {
        +PingAsync() Task
    }

    class LoggerTest {
        +TestSimpleWrite() void
        +TestHeavyWrite() Task
        +TestContext TestContext
    }

    class LoggerConfiguration {
        +LoggerConfiguration(path string)
        +LoggerConfiguration(path string, segmentMode LoggerSegmentMode, maxSizeBytes long)
        +Path string
        +SegmentMode LoggerSegmentMode
        +MaxSizeBytes long
    }

    class Logger {
        +Logger(configuration LoggerConfiguration)
        +Info(message string) void
        +Dispose() void
        +DisposeAsync() ValueTask
    }

    class TestContext {
        +CancellationToken CancellationToken
    }

    class SemVerTest {
        +TestParse() void
    }

    class SemVer {
        +Major int
        +Minor int
        +Patch int
        +Prerelease string
        +Parse(input string) SemVer
        +ToString() string
        +CompareTo(other SemVer) int
    }

    McPingTest ..> McPingServiceFactory : uses
    McPingServiceFactory ..> McPingService : creates
    McPingTest ..> McPingService : uses

    LoggerTest ..> LoggerConfiguration : creates
    LoggerTest ..> Logger : uses
    LoggerTest ..> TestContext : uses

    SemVerTest ..> SemVer : uses

    Logger --> LoggerConfiguration : has
    SemVerTest ..> SemVer : parses_and_compares
Loading

File-Level Changes

Change Details Files
Update Minecraft ping tests to use the new McPing service factory and remove obsolete test coverage.
  • Add using for the McPing service namespace to resolve the factory type
  • Replace direct McPing constructor usage with McPingServiceFactory.CreateService for two servers
  • Fix the second server hostname string from mc233.cn to mc.233.cn
  • Remove the legacy PingTestOld test that used PingOldAsync, including its local loopback configuration
PCL.Core.Test/Minecraft/McPing.cs
Align logger tests with current LoggerConfiguration and async-disposable Logger behavior.
  • Simplify LoggerConfiguration construction in TestSimpleWrite to only pass the log directory path
  • Use await using for Logger in TestHeavyWrite to respect IAsyncDisposable semantics
  • Pass the test framework’s CancellationToken into Task.Run to allow cancellation of heavy logging workloads
  • Remove the now-unnecessary explicit logger.Dispose() call
  • Introduce a TestContext property to access the test cancellation token
PCL.Core.Test/LoggerTest.cs
Expand semantic version parsing test coverage to include a long prerelease tag and ordering comparison.
  • Normalize the using directive for MSTest (remove BOM artifact)
  • Add parsing of version string v2.14.5-beta.1.2147483647 and assert it is not null
  • Write the parsed version to the console for debugging
  • Assert that the new version compares greater than the prior alpha version to validate SemVer comparison logic
PCL.Core.Test/SemVerTest.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我发现了 2 个问题,并留下了一些高层次的反馈:

  • SemVerTest 中,Assert.IsGreaterThan(t5, t4); 无法使用标准 MSTest 的 Assert API 进行编译;建议改为使用 Assert.IsTrue(t5.CompareTo(t4) > 0); 或者自定义比较方法来进行断言。
  • LoggerTest.TestHeavyWrite 中,仅将 TestContext.CancellationToken 传给 Task.Run 并不能协作式地取消内部的日志循环;如果你希望测试可以被取消,那么需要在循环内部检查该 token,或使用在执行过程中会观察该 token 的 API。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- In `SemVerTest`, `Assert.IsGreaterThan(t5, t4);` will not compile with the standard MSTest `Assert` API; consider asserting via `Assert.IsTrue(t5.CompareTo(t4) > 0);` or a custom comparer method instead.
- In `LoggerTest.TestHeavyWrite`, passing `TestContext.CancellationToken` only to `Task.Run` does not cooperatively cancel the inner logging loop; if you intend the test to be cancellable, also check the token inside the loop or use an API that observes the token during execution.

## Individual Comments

### Comment 1
<location path="PCL.Core.Test/Minecraft/McPing.cs" line_range="22" />
<code_context>
         Console.WriteLine(res.Description);

-        using var ping1 = new McPing("mc233.cn", 25565);
+        using var ping1 = McPingServiceFactory.CreateService("mc.233.cn", 25565);
         res = await ping1.PingAsync();
         Assert.IsNotNull(res);
</code_context>
<issue_to_address>
**issue (bug_risk):** Host name changed from `mc233.cn` to `mc.233.cn` may be an unintended typo.

The original test used "mc233.cn" (no dot after `mc`), while the new value is "mc.233.cn". This may be an accidental change and could break DNS resolution. If this host change is intentional, that’s fine; otherwise, please revert to the original string.
</issue_to_address>

### Comment 2
<location path="PCL.Core.Test/SemVerTest.cs" line_range="49" />
<code_context>
+            var t5 = SemVer.Parse("v2.14.5-beta.1.2147483647");
+            Assert.IsNotNull(t5);
+            Console.WriteLine(t5.ToString());
+            Assert.IsGreaterThan(t5, t4);
         }
     }
</code_context>
<issue_to_address>
**issue (testing):** Use a MSTest-compatible assertion instead of `Assert.IsGreaterThan`, which is not available in MSTest.

`Assert.IsGreaterThan` is defined in other frameworks (e.g., NUnit), not in MSTest, so this test won’t compile unless you’ve added a custom extension. In MSTest, use something like `Assert.IsTrue(t5.CompareTo(t4) > 0);` or `Assert.IsTrue(t5 > t4);` (if the comparison operators are overloaded) instead.
</issue_to_address>

Sourcery 对开源项目免费 —— 如果你觉得我们的代码评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的代码评审。
Original comment in English

Hey - I've found 2 issues, and left some high level feedback:

  • In SemVerTest, Assert.IsGreaterThan(t5, t4); will not compile with the standard MSTest Assert API; consider asserting via Assert.IsTrue(t5.CompareTo(t4) > 0); or a custom comparer method instead.
  • In LoggerTest.TestHeavyWrite, passing TestContext.CancellationToken only to Task.Run does not cooperatively cancel the inner logging loop; if you intend the test to be cancellable, also check the token inside the loop or use an API that observes the token during execution.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `SemVerTest`, `Assert.IsGreaterThan(t5, t4);` will not compile with the standard MSTest `Assert` API; consider asserting via `Assert.IsTrue(t5.CompareTo(t4) > 0);` or a custom comparer method instead.
- In `LoggerTest.TestHeavyWrite`, passing `TestContext.CancellationToken` only to `Task.Run` does not cooperatively cancel the inner logging loop; if you intend the test to be cancellable, also check the token inside the loop or use an API that observes the token during execution.

## Individual Comments

### Comment 1
<location path="PCL.Core.Test/Minecraft/McPing.cs" line_range="22" />
<code_context>
         Console.WriteLine(res.Description);

-        using var ping1 = new McPing("mc233.cn", 25565);
+        using var ping1 = McPingServiceFactory.CreateService("mc.233.cn", 25565);
         res = await ping1.PingAsync();
         Assert.IsNotNull(res);
</code_context>
<issue_to_address>
**issue (bug_risk):** Host name changed from `mc233.cn` to `mc.233.cn` may be an unintended typo.

The original test used "mc233.cn" (no dot after `mc`), while the new value is "mc.233.cn". This may be an accidental change and could break DNS resolution. If this host change is intentional, that’s fine; otherwise, please revert to the original string.
</issue_to_address>

### Comment 2
<location path="PCL.Core.Test/SemVerTest.cs" line_range="49" />
<code_context>
+            var t5 = SemVer.Parse("v2.14.5-beta.1.2147483647");
+            Assert.IsNotNull(t5);
+            Console.WriteLine(t5.ToString());
+            Assert.IsGreaterThan(t5, t4);
         }
     }
</code_context>
<issue_to_address>
**issue (testing):** Use a MSTest-compatible assertion instead of `Assert.IsGreaterThan`, which is not available in MSTest.

`Assert.IsGreaterThan` is defined in other frameworks (e.g., NUnit), not in MSTest, so this test won’t compile unless you’ve added a custom extension. In MSTest, use something like `Assert.IsTrue(t5.CompareTo(t4) > 0);` or `Assert.IsTrue(t5 > t4);` (if the comparison operators are overloaded) instead.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Console.WriteLine(res.Description);

using var ping1 = new McPing("mc233.cn", 25565);
using var ping1 = McPingServiceFactory.CreateService("mc.233.cn", 25565);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): 将主机名从 mc233.cn 改为 mc.233.cn 可能是无意的拼写错误。

原始测试使用的是 "mc233.cn"(mc 后面没有点),而新值是 "mc.233.cn"。这可能是一次无意的修改,并可能导致 DNS 解析失败。如果确实是有意更换主机,那就没问题;否则请恢复为原始字符串。

Original comment in English

issue (bug_risk): Host name changed from mc233.cn to mc.233.cn may be an unintended typo.

The original test used "mc233.cn" (no dot after mc), while the new value is "mc.233.cn". This may be an accidental change and could break DNS resolution. If this host change is intentional, that’s fine; otherwise, please revert to the original string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这应当是正确的。
https://namemc.com/server/mc233.cn 得到服务器信息
https://namemc.com/server/mc.233.cn 得到大写的 Error: 404 (Not Found)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

了解了,感谢补充这两个链接的验证信息。

从你提供的结果来看:

  • mc233.cn 可以正常解析并获取服务器信息
  • mc.233.cn 返回 404 错误

这样可以确定当前改动里把主机名改成 mc.233.cn 确实会导致目标服务器不可用,是一个回归问题。

建议在这个测试里把主机名改回原来的:

using var ping1 = McPingServiceFactory.CreateService("mc233.cn", 25565);

如果后续有需要替换或新增其他测试服务器,也可以在单独的 commit 里做,这个 PR 里就专注于修复编译问题和 API 变更适配。

var t5 = SemVer.Parse("v2.14.5-beta.1.2147483647");
Assert.IsNotNull(t5);
Console.WriteLine(t5.ToString());
Assert.IsGreaterThan(t5, t4);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (testing): 请使用与 MSTest 兼容的断言来替代 Assert.IsGreaterThan,因为 MSTest 中没有该方法。

Assert.IsGreaterThan 是在其他测试框架(例如 NUnit)中定义的,而不是在 MSTest 中定义的,因此除非你添加了自定义扩展,否则该测试将无法编译。在 MSTest 中,可以使用类似 Assert.IsTrue(t5.CompareTo(t4) > 0);Assert.IsTrue(t5 > t4);(如果已重载比较运算符)之类的断言语句来代替。

Original comment in English

issue (testing): Use a MSTest-compatible assertion instead of Assert.IsGreaterThan, which is not available in MSTest.

Assert.IsGreaterThan is defined in other frameworks (e.g., NUnit), not in MSTest, so this test won’t compile unless you’ve added a custom extension. In MSTest, use something like Assert.IsTrue(t5.CompareTo(t4) > 0); or Assert.IsTrue(t5 > t4); (if the comparison operators are overloaded) instead.

Copy link
Copy Markdown
Member

@SALTWOOD SALTWOOD left a comment

Choose a reason for hiding this comment

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

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M PR 大小评估:中型 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants