87 lines
6.7 KiB
C#
87 lines
6.7 KiB
C#
using System.Diagnostics;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Text.Json;
|
||
using SopHelper.Engine.Sop;
|
||
|
||
internal static class RealtimeDeadlineChecks
|
||
{
|
||
public static async Task RunAsync()
|
||
{
|
||
var request = new VisionDecisionRequest("通用", 1, "状态检查", "完整结果", "完整结果", 1, DateTime.Now, [1, 2, 3],
|
||
StartedTimestamp: Stopwatch.GetTimestamp(), TimeoutMs: 500);
|
||
var slow = new Client(1200);
|
||
var watch = Stopwatch.StartNew();
|
||
var outcome = await RealtimeDecisionPolicy.RunAsync(slow, request, default);
|
||
Check(!outcome.Completed && outcome.EffectiveStatus == VisionDecisionStatus.TimedOut && watch.ElapsedMilliseconds < 900,
|
||
"服务忽略取消仍按500ms请求等待期限结束,不能采纳迟到true");
|
||
var stale = new Client(0);
|
||
outcome = await RealtimeDecisionPolicy.RunAsync(stale, request with { StartedTimestamp = Stopwatch.GetTimestamp() - Stopwatch.Frequency }, default);
|
||
Check(outcome.Completed && stale.Calls == 1, "取图和排队时间不扣减请求等待预算");
|
||
outcome = RealtimeDecisionPolicy.AtPublication(request with { StartedTimestamp = Stopwatch.GetTimestamp() - Stopwatch.Frequency }, new(true, 1, "迟到"));
|
||
Check(outcome.Completed, "及时返回的结果不会因界面调度超过期限被误判超时");
|
||
var configured = request with { TimeoutMs = 1000, StartedTimestamp = Stopwatch.GetTimestamp() };
|
||
outcome = await RealtimeDecisionPolicy.RunAsync(new Client(650), configured, default);
|
||
Check(outcome.Completed && outcome.ElapsedMs >= 600, "用户放宽期限后,超过旧500ms的有效结果可以通过");
|
||
configured = configured with { StartedTimestamp = Stopwatch.GetTimestamp() - Stopwatch.Frequency * 8 / 10 };
|
||
Check(RealtimeDecisionPolicy.AtPublication(configured, new(true, 1, "有效")).Completed,
|
||
"界面按请求快照的期限采纳结果,不再固定500ms");
|
||
var expired = configured with { StartedTimestamp = Stopwatch.GetTimestamp() - Stopwatch.Frequency * 2 };
|
||
outcome = await RealtimeDecisionPolicy.RunAsync(stale, expired, default);
|
||
Check(outcome.Completed && stale.Calls == 2
|
||
&& !RealtimeDecisionPolicy.AtPublication(expired, new(true, 1, "请求迟到", ElapsedMs: 1001)).Completed,
|
||
"只根据响应等待耗时拒绝迟到结果,不使用取图时间计时");
|
||
outcome = await RealtimeDecisionPolicy.RunAsync(new Client(450), request with { StartedTimestamp = 0 }, default);
|
||
Check(outcome.Completed && outcome.ElapsedMs >= 400, "500ms设置提供完整等待时间,不再扣除100ms预留");
|
||
outcome = await RealtimeDecisionPolicy.RunAsync(new Client(50), request with { TimeoutMs = 1 }, default);
|
||
Check(outcome.EffectiveStatus == VisionDecisionStatus.TimedOut, "1ms是有效请求期限,超时返回未通过而不是配置错误");
|
||
outcome = await RealtimeDecisionPolicy.RunAsync(stale, request with { TimeoutMs = 1001 }, default);
|
||
Check(outcome.EffectiveStatus == VisionDecisionStatus.Insufficient, "请求期限上限为1000ms");
|
||
var config = new SopConfig { Sop = new()
|
||
{ Name = "超时隔离", Steps = [new() { Id = 1, Name = "检查", AiConfirmation = new() { TimeoutSeconds = 99 } }] } };
|
||
config.Sop.Settings.DjevTimeoutMs = 375;
|
||
var yaml = SopConfigLoader.ToYaml(config);
|
||
Check(!yaml.Contains("timeout_seconds") && SopConfigLoader.Parse(yaml).Sop.Settings.DjevTimeoutMs == 300,
|
||
"配方导出不含全局djev超时及旧步骤超时,重新导入不能覆盖运行参数");
|
||
var old = Environment.GetEnvironmentVariable("SOPHELPER_VISION_PROVIDER");
|
||
var oldEndpoint = Environment.GetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT");
|
||
try
|
||
{
|
||
Environment.SetEnvironmentVariable("SOPHELPER_VISION_PROVIDER", "chat-completions");
|
||
Environment.SetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT", null);
|
||
using var handler = new Http(); using var http = new HttpClient(handler);
|
||
var client = new ConfiguredVisionDecisionClient(http, new Client(0));
|
||
Check(!client.SupportsSequences, "运行配置不提供Agent多图能力");
|
||
outcome = await client.DecideAsync(request with { StartedTimestamp = Stopwatch.GetTimestamp() }, default);
|
||
Check(outcome.Completed && handler.Images == 1 && handler.Url.Contains("systemone"), "环境及旧Agent参数不能切换判定模型,必须调用dlite单图协议");
|
||
handler.DelayMs = 650;
|
||
outcome = await client.DecideAsync(request with { TimeoutMs = 1000, StartedTimestamp = Stopwatch.GetTimestamp() - Stopwatch.Frequency * 10 }, default);
|
||
Check(outcome.Completed, "实际配置客户端保留用户期限,不被内部策略再次压回500ms");
|
||
var count = handler.Calls;
|
||
outcome = await client.DecideAsync(request with { EvidenceMode = "sequence", Frames = [new(1, DateTime.Now, [1]), new(2, DateTime.Now, [2])] }, default);
|
||
Check(outcome.EffectiveStatus == VisionDecisionStatus.Unsupported && handler.Calls == count, "禁止发送多图,不能回退到其他模型");
|
||
}
|
||
finally { Environment.SetEnvironmentVariable("SOPHELPER_VISION_PROVIDER", old); Environment.SetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT", oldEndpoint); }
|
||
Console.WriteLine("PASS: dlite单图、全局期限、配方隔离、旧图拒绝、取消不配合及迟到结果隔离");
|
||
}
|
||
private sealed class Client(int delay) : IVisionDecisionClient
|
||
{
|
||
public int Calls;
|
||
public bool SupportsSequences => true;
|
||
public async Task<VisionDecisionResult> DecideAsync(VisionDecisionRequest request, CancellationToken token)
|
||
{ Calls++; await Task.Delay(delay); return new(true, 1, "返回true"); }
|
||
}
|
||
private sealed class Http : HttpMessageHandler
|
||
{
|
||
public int Calls, Images, DelayMs; public string Url = "";
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
|
||
{
|
||
if (DelayMs > 0) await Task.Delay(DelayMs, token);
|
||
Calls++; Url = request.RequestUri!.ToString(); using var body = JsonDocument.Parse(await request.Content!.ReadAsStringAsync(token));
|
||
Images = body.RootElement.GetProperty("images").GetArrayLength();
|
||
return new(HttpStatusCode.OK) { Content = new StringContent("{\"answers\":{\"step_completed\":{\"type\":\"noul\",\"noul\":0.99}}}", Encoding.UTF8, "application/json") };
|
||
}
|
||
}
|
||
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
|
||
}
|