Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Flaky History Test #3887

Merged
merged 5 commits into from
May 30, 2024
Merged
Changes from all 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
40 changes: 26 additions & 14 deletions test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/HistoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,41 @@
await _client.UpdateAsync(AddNarative(thirdTestResource, thirdTestResource.Text.Div) as Observation);

// Calculate the min/max from db values.
List<string> expectedResourceIds = [firstTestResource.Id, secondTestResource.Id, thirdTestResource.Id, fourthTestResource.Id];
var allTestResources = (await _client.SearchAsync($"_history")).Resource.Entry.Where(r => expectedResourceIds.Contains(r.Resource.Id));
var sinceTime = allTestResources.Min(r => r.Resource.Meta.LastUpdated.Value).UtcDateTime.ToString("o");
var beforeTime = allTestResources.Max(r => r.Resource.Meta.LastUpdated.Value).UtcDateTime.AddMilliseconds(1).ToString("o");
List<DomainResource> expectedResources = [firstTestResource, secondTestResource, thirdTestResource, fourthTestResource];
List<string> expectedResourceIds = expectedResources.Select(r => r.Id).ToList();
DateTime sinceTime = DateTime.MaxValue, beforeTime = DateTime.MinValue;
object lockObject = new();

await Task.WhenAll(expectedResources.Select(async testResource =>
{
var serverLastUpdates = (await _client.SearchAsync($"{testResource.TypeName}/{testResource.Id}/_history")).Resource.Entry
.Select(r => r.Resource.Meta.LastUpdated.Value.UtcDateTime);

lock (lockObject)
{
sinceTime = ((DateTime[])[..serverLastUpdates, sinceTime]).Min();
Dismissed Show dismissed Hide dismissed
beforeTime = ((DateTime[])[.. serverLastUpdates.Select(x => x.AddMilliseconds(1)), beforeTime]).Max();
Dismissed Show dismissed Hide dismissed
}
}));

// Run test queries
var allSummaryCountResult = await _client.SearchAsync($"_history?_since={sinceTime}&_before={beforeTime}&_summary=count");
var allSummaryCountZero = await _client.SearchAsync($"_history?_since={sinceTime}&_before={beforeTime}&_count=0");
var allObservationSummaryCountResult = await _client.SearchAsync($"Observation/_history?_since={sinceTime}&_before={beforeTime}&_summary=count");
var allObservationSummaryCountZero = await _client.SearchAsync($"Observation/_history?_since={sinceTime}&_before={beforeTime}&_count=0");
var observationSummaryCountResult = await _client.SearchAsync($"Observation/{firstTestResource.Id}/_history?_since={sinceTime}&_before={beforeTime}&_summary=count");
var observationSummaryCountZero = await _client.SearchAsync($"Observation/{firstTestResource.Id}/_history?_since={sinceTime}&_before={beforeTime}&_count=0");
var allSummaryCountResult = await _client.SearchAsync($"_history?_since={sinceTime:o}&_before={beforeTime:o}&_summary=count");
var allSummaryCountZero = await _client.SearchAsync($"_history?_since={sinceTime:o}&_before={beforeTime:o}&_count=0");
var allObservationSummaryCountResult = await _client.SearchAsync($"Observation/_history?_since={sinceTime:o}&_before={beforeTime:o}&_summary=count");
var allObservationSummaryCountZero = await _client.SearchAsync($"Observation/_history?_since={sinceTime:o}&_before={beforeTime:o}&_count=0");
var observationSummaryCountResult = await _client.SearchAsync($"Observation/{firstTestResource.Id}/_history?_since={sinceTime:o}&_before={beforeTime:o}&_summary=count");
var observationSummaryCountZero = await _client.SearchAsync($"Observation/{firstTestResource.Id}/_history?_since={sinceTime:o}&_before={beforeTime:o}&_count=0");

// Find all resources in window - needed to filter out stragglers from other tests
var allResources = await _client.SearchAsync($"_history?_since={sinceTime}&_before={beforeTime}");
var allResources = await _client.SearchAsync($"_history?_since={sinceTime:o}&_before={beforeTime:o}");
var stragglerResources = allResources.Resource.Entry.Where(r => !expectedResourceIds.Contains(r.Resource.Id));

// 9 versions total for all resources.
var expectedAllCount = 9 + stragglerResources.Count();
if (allSummaryCountResult.Resource.Total != expectedAllCount || allSummaryCountZero.Resource.Total != expectedAllCount)
{
Assert.Fail($"allSummaryCountResult or allSummaryCountZero not equal to {expectedAllCount}. allSummaryCountResult {allSummaryCountResult.Resource.Total}. " +
$"allSummaryCountZero {allSummaryCountZero.Resource.Total}.\n{await GetSummaryMessage($"_history?_since={sinceTime}&_before={beforeTime}")}.\n" +
$"allSummaryCountZero {allSummaryCountZero.Resource.Total}.\n{await GetSummaryMessage($"_history?_since={sinceTime:o}&_before={beforeTime:o}")}.\n" +
$"straggler resources {string.Join(',', stragglerResources.Select(r => r.Resource.Id))}.");
}

Expand All @@ -147,7 +159,7 @@
if (allObservationSummaryCountResult.Resource.Total != expectedObservationCount || allObservationSummaryCountZero.Resource.Total != expectedObservationCount)
{
Assert.Fail($"allSummaryCountResult or allSummaryCountZero not equal to {expectedObservationCount}. allObservationSummaryCountResult {allObservationSummaryCountResult.Resource.Total}. " +
$"allObservationSummaryCountZero {allObservationSummaryCountZero.Resource.Total}\n{await GetSummaryMessage($"Observation/_history?_since={sinceTime}&_before={beforeTime}")}.\n" +
$"allObservationSummaryCountZero {allObservationSummaryCountZero.Resource.Total}\n{await GetSummaryMessage($"Observation/_history?_since={sinceTime:o}&_before={beforeTime:o}")}.\n" +
$"straggler resources {string.Join(',', allObservationStragglerResources.Select(r => r.Resource.Id))}.");
}

Expand All @@ -158,7 +170,7 @@
if (observationSummaryCountResult.Resource.Total != expectedSpecificObservationCount || observationSummaryCountZero.Resource.Total != expectedSpecificObservationCount)
{
Assert.Fail($"observationSummaryCountResult or observationSummaryCountZero not equal to {expectedSpecificObservationCount}. observationSummaryCountResult {observationSummaryCountResult.Resource.Total}. " +
$"observationSummaryCountZero {observationSummaryCountZero.Resource.Total}.\n{await GetSummaryMessage($"Observation/{firstTestResource.Id}/_history?_since={sinceTime}&_before={beforeTime}")}.\n" +
$"observationSummaryCountZero {observationSummaryCountZero.Resource.Total}.\n{await GetSummaryMessage($"Observation/{firstTestResource.Id}/_history?_since={sinceTime:o}&_before={beforeTime:o}")}.\n" +
$"straggler resources {string.Join(',', specificObservationStragglerResources.Select(r => r.Resource.Id))}.");
}

Expand Down