Skip to content
Merged
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
11 changes: 9 additions & 2 deletions .github/workflows/release-baget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ jobs:
working-directory: .
run: dotnet build StdUnit.Tags.sln -c Release --nologo

- name: Test with Coverage
# ⚠ 两个测试项目**分成两步顺序跑**(理由同 release-nuget.yml):`dotnet test` 直接打解决方案会并行
# 启动多个 testhost,windows-latest 只有 4 个 vCPU,WebUi 的观测流测试(真实轮询循环 + SSE 写循环)
# 与另外上千个用例抢 CPU 后会出现"偶发超时"的假红。
- name: Test with Coverage (core, net8.0 + net472)
working-directory: .
run: dotnet test -c Release --no-build --collect:"XPlat Code Coverage" --results-directory ./TestResults --nologo
run: dotnet test tests/StdUnit.Tags.Tests/StdUnit.Tags.Tests.csproj -c Release --no-build --collect:"XPlat Code Coverage" --results-directory ./TestResults --nologo

- name: Test with Coverage (net8.0-only)
working-directory: .
run: dotnet test tests/StdUnit.Tags.Tests.NetCoreOnly/StdUnit.Tags.Tests.NetCoreOnly.csproj -c Release --no-build --collect:"XPlat Code Coverage" --results-directory ./TestResults --nologo

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/release-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ jobs:
working-directory: .
run: dotnet build StdUnit.Tags.sln -c Release --nologo

- name: Test with Coverage
# ⚠ 两个测试项目**分成两步顺序跑**,不要用 `dotnet test` 直接打解决方案:
# 那样 MSBuild 会并行启动多个 testhost(Tests/net8.0 + Tests/net472 + NetCoreOnly),
# 而 windows-latest 只有 4 个 vCPU、WebUi 的观测流测试要跑真实轮询循环 + SSE 写循环,
# 彼此抢 CPU 后 5s 级的等待窗口会偶发超时(2026-11 实测过一次打挂 3 个不相干的用例)。
# 顺序跑的代价只有十几秒,换来的是"红就是真红"。
- name: Test with Coverage (core, net8.0 + net472)
working-directory: .
run: dotnet test -c Release --no-build --collect:"XPlat Code Coverage" --results-directory ./TestResults --nologo
run: dotnet test tests/StdUnit.Tags.Tests/StdUnit.Tags.Tests.csproj -c Release --no-build --collect:"XPlat Code Coverage" --results-directory ./TestResults --nologo

- name: Test with Coverage (net8.0-only)
working-directory: .
run: dotnet test tests/StdUnit.Tags.Tests.NetCoreOnly/StdUnit.Tags.Tests.NetCoreOnly.csproj -c Release --no-build --collect:"XPlat Code Coverage" --results-directory ./TestResults --nologo

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ StdUnit.Tags.Core 硬件无关的核心抽象 + Schemas/tagsproje
- **分隔符**:相对路径一律用 `/` 或 `Path.Combine` 分段,**不要写 `@"Samples\Web\index.xml"`**——反斜杠在 Linux 是合法文件名字符,`Path.Combine` 不会转换,结果是"Windows 上绿、Linux 上 `File.Exists` 失败"。夹具/示例用 `TestPaths.Fixture("Samples", "Web", "index.xml")` 定位。
- **Windows 专有绝对路径**:`C:\base` 在 Linux 上**不是** rooted 路径,`Path.Combine` 的行为会不同(Windows 丢弃前一段、Linux 拼接),于是同一个用例在两个平台上"测的不是同一件事"。需要"绝对路径"语义的数据请用 `TestPaths.TempPath("base")`(运行时由 `Path.GetTempPath()` 拼出,两个平台都真 rooted)。
- 两者都收在 `tests/StdUnit.Tags.Tests/TestPaths.cs`。新增夹具/路径类用例后请在 WSL 上跑一遍 `-f net8.0`(或直接看 CI)。
- **不要用"短固定窗口"等待异步推进**:WebUi 的观测流测试要跑真实轮询循环 + SSE 写循环,CI runner 只有 4 个 vCPU、
且多个测试项目可能同时在跑(`dotnet test` 打解决方案时会并行启动多个 testhost),5s 级的窗口会把"机器忙"变成随机红
(2026-11 实测一次打挂 3 个互不相干的用例:`Subscribe_FiltersByEntry` / `WriteValue_ParsesEveryBuiltinKind` / `Delta_IsSplitByMaxValuesPerFrame`)。
- 等待"某事发生了"请用 `StreamTestContext.WaitUntil(...)`(默认 30s,条件成立即返回)或 `WaitUntilAsync`,**不要写 `Task.Delay(150)` 再断言**;
- 等待上限给宽松不会拖慢通过路径,只影响真失败时的反馈时间——所以宁可 30s 也不要 5s;
- 断言"某事**没有**发生"时,必须先用一个**正向信号**证明"轮询确实又推进了"(例如再改一个值并等它到达),
否则循环停摆也会让负向断言通过(测试失去鉴别力);
- `tests/StdUnit.Tags.Tests.NetCoreOnly/TestProcessSetup.cs` 会在进程启动时抬高线程池最小线程数,避免线程池爬坡造成的假超时;
两个 release 工作流也据此把测试项目**分成两步顺序跑**。
- **大小写也算"跨平台"**:Linux 文件系统区分大小写,Windows 不区分。写错大小写的路径(例如测试里的 `Schemas/drivers` 而 csproj 的 `Link` 是 `Schemas\Drivers`)在 Windows 上一直绿,一到 Linux CI 就 `DirectoryNotFoundException`。
- **警告:这个坑在 WSL 里测不出来**——`/mnt/d`(DrvFs)同样不区分大小写。要真验证,得把仓库复制到 WSL 的 ext4(`tar` 排除 `bin`/`obj`,删掉 `paket-files/paket.restore.cached` 后 `paket restore`),或直接依赖 CI。
- 需要按名字定位文件/目录时,**大小写必须与产出方(csproj 的 `Link`、`TestPaths.Fixture(...)`)逐字符一致**——不要为了"容错"写成不区分大小写,那恰好把这个坑掩盖掉(我们就是先写成 `SchemaPath("drivers")` 才红的)。写错了就让它在 CI 上红,并顺手核对 csproj。
Expand Down
33 changes: 33 additions & 0 deletions tests/StdUnit.Tags.Tests.NetCoreOnly/TestProcessSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;

namespace StdUnit.Tags.Tests.NetCoreOnly;

/// <summary>
/// 进程级测试设置。<br/>
/// 本程序集里的 WebUi 测试同时跑着"真实轮询循环 + hub 的发送/心跳循环 + SSE 写循环 + 若干真实宿主",
/// 一个用例就要占用 4~6 个线程池线程;而 CI runner 只有 4 个 vCPU,且运行 `dotnet test` 时可能还有别的 testhost 抢 CPU。
/// 线程池的<b>最小</b>线程数默认等于核数,超出的需求要按"爬坡"策略慢慢补线程——在 5s 级的等待窗口里就会表现为
/// "偶发超时",与本库代码无关(2026-11 的 CI 上曾同时打挂 3 个互不相干的用例)。
/// <para>
/// 这里把最小线程数抬高:这些线程绝大多数时间在等待而不是占 CPU,不会拖慢 CI,
/// 但能消掉"续体排队"这类假失败。
/// </para>
/// </summary>
internal static class TestProcessSetup
{
private const int MinThreads = 64;

[ModuleInitializer]
internal static void Initialize()
{
ThreadPool.GetMinThreads(out var workerThreads, out var completionPortThreads);
if (workerThreads >= MinThreads && completionPortThreads >= MinThreads)
{
return;
}

ThreadPool.SetMinThreads(Math.Max(workerThreads, MinThreads), Math.Max(completionPortThreads, MinThreads));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ private static string CreateRoot()
return root;
}

public static bool WaitUntil(Func<bool> condition, int timeoutMs = 5000)
/// <summary>
/// 轮询等待条件成立。<br/>
/// <b>超时给得宽松(30s)是有意的</b>:条件成立就立刻返回,所以正常路径不受影响;而 CI runner 只有 2 个 vCPU,
/// 线程池爬坡(每秒一两个线程)可能让轮询循环的续体排队数秒——用 5s 当上限会把"机器慢"变成随机失败。
/// </summary>
public static bool WaitUntil(Func<bool> condition, int timeoutMs = 30_000)
{
var deadline = DateTime.UtcNow.AddMilliseconds(timeoutMs);
while (DateTime.UtcNow < deadline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ namespace StdUnit.Tags.Tests.NetCoreOnly.WebUi;
/// </summary>
public class TagsStreamCommandsTests : IDisposable
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5);
/// <summary>
/// 写意图的超时上限。<b>给得宽松是有意的</b>:CI runner 只有 2 个 vCPU,线程池爬坡可能让轮询循环的
/// 续体排队数秒,5s 的上限会把"机器慢"变成随机失败(实测 INT16 那个用例报过 504)。
/// 真正要验证"超时"的用例请用 <see cref="ShortTimeout"/>。
/// </summary>
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30);

private static readonly TimeSpan ShortTimeout = TimeSpan.FromMilliseconds(200);

private readonly ServiceProvider _root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,14 @@ public async Task Subscribe_FiltersByEntry()
StreamTestContext.TagAt(project, "g2/a").Value = (short)22;

Assert.True(StreamTestContext.WaitUntil(() => output.Text.Contains("\"raw\":\"22\"")));
await Task.Delay(150);

// 再用"g2 的第二个值"确认轮询又推进了至少一轮:两个入口是并行轮询的,
// 所以 g1 只要会发增量,早就该在 output 里出现了——不用固定延时去赌调度速度。
StreamTestContext.TagAt(project, "g2/a").Value = (short)23;
Assert.True(StreamTestContext.WaitUntil(() => output.Text.Contains("\"raw\":\"23\"")));

Assert.DoesNotContain("\"raw\":\"11\"", output.Text);
Assert.DoesNotContain("\"path\":\"g1/a\"", output.Text);

await StreamTestContext.StopAsync(runCts, run);
await StopSubscriberAsync(piperCts, writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ namespace StdUnit.Tags.Tests.NetCoreOnly.WebUi;
/// </summary>
public class TagsStreamShutdownTests
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5);
/// <summary>
/// "流应当结束"的等待上限。<b>给得宽松是有意的</b>:条件成立就立刻返回,慢的只可能是 CI 的线程池爬坡
/// (2 核 runner 上每秒才一两个新线程),用 5s 当上限会把"机器慢"变成随机失败。
/// </summary>
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30);

private static TagsStreamOptions FastOptions() => new()
{
Expand Down
Loading