Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ internal sealed record AzureDevOpsTestSubResult(
/// <summary>A test case result bundled with optional attachments to upload after the result is published.</summary>
internal sealed record AzureDevOpsTestCaseResultWithAttachments(
AzureDevOpsTestCaseResult Result,
IReadOnlyList<AzureDevOpsTestResultAttachment> Attachments);
IReadOnlyList<AzureDevOpsTestResultAttachment> Attachments)
{
/// <summary>
/// Gets earlier executions that an in-process retry performed before <see cref="Result"/> became the
/// test's final outcome.
/// </summary>
public IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> PreviousAttempts { get; init; } = [];
}

internal sealed class AzureDevOpsPublishedTestResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public static async Task<AzureDevOpsResultIdStore> OpenAsync(IFileSystem fileSys
/// Records the result id Azure DevOps assigned to a newly created result, along with its first attempt.
/// </summary>
public void RecordCreated(AzureDevOpsTestCaseResult result, int resultId)
=> RecordCreated(result, resultId, [result]);

/// <summary>
/// Records the result id Azure DevOps assigned to a newly created result, along with every execution
/// already performed by an in-process retry.
/// </summary>
public void RecordCreated(AzureDevOpsTestCaseResult result, int resultId, IReadOnlyList<AzureDevOpsTestCaseResult> attempts)
{
string key = CreateKey(result.AutomatedTestStorage, result.AutomatedTestName, result.TestCaseTitle);
if (_ambiguousKeys.Contains(key))
Expand All @@ -116,11 +123,11 @@ public void RecordCreated(AzureDevOpsTestCaseResult result, int resultId)
result.AutomatedTestName,
result.TestCaseTitle,
resultId,
[CreateFirstAttempt(result)])
CreateAttempts(attempts, firstSequenceId: 1))
{
TotalDurationInMs = result.DurationInMs,
StartedDate = result.StartedDate,
CompletedDate = result.CompletedDate,
TotalDurationInMs = SumResultDurations(attempts),
StartedDate = GetEarliestStartedDate(attempts),
CompletedDate = GetLatestCompletedDate(attempts),
};
_hasUnsavedChanges = true;
}
Expand All @@ -141,6 +148,20 @@ public void RecordPublishedSubResults(AzureDevOpsTestCaseResult result, int resu
public static AzureDevOpsTestSubResult CreateFirstAttempt(AzureDevOpsTestCaseResult result)
=> ToSubResult(result, sequenceId: 1);

public static IReadOnlyList<AzureDevOpsTestSubResult> CreateAttempts(
IReadOnlyList<AzureDevOpsTestCaseResult> results,
int firstSequenceId)
{
var attempts = new List<AzureDevOpsTestSubResult>(results.Count);
for (int i = 0; i < results.Count; i++)
{
attempts.Add(ToSubResult(results[i], firstSequenceId + i));
}

TrimAttempts(attempts);
return attempts;
}

/// <summary>
/// Builds what the attempt history would become if <paramref name="result"/> were published, without
/// recording it.
Expand All @@ -151,16 +172,16 @@ public static AzureDevOpsTestSubResult CreateFirstAttempt(AzureDevOpsTestCaseRes
/// leaving the same execution listed twice under the test.
/// </remarks>
public static IReadOnlyList<AzureDevOpsTestSubResult> BuildNextAttempts(AzureDevOpsPublishedResult published, AzureDevOpsTestCaseResult result)
=> BuildNextAttempts(published, [result]);

public static IReadOnlyList<AzureDevOpsTestSubResult> BuildNextAttempts(
AzureDevOpsPublishedResult published,
IReadOnlyList<AzureDevOpsTestCaseResult> results)
{
int nextSequenceId = published.Attempts.Count == 0 ? 1 : published.Attempts[^1].SequenceId + 1;
List<AzureDevOpsTestSubResult> attempts = [.. published.Attempts, ToSubResult(result, nextSequenceId)];
List<AzureDevOpsTestSubResult> attempts = [.. published.Attempts, .. CreateAttempts(results, nextSequenceId)];

// Azure DevOps caps sub-results per result; keep the most recent attempts because they are the ones
// that explain the parent outcome. A retry sequence never gets close to this.
if (attempts.Count > AzureDevOpsLivePublishingConstants.MaxSubResultsPerResult)
{
attempts.RemoveRange(0, attempts.Count - AzureDevOpsLivePublishingConstants.MaxSubResultsPerResult);
}
TrimAttempts(attempts);

return attempts;
}
Expand All @@ -172,6 +193,28 @@ public static IReadOnlyList<AzureDevOpsTestSubResult> BuildNextAttempts(AzureDev
public static long? BuildNextTotalDuration(AzureDevOpsPublishedResult published, AzureDevOpsTestCaseResult result)
=> AddDurations(published.TotalDurationInMs ?? SumDurations(published.Attempts), result.DurationInMs);

public static long? BuildNextTotalDuration(
AzureDevOpsPublishedResult published,
IReadOnlyList<AzureDevOpsTestCaseResult> results)
=> AddDurations(published.TotalDurationInMs ?? SumDurations(published.Attempts), SumResultDurations(results));

public static long? SumResultDurations(IReadOnlyList<AzureDevOpsTestCaseResult> results)
{
long? total = null;
foreach (AzureDevOpsTestCaseResult result in results)
{
total = AddDurations(total, result.DurationInMs);
}

return total;
}

public static DateTimeOffset? GetEarliestStartedDate(IReadOnlyList<AzureDevOpsTestCaseResult> attempts)
=> attempts.Where(attempt => attempt.StartedDate is not null).Min(attempt => attempt.StartedDate);

public static DateTimeOffset? GetLatestCompletedDate(IReadOnlyList<AzureDevOpsTestCaseResult> attempts)
=> attempts.Where(attempt => attempt.CompletedDate is not null).Max(attempt => attempt.CompletedDate);

/// <summary>
/// Records an attempt history that Azure DevOps has accepted.
/// </summary>
Expand Down Expand Up @@ -481,6 +524,16 @@ or AzureDevOpsLivePublishingConstants.NotExecutedTestOutcome
private static DateTimeOffset? GetLatestCompletedDate(IReadOnlyList<AzureDevOpsTestSubResult> attempts)
=> attempts.Where(attempt => attempt.CompletedDate is not null).Max(attempt => attempt.CompletedDate);

private static void TrimAttempts(List<AzureDevOpsTestSubResult> attempts)
{
// Azure DevOps caps sub-results per result; keep the most recent attempts because they are the ones
// that explain the parent outcome. A retry sequence never gets close to this.
if (attempts.Count > AzureDevOpsLivePublishingConstants.MaxSubResultsPerResult)
{
attempts.RemoveRange(0, attempts.Count - AzureDevOpsLivePublishingConstants.MaxSubResultsPerResult);
}
}

private void TryDeleteFile(string path)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,38 +299,39 @@ private async Task<bool> TryCreateResultsAsync(
// Record the whole accepted batch before any cancellable attachment upload. Azure DevOps accepted
// every result in one operation, so the map must describe all of them even if cancellation
// interrupts the best-effort attachment phase.
List<(int ResultId, AzureDevOpsTestCaseResultWithAttachments Attempt)> firstAttemptSeeds = [];
List<(int ResultId, AzureDevOpsTestCaseResultWithAttachments Parent, IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> Attempts)> initialAttemptSeeds = [];
for (int i = 0; i < batch.Count; i++)
{
IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> attempts = GetAttempts(batch[i]);
AzureDevOpsTestCaseResult[] attemptResults = [.. attempts.Select(static attempt => attempt.Result)];

// Folded data-driven rows share one uid. A failure in any row retries the whole uid, including
// rows that passed or were skipped, so every row must retain its own result id and history.
if (_resultIdStore is not null)
{
_resultIdStore.RecordCreated(batch[i].Result, publishedResults[i].Id);
_resultIdStore.RecordCreated(batch[i].Result, publishedResults[i].Id, attemptResults);
_claimedResultIds.Add(publishedResults[i].Id);
}

if (batch[i].Attachments.Count > 0)
bool hasAttachments = attempts.Any(static attempt => attempt.Attachments.Count > 0);
if (attempts.Count > 1 || (_resultIdStore is not null && hasAttachments))
{
if (_resultIdStore is not null)
{
firstAttemptSeeds.Add((publishedResults[i].Id, batch[i]));
}
else
{
deferredAttachments.Add((
publishedResults[i].Id,
TestSubResultId: null,
batch[i].Attachments));
}
initialAttemptSeeds.Add((publishedResults[i].Id, batch[i], attempts));
}
else if (hasAttachments)
{
deferredAttachments.Add((
publishedResults[i].Id,
TestSubResultId: null,
batch[i].Attachments));
}
}

if (firstAttemptSeeds.Count > 0)
if (initialAttemptSeeds.Count > 0)
{
try
{
await SeedFirstAttemptsAsync(firstAttemptSeeds, deferredAttachments, cancellationToken).ConfigureAwait(false);
await SeedInitialAttemptsAsync(initialAttemptSeeds, deferredAttachments, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException ex)
{
Expand All @@ -351,19 +352,23 @@ private async Task<bool> TryCreateResultsAsync(
/// appending each attempt exactly once also prevents a later PATCH from replacing the sub-result that owns
/// the attachment.
/// </remarks>
private async Task SeedFirstAttemptsAsync(
List<(int ResultId, AzureDevOpsTestCaseResultWithAttachments Attempt)> seeds,
private async Task SeedInitialAttemptsAsync(
List<(int ResultId, AzureDevOpsTestCaseResultWithAttachments Parent, IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> Attempts)> seeds,
List<(int ResultId, int? TestSubResultId, IReadOnlyList<AzureDevOpsTestResultAttachment> Attachments)> deferredAttachments,
CancellationToken cancellationToken)
{
var parents = new AzureDevOpsTestCaseResult[seeds.Count];
for (int i = 0; i < seeds.Count; i++)
{
parents[i] = seeds[i].Attempt.Result with
AzureDevOpsTestCaseResult[] attemptResults = [.. seeds[i].Attempts.Select(static attempt => attempt.Result)];
parents[i] = seeds[i].Parent.Result with
{
Id = seeds[i].ResultId,
ResultGroupType = AzureDevOpsLivePublishingConstants.RerunResultGroupType,
SubResults = [AzureDevOpsResultIdStore.CreateFirstAttempt(seeds[i].Attempt.Result)],
SubResults = AzureDevOpsResultIdStore.CreateAttempts(attemptResults, firstSequenceId: 1),
DurationInMs = AzureDevOpsResultIdStore.SumResultDurations(attemptResults),
StartedDate = AzureDevOpsResultIdStore.GetEarliestStartedDate(attemptResults),
CompletedDate = AzureDevOpsResultIdStore.GetLatestCompletedDate(attemptResults),
};
}

Expand All @@ -386,23 +391,33 @@ private async Task SeedFirstAttemptsAsync(
{
// The parent POST already succeeded, but the PATCH may have reached Azure DevOps. Forget the
// mapping before propagating cancellation or falling back so a later attempt cannot replay an
// uncertain first sub-result and duplicate sequence 1.
foreach ((int resultId, AzureDevOpsTestCaseResultWithAttachments attempt) in seeds)
// uncertain first sub-result and duplicate sequence 1. The POST contained only the final
// outcome, so preserve earlier in-process executions as independent results rather than losing
// their outcomes and diagnostics.
List<AzureDevOpsTestCaseResultWithAttachments> previousAttempts = [];
foreach ((int resultId, AzureDevOpsTestCaseResultWithAttachments parent, IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> _) in seeds)
{
if (_resultIdStore!.TryGet(attempt.Result) is { } published)
if (_resultIdStore?.TryGet(parent.Result) is { } published)
{
_resultIdStore.Forget(published);
}

previousAttempts.AddRange(parent.PreviousAttempts);
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}

if (previousAttempts.Count > 0)
{
RequeueUnsafe(previousAttempts);
}

if (ex is OperationCanceledException)
{
throw;
}

foreach ((int resultId, AzureDevOpsTestCaseResultWithAttachments attempt) in seeds)
foreach ((int resultId, AzureDevOpsTestCaseResultWithAttachments parent, IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> _) in seeds)
{
deferredAttachments.Add((resultId, TestSubResultId: null, attempt.Attachments));
deferredAttachments.Add((resultId, TestSubResultId: null, parent.Attachments));
}

TryLogWarning($"{AzureDevOpsResources.AzureDevOpsLivePublishingPublishResultsFailed} {ex.Message}");
Expand All @@ -411,19 +426,26 @@ private async Task SeedFirstAttemptsAsync(

for (int i = 0; i < seeds.Count; i++)
{
_resultIdStore!.RecordPublishedSubResults(
seeds[i].Attempt.Result,
int lastSequenceId = parents[i].SubResults![^1].SequenceId;
_resultIdStore?.RecordPublishedSubResults(
seeds[i].Parent.Result,
seeds[i].ResultId,
lastPublishedSubResultSequenceId: 1);
lastSequenceId);

int? testSubResultId = publishedResults is not null
&& publishedResults[i].TryGetSubResultId(sequenceId: 1, out int resolvedSubResultId)
? resolvedSubResultId
: null;
deferredAttachments.Add((
seeds[i].ResultId,
testSubResultId,
seeds[i].Attempt.Attachments));
int firstSequenceId = lastSequenceId - seeds[i].Attempts.Count + 1;
for (int attemptIndex = 0; attemptIndex < seeds[i].Attempts.Count; attemptIndex++)
{
AzureDevOpsTestCaseResultWithAttachments attempt = seeds[i].Attempts[attemptIndex];
int sequenceId = firstSequenceId + attemptIndex;
int? testSubResultId = publishedResults is not null
&& publishedResults[i].TryGetSubResultId(sequenceId, out int resolvedSubResultId)
? resolvedSubResultId
: null;
deferredAttachments.Add((
seeds[i].ResultId,
testSubResultId,
attempt.Attachments));
}
}
}

Expand All @@ -449,15 +471,19 @@ private async Task<bool> TryUpdateResultsAsync(
long?[] totalDurations = new long?[updates.Count];
var startedDates = new DateTimeOffset?[updates.Count];
var completedDates = new DateTimeOffset?[updates.Count];
var newAttempts = new IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments>[updates.Count];
for (int i = 0; i < updates.Count; i++)
{
newAttempts[i] = GetAttempts(updates[i].Attempt);
AzureDevOpsTestCaseResult[] newAttemptResults = [.. newAttempts[i].Select(static attempt => attempt.Result)];

// Built but not recorded yet: the map may only advance once Azure DevOps has accepted the
// update, otherwise retrying a failed update would list the same execution twice.
attemptHistories[i] = AzureDevOpsResultIdStore.BuildNextAttempts(updates[i].Published, updates[i].Attempt.Result);
attemptHistories[i] = AzureDevOpsResultIdStore.BuildNextAttempts(updates[i].Published, newAttemptResults);
appendedAttempts[i] = [.. attemptHistories[i].Where(attempt => attempt.SequenceId > updates[i].Published.LastPublishedSubResultSequenceId)];
totalDurations[i] = AzureDevOpsResultIdStore.BuildNextTotalDuration(updates[i].Published, updates[i].Attempt.Result);
startedDates[i] = Min(updates[i].Published.StartedDate, updates[i].Attempt.Result.StartedDate);
completedDates[i] = Max(updates[i].Published.CompletedDate, updates[i].Attempt.Result.CompletedDate);
totalDurations[i] = AzureDevOpsResultIdStore.BuildNextTotalDuration(updates[i].Published, newAttemptResults);
startedDates[i] = Min(updates[i].Published.StartedDate, AzureDevOpsResultIdStore.GetEarliestStartedDate(newAttemptResults));
completedDates[i] = Max(updates[i].Published.CompletedDate, AzureDevOpsResultIdStore.GetLatestCompletedDate(newAttemptResults));
parents[i] = updates[i].Attempt.Result with
{
Id = updates[i].Published.Id,
Expand Down Expand Up @@ -512,23 +538,31 @@ private async Task<bool> TryUpdateResultsAsync(

for (int i = 0; i < updates.Count; i++)
{
if (updates[i].Attempt.Attachments.Count > 0)
int lastSequenceId = attemptHistories[i][^1].SequenceId;
int firstSequenceId = lastSequenceId - newAttempts[i].Count + 1;
for (int attemptIndex = 0; attemptIndex < newAttempts[i].Count; attemptIndex++)
{
int sequenceId = attemptHistories[i][^1].SequenceId;
AzureDevOpsTestCaseResultWithAttachments attempt = newAttempts[i][attemptIndex];
if (attempt.Attachments.Count == 0)
{
continue;
}

int sequenceId = firstSequenceId + attemptIndex;
if (publishedResults is null
|| !publishedResults[i].TryGetSubResultId(sequenceId, out int testSubResultId))
{
deferredAttachments.Add((
updates[i].Published.Id,
TestSubResultId: null,
updates[i].Attempt.Attachments));
attempt.Attachments));
continue;
}

deferredAttachments.Add((
updates[i].Published.Id,
testSubResultId,
updates[i].Attempt.Attachments));
attempt.Attachments));
}
}

Expand Down Expand Up @@ -599,7 +633,7 @@ private static bool BatchHasAttachments(IReadOnlyList<AzureDevOpsTestCaseResultW
{
for (int i = 0; i < batch.Count; i++)
{
if (batch[i].Attachments.Count > 0)
if (GetAttempts(batch[i]).Any(static attempt => attempt.Attachments.Count > 0))
{
return true;
}
Expand All @@ -613,12 +647,18 @@ private static int CountAttachments(IReadOnlyList<AzureDevOpsTestCaseResultWithA
int count = 0;
for (int i = 0; i < batch.Count; i++)
{
count += batch[i].Attachments.Count;
foreach (AzureDevOpsTestCaseResultWithAttachments attempt in GetAttempts(batch[i]))
{
count += attempt.Attachments.Count;
}
}

return count;
}

private static IReadOnlyList<AzureDevOpsTestCaseResultWithAttachments> GetAttempts(AzureDevOpsTestCaseResultWithAttachments result)
=> result.PreviousAttempts.Count == 0 ? [result] : [.. result.PreviousAttempts, result];

private sealed class FirstAttemptSeedCanceledException(OperationCanceledException innerException)
: OperationCanceledException(innerException.Message, innerException, innerException.CancellationToken);

Expand Down
Loading
Loading