Skip to content

fix(tests): record progress events synchronously to remove flake - #2

Merged
hzqst merged 1 commit into
mainfrom
fix/progress-test-race
Oct 17, 2026
Merged

hzqst merged 1 commit into
mainfrom
fix/progress-test-race

Conversation

@hzqst

@hzqst hzqst commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

问题

release 工作流 (run 39021878275) 的 bsp-localization-tools / build job 失败:

Failed BSPLocalizationTools.Tests.BatchLocalizationRunnerTests.RunAsyncContinuesAfterSkippedItem [42 ms]
Assert.Contains() Failure: Filter not matched in collection
  at BatchLocalizationRunnerTests.cs:line 31

失败时收集到的进度事件停在第二个 BSP 的 ExtractingGameText,缺少 Completed。

根因

测试用 new Progress<TranslationProgress>(progressEvents.Add) 收集进度。Progress<T>.Report 不直接调用回调,而是把回调 Post 到捕获的 SynchronizationContext;xUnit 的 AsyncTestSyncContext 会把它排到线程池。因此 await RunAsync(...) 返回、断言开始执行时,末尾的事件可能尚未写入列表,断言偶发落空。属于测试竞态,产品代码无问题。

同一文件中原有的 RunAsyncReportsNestedItemProgressBeforeReturning 已经使用同步的 ListProgress,本 PR 将这一做法提取为共享辅助类并用于两个受影响用例。

改动

  • 新增 tests/BSPLocalizationTools.Tests/ListProgress.cs:同步 IProgress<TranslationProgress> 实现
  • BatchLocalizationRunnerTests.RunAsyncContinuesAfterSkippedItem 改用 ListProgress,移除重复的内嵌实现
  • LocalizationRunnerProgressTests.RunAsyncReportsStagesAndOutputPath 改用 ListProgress(该用例也有 progressEvents[^1].Stage 断言,存在同样的竞态)

验证

  • dotnet build:0 警告 0 错误
  • dotnet test -c Release 连续运行 10 次:每次 85 passed, 0 failed

🤖 Generated with Claude Code

Progress<T> posts callbacks to the captured SynchronizationContext, so
under xUnit's AsyncTestSyncContext events could arrive after the awaited
call returned and the assertions had already run. Use a shared synchronous
ListProgress so RunAsyncContinuesAfterSkippedItem and
RunAsyncReportsStagesAndOutputPath observe every event deterministically.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@hzqst
hzqst merged commit 0772f9b into main Oct 17, 2026
1 check passed
@hzqst
hzqst deleted the fix/progress-test-race branch October 17, 2026 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant