Skip to content

Commit ea5b666

Browse files
net0wellclaude
andcommitted
test: pico real de concorrência no worker e diagnóstico do snapshot do OpenAPI (specs 006, 007)
- Spec 006 AC-8 contava sobreposições por tentativa (A cruza B e C mesmo quando B e C não rodam juntas); agora mede o pico simultâneo com varredura no tempo. - O snapshot é localizado pela raiz do clone (builds determinísticos mapeiam o caminho do fonte para /_/) e a falha mostra as primeiras linhas diferentes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5cdda49 commit ea5b666

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Runtime.CompilerServices;
21
using System.Text.Encodings.Web;
32
using System.Text.Json;
43
using System.Text.Json.Nodes;
@@ -13,9 +12,9 @@ public static class Snapshot
1312
{
1413
private static readonly JsonSerializerOptions Indented = new() { WriteIndented = true, NewLine = "\n", Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
1514

16-
public static async Task MatchJsonAsync(string name, string json, [CallerFilePath] string testFile = "")
15+
public static async Task MatchJsonAsync(string name, string json)
1716
{
18-
var folder = Path.Combine(Path.GetDirectoryName(testFile)!, "Snapshots");
17+
var folder = Path.Combine(RepositoryRoot(), "tests", "PythonScriptRunner.Dashboard.Tests", "Snapshots");
1918
var verified = Path.Combine(folder, $"{name}.verified.json");
2019
var received = Path.Combine(folder, $"{name}.received.json");
2120
var actual = (JsonNode.Parse(json) ?? throw new ArgumentException("Empty JSON.", nameof(json))).ToJsonString(Indented) + "\n";
@@ -36,6 +35,40 @@ public static async Task MatchJsonAsync(string name, string json, [CallerFilePat
3635
await File.WriteAllTextAsync(received, actual);
3736
Assert.Fail(expected is null
3837
? $"No snapshot {verified}; review {received} and rename it to .verified.json."
39-
: $"The snapshot changed: compare {received} with {verified} and replace the verified file when the change is intended.");
38+
: $"The snapshot changed: compare {received} with {verified} and replace the verified file when the change is intended."
39+
+ Environment.NewLine + Differences(expected, actual));
40+
}
41+
42+
/// <summary>The first lines that differ (the CI log shows them without the files).</summary>
43+
private static string Differences(string expected, string actual)
44+
{
45+
var before = expected.Split('\n');
46+
var after = actual.Split('\n');
47+
var lines = new List<string>();
48+
for (var i = 0; i < Math.Max(before.Length, after.Length) && lines.Count < 24; i++)
49+
{
50+
var left = i < before.Length ? before[i] : "<end>";
51+
var right = i < after.Length ? after[i] : "<end>";
52+
if (left != right)
53+
{
54+
lines.Add($"line {i + 1}:{Environment.NewLine} - {left}{Environment.NewLine} + {right}");
55+
}
56+
}
57+
58+
return string.Join(Environment.NewLine, lines);
59+
}
60+
61+
/// <summary>The clone (deterministic CI builds map source paths to <c>/_/</c>, so the test file path cannot be used).</summary>
62+
private static string RepositoryRoot()
63+
{
64+
for (var directory = new DirectoryInfo(AppContext.BaseDirectory); directory is not null; directory = directory.Parent)
65+
{
66+
if (File.Exists(Path.Combine(directory.FullName, "PythonScriptRunner.slnx")))
67+
{
68+
return directory.FullName;
69+
}
70+
}
71+
72+
throw new InvalidOperationException($"PythonScriptRunner.slnx not found above {AppContext.BaseDirectory}.");
4073
}
4174
}

‎tests/PythonScriptRunner.Worker.Tests/StepExecutionTests.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ public async Task Spec006_AC08_MaxConcurrency_LimitsTheProcessesOfAWorker()
160160
attempts.Add(run.Steps.Single().Attempts.Single());
161161
}
162162

163-
var peak = attempts.Max(a => attempts.Count(other => other.StartedAt < a.FinishedAt && a.StartedAt < other.FinishedAt));
163+
// Most attempts running at the same instant (a finish and a start at the same moment do not overlap).
164+
var peak = attempts
165+
.SelectMany(a => (IEnumerable<(DateTimeOffset At, int Delta)>)[(a.StartedAt, 1), (a.FinishedAt!.Value, -1)])
166+
.OrderBy(e => e.At).ThenBy(e => e.Delta)
167+
.Aggregate((Running: 0, Peak: 0), (state, e) => (state.Running + e.Delta, Math.Max(state.Peak, state.Running + e.Delta))).Peak;
164168
peak.ShouldBe(2, string.Join(" | ", attempts.Select(a => $"{a.StartedAt:HH:mm:ss.ffffff}-{a.FinishedAt:HH:mm:ss.ffffff} {a.WorkerId}")));
165169
}
166170

0 commit comments

Comments
 (0)