test(infra): DbDiagnose 用例库不可达自守卫——真探测一次+进程级缓存结论
原守卫 try 挂在 GetYLDbContext() 上永远打不中(EF context 构造不连库, Connect Timeout 发生在首个查询),无库裸跑 14 个用例逐个假红且各耗 15s。 改为 DbDiagnoseGuard.RequireTestDb():CanConnect 真探测一次并缓存, 不可达全批 Inconclusive——裸跑不再有假失败噪音,探测成本 14×15s→1×15s。 连上库的机器行为不变(照常真跑)。
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ namespace YLErp.Modules.EodModule
|
||||
[TestCategory("DbDiagnose")]
|
||||
public void Diagnose_FR007_UnderlyingIdMismatch()
|
||||
{
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -164,6 +165,7 @@ namespace YLErp.Modules.EodModule
|
||||
public void Diagnose_Trade_FR007_ResetDays()
|
||||
{
|
||||
const string TradeNumber = "GLMS-JIATT-20260805-FICC-01-2180120IB";
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -256,6 +258,7 @@ namespace YLErp.Modules.EodModule
|
||||
public void Diagnose_EOD_vs_Unwind_Compound_DailyCompare()
|
||||
{
|
||||
const string TradeNumber = "GLMS-JIATT-20260805-FICC-01-2180120IB";
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -367,6 +370,7 @@ namespace YLErp.Modules.EodModule
|
||||
{
|
||||
const string TradeNumber = "GLMS-JIATT-20260805-FICC-01-2180120IB";
|
||||
const long CompoundPositionId = 38122;
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -431,6 +435,7 @@ namespace YLErp.Modules.EodModule
|
||||
public void Diagnose_Trade_804_ResetDay_FloatRate_Trace()
|
||||
{
|
||||
const string TradeNumber = "GLMS-JIATT-20260805-FICC-01-2180120IB";
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -528,6 +533,7 @@ namespace YLErp.Modules.EodModule
|
||||
public void Diagnose_Trade_AllLegs_And_EodMapping()
|
||||
{
|
||||
const string TradeNumber = "GLMS-JIATT-20260805-FICC-01-2180120IB";
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace YLErp.Modules.SwapModule
|
||||
[TestCategory("DbDiagnose")]
|
||||
public void Record_RealSnapshot()
|
||||
{
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -113,6 +114,7 @@ namespace YLErp.Modules.SwapModule
|
||||
|
||||
private void DiagnoseTrade(string tradeNumber)
|
||||
{
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -266,6 +268,7 @@ namespace YLErp.Modules.SwapModule
|
||||
public void Diagnose_0006_UnwindPercentRate_Display()
|
||||
{
|
||||
const string tradeNumber = "GLMS-20260701-0006";
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace YLErp.Modules.SwapModule
|
||||
[TestCategory("DbDiagnose")]
|
||||
public void Record_RealSnapshot()
|
||||
{
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
@@ -193,6 +194,7 @@ namespace YLErp.Modules.SwapModule
|
||||
[TestCategory("DbDiagnose")]
|
||||
public void Diagnose_100vs40_InterestDiff()
|
||||
{
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库:{ex.Message}"); return; }
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace UnitTestProject.Modules.SwapModule.Margin
|
||||
[TestCategory("DbDiagnose")]
|
||||
public void 保证金腿_真实库_EOD逐日新旧比对()
|
||||
{
|
||||
DbDiagnoseGuard.RequireTestDb();
|
||||
YLContext db;
|
||||
try { db = DbContextFactory.GetYLDbContext(); }
|
||||
catch (Exception ex) { Assert.Inconclusive($"无法连接测试库(192.168.2.96):{ex.Message}"); return; }
|
||||
|
||||
Reference in New Issue
Block a user