* 为 FANUC 命令、状态和 J519 客户端注入独立日志记录器 * 补充不可接收命令状态下队列完成等待的回归测试 * 调整 J519 命令流结束后的状态处理语义
56 lines
2.5 KiB
C#
56 lines
2.5 KiB
C#
using Flyshot.Runtime.Common;
|
|
using Flyshot.Runtime.Fanuc;
|
|
using Flyshot.Runtime.Fanuc.Protocol;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Reflection;
|
|
|
|
namespace Flyshot.Core.Tests;
|
|
|
|
/// <summary>
|
|
/// 验证 FANUC 运行时通过依赖注入解析时,能够稳定拿到日志依赖。
|
|
/// </summary>
|
|
public sealed class FanucControllerRuntimeLoggingRegistrationTests
|
|
{
|
|
/// <summary>
|
|
/// 验证宿主按 IControllerRuntime 解析 FANUC 运行时时,不会因为可选日志参数而退回到无日志实例。
|
|
/// </summary>
|
|
[Fact]
|
|
public void ServiceProvider_Resolves_FanucControllerRuntime_WithAllClientLoggers()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddLogging();
|
|
services.AddSingleton<IControllerRuntime, FanucControllerRuntime>();
|
|
|
|
using var serviceProvider = services.BuildServiceProvider();
|
|
|
|
var runtime = serviceProvider.GetRequiredService<IControllerRuntime>();
|
|
var concreteRuntime = Assert.IsType<FanucControllerRuntime>(runtime);
|
|
var loggerField = typeof(FanucControllerRuntime).GetField("_logger", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
Assert.NotNull(loggerField);
|
|
Assert.NotNull(loggerField!.GetValue(concreteRuntime));
|
|
|
|
AssertClientLoggerIsInjected<FanucCommandClient>(concreteRuntime, "_commandClient");
|
|
AssertClientLoggerIsInjected<FanucStateClient>(concreteRuntime, "_stateClient");
|
|
AssertClientLoggerIsInjected<FanucJ519Client>(concreteRuntime, "_j519Client");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 断言运行时持有的底层协议客户端已注入对应类型的日志记录器。
|
|
/// </summary>
|
|
/// <typeparam name="TClient">待检查的协议客户端类型。</typeparam>
|
|
/// <param name="runtime">已经通过依赖注入解析出的 FANUC 运行时。</param>
|
|
/// <param name="clientFieldName">运行时内部保存该客户端实例的字段名。</param>
|
|
private static void AssertClientLoggerIsInjected<TClient>(FanucControllerRuntime runtime, string clientFieldName)
|
|
{
|
|
var clientField = typeof(FanucControllerRuntime).GetField(clientFieldName, BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
Assert.NotNull(clientField);
|
|
var client = Assert.IsType<TClient>(clientField!.GetValue(runtime));
|
|
var loggerField = typeof(TClient).GetField("_logger", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
Assert.NotNull(loggerField);
|
|
Assert.NotNull(loggerField!.GetValue(client));
|
|
}
|
|
}
|