diff --git a/UnitTestProject/Modules/DbDiagnoseGuard.cs b/UnitTestProject/Modules/DbDiagnoseGuard.cs new file mode 100644 index 00000000..0b6c2d4b --- /dev/null +++ b/UnitTestProject/Modules/DbDiagnoseGuard.cs @@ -0,0 +1,33 @@ +namespace YLErp.Modules +{ + /// + /// DbDiagnose 用例自守卫:真实探测一次测试库可达性并缓存结论(整个测试进程共享)。 + /// 不可达 → Assert.Inconclusive:裸跑全量不再出假红,且多个用例只付一次连接超时。 + /// 原先各用例把 try 挂在 GetYLDbContext() 上是无效守卫——EF context 构造不连库, + /// Connect Timeout 发生在首个查询执行时,守卫永远打不中。 + /// + 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}"); + } + } + } +} diff --git a/UnitTestProject/Modules/EodModule/GLMS20260805FR007UnderlyingIdDiagnoseTest.cs b/UnitTestProject/Modules/EodModule/GLMS20260805FR007UnderlyingIdDiagnoseTest.cs index 1a8020cf..80da3633 100644 --- a/UnitTestProject/Modules/EodModule/GLMS20260805FR007UnderlyingIdDiagnoseTest.cs +++ b/UnitTestProject/Modules/EodModule/GLMS20260805FR007UnderlyingIdDiagnoseTest.cs @@ -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; } diff --git a/UnitTestProject/Modules/SwapModule/GLMS20260701DbDiagnoseTest.cs b/UnitTestProject/Modules/SwapModule/GLMS20260701DbDiagnoseTest.cs index 4b41b962..6dfae6ab 100644 --- a/UnitTestProject/Modules/SwapModule/GLMS20260701DbDiagnoseTest.cs +++ b/UnitTestProject/Modules/SwapModule/GLMS20260701DbDiagnoseTest.cs @@ -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; } diff --git a/UnitTestProject/Modules/SwapModule/GLMS20260805ClosePercentDiffDiagnoseTest.cs b/UnitTestProject/Modules/SwapModule/GLMS20260805ClosePercentDiffDiagnoseTest.cs index 6bccc538..3daad3b2 100644 --- a/UnitTestProject/Modules/SwapModule/GLMS20260805ClosePercentDiffDiagnoseTest.cs +++ b/UnitTestProject/Modules/SwapModule/GLMS20260805ClosePercentDiffDiagnoseTest.cs @@ -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; } diff --git a/UnitTestProject/Modules/SwapModule/Margin/MarginInterestGoldenReplayTest.cs b/UnitTestProject/Modules/SwapModule/Margin/MarginInterestGoldenReplayTest.cs index e9816011..2ff99fe0 100644 --- a/UnitTestProject/Modules/SwapModule/Margin/MarginInterestGoldenReplayTest.cs +++ b/UnitTestProject/Modules/SwapModule/Margin/MarginInterestGoldenReplayTest.cs @@ -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; }