Skip to content

Commit

Permalink
Fix Flaky History Test (#3887)
Browse files Browse the repository at this point in the history
* Trying to fix flaky history test

* Changed loop to when all

* Changed to compare lastUpdated for each request.

* small formatting fix

* fix test
  • Loading branch information
mikaelweave authored May 30, 2024
1 parent 8968ae1 commit bec76c4
Showing 1 changed file with 26 additions and 14 deletions.
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 @@ public async Task GivenTestResourcesWithUpdatesAndDeletes_WhenGettingResourceHis
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();
beforeTime = ((DateTime[])[.. serverLastUpdates.Select(x => x.AddMilliseconds(1)), beforeTime]).Max();
}
}));

// 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 @@ public async Task GivenTestResourcesWithUpdatesAndDeletes_WhenGettingResourceHis
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 @@ public async Task GivenTestResourcesWithUpdatesAndDeletes_WhenGettingResourceHis
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

0 comments on commit bec76c4

Please sign in to comment.