aisop/tests/Training.Smoke/Program.cs

46 lines
2.5 KiB
C#

using SopHelper.Engine.Training;
var root = Path.Combine(Path.GetTempPath(), "parttrainer-test-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(root);
try
{
var project = new TrainingProject { FilePath = Path.Combine(root, "project.json"), Name = "中文零件", Width = 640, Height = 480,
Frames = [new FrameAnnotation { Image = "frames/000000.jpg", Box = [10, 20, 100, 200], Status = "reviewed", Prompt = true }] };
project.Save();
var loaded = TrainingProject.Load(project.FilePath);
if (loaded.Name != project.Name || !loaded.Frames[0].Prompt || loaded.Frames[0].Box![2] != 100) throw new Exception("Project round trip failed");
foreach (var invalid in new double[][] { [-1,0,10,10], [0,0,0,10], [0,0,641,10], [double.NaN,0,1,1] })
{
try { loaded.ValidateBox(invalid); throw new Exception("Invalid box accepted"); }
catch (ArgumentException) { }
}
loaded.Frames[0].Image = "../outside.jpg"; loaded.Save();
try { TrainingProject.Load(loaded.FilePath); throw new Exception("Traversal accepted"); }
catch (InvalidDataException) { }
Console.WriteLine("PASS: project persistence, Unicode, invalid box rejection, path traversal rejection");
if (args.Length > 0)
{
var script = Path.Combine(root, "worker.py");
File.WriteAllText(script, "import json,sys,time\nr=json.load(open(sys.argv[1]))\nprint('worker-ready',flush=True)\ntime.sleep(r.get('sleep',0))\nsys.exit(r.get('exit',0))\n");
var client = new WorkerClient();
var lines = new List<string>();
await client.RunAsync(args[0], script, new { sleep = 0 }, line => { lock(lines) lines.Add(line); }, CancellationToken.None);
if (!lines.Contains("worker-ready")) throw new Exception("Worker output was lost");
try
{
await client.RunAsync(args[0], script, new { exit = 2 }, _ => { }, CancellationToken.None);
throw new Exception("Worker exit failure was ignored");
}
catch (InvalidOperationException) { }
using var cancel = new CancellationTokenSource(TimeSpan.FromSeconds(1));
try
{
await client.RunAsync(args[0], script, new { sleep = 60 }, _ => { }, cancel.Token);
throw new Exception("Worker cancellation was ignored");
}
catch (OperationCanceledException) { }
Console.WriteLine("PASS: real Python process output, exit failure, cancellation");
}
}
finally { Directory.Delete(root, true); }