原守卫 try 挂在 GetYLDbContext() 上永远打不中(EF context 构造不连库, Connect Timeout 发生在首个查询),无库裸跑 14 个用例逐个假红且各耗 15s。 改为 DbDiagnoseGuard.RequireTestDb():CanConnect 真探测一次并缓存, 不可达全批 Inconclusive——裸跑不再有假失败噪音,探测成本 14×15s→1×15s。 连上库的机器行为不变(照常真跑)。
34 lines
1.4 KiB
C#
34 lines
1.4 KiB
C#
namespace YLErp.Modules
|
|
{
|
|
/// <summary>
|
|
/// DbDiagnose 用例自守卫:真实探测一次测试库可达性并缓存结论(整个测试进程共享)。
|
|
/// 不可达 → Assert.Inconclusive:裸跑全量不再出假红,且多个用例只付一次连接超时。
|
|
/// 原先各用例把 try 挂在 GetYLDbContext() 上是无效守卫——EF context 构造不连库,
|
|
/// Connect Timeout 发生在首个查询执行时,守卫永远打不中。
|
|
/// </summary>
|
|
public static class DbDiagnoseGuard
|
|
{
|
|
private static int _state; // 0=未探测 1=可达 2=不可达
|
|
|
|
public static void RequireTestDb()
|
|
{
|
|
var state = Volatile.Read(ref _state);
|
|
if (state == 1) return;
|
|
if (state == 2) Assert.Inconclusive("测试库不可达(结论已缓存),跳过 DbDiagnose 用例");
|
|
|
|
try
|
|
{
|
|
using var db = DbContextFactory.GetYLDbContext();
|
|
if (!db.Database.CanConnect())
|
|
throw new InvalidOperationException("CanConnect=false");
|
|
Volatile.Write(ref _state, 1);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Volatile.Write(ref _state, 2);
|
|
Assert.Inconclusive($"无法连接测试库,跳过 DbDiagnose 用例:{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|