Files
zszq-trs/UnitTestProject/Program.cs
T
hjhan 97aff02e1d test(infra): P0——ModuleInitializer 初始化段容错,测试库不可达不再连坐全部测试
问题(2026-08-16 栈实证):M1→AppManager.Initialize→InitializePsConfig→
InnerAppConfigService.ConfigDic 读 AppConfig 表→ServerVersion.AutoDetect 连库,
库不可达时 ModuleInitializer 抛异常→903 个测试全部'Unable to create instance'全红,
DB 断网期间阻塞全部开发(本周实证两天)。

修复:初始化段(AppManager.Initialize + DataCacheManager.UpdateOnce)包 try-catch,
失败降级为醒目警告(Console+NLog),纯内存测试照常可跑;依赖配置/缓存/库的测试
以各自连接错误失败(与降级前一致,不再伪装成类创建失败)。
完全跳过初始化仍用 YLErp_UNIT_TEST_SKIP_INITIALIZATION=1。
仅测试工程 Program.cs,零生产代码改动。

验证(库不可达、不开逃生门):修复前 0/17 类创建全失败;修复后 17/17 通过;
扩大集 48/48 通过(147ms)。常量类不受影响已证(ConsTrade.InterestMarginModels
为编译期常量,MarginModesTest 离线绿)。
2026-08-17 10:00:09 +08:00

78 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using System.Net;
using System.Runtime.CompilerServices;
using YieldChain.Commons;
using YLErp.Modules.DataCacheModule;
namespace YLErp
{
public class Program
{
static IServiceProvider ServiceProvider;
[ModuleInitializer]
internal static void M1()
{
if (string.Equals(Environment.GetEnvironmentVariable("YLErp_UNIT_TEST_SKIP_INITIALIZATION"), "1", StringComparison.Ordinal))
return;
Console.WriteLine("version--" + AppManager.Version);
var logger = NLog.LogManager.Setup().GetCurrentClassLogger();
logger.Debug("init main");
IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var services = new ServiceCollection();
YLServiceLocator.SetServiceCollection(services);
// P0 容错(2026-08-16):初始化段(AppManager.Initialize 内部 InitializePsConfig 读 AppConfig 表、
// DataCacheManager.UpdateOnce 预热)在测试库不可达时不再让 ModuleInitializer 抛异常连坐全部 903 个
// 测试——降级为醒目警告,纯内存测试照常可跑;依赖配置/缓存/库的测试将以各自的连接错误失败
// (与降级前表现一致,只是不再全红归因到"类创建失败")。完全跳过初始化仍用
// YLErp_UNIT_TEST_SKIP_INITIALIZATION=1。实测触发链:M1→AppManager.Initialize→ConfigDic→
// ServerVersion.AutoDetect→MySQL 不可达(Program.cs:332026-08-16 栈实证)。
try
{
AppManager.Initialize(YLErp.Enums.SubSystemName.UnitTest, configuration);
DataCacheManager.UpdateOnce();
}
catch (Exception ex)
{
var warn = $"[UnitTest初始化降级] 初始化段失败(测试库不可达?):{ex.GetType().Name}: {ex.Message}。" +
"纯内存测试继续;依赖配置/缓存/数据库的测试将失败——这是网络问题不是代码问题。";
Console.WriteLine(warn);
logger.Warn(warn);
}
services.AddHttpClient("")
.ConfigurePrimaryHttpMessageHandler(messageHandler =>
{
var handler = new HttpClientHandler();
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
}
return handler;
});
ServiceProvider = services.BuildServiceProvider();
YLServiceLocator.SetServiceProvider(ServiceProvider);
OtcAppContext.Initialize(s =>
{
var s2 = s.Trim('/').Replace("/", "\\");
return @"d:\\" + s2;
});
}
public static T GetService<T>()
{
return ServiceProvider.GetService<T>();
}
}
}