Skip to content

Commit

Permalink
Add a wait after integ test CloudFormation deployment to wait for any…
Browse files Browse the repository at this point in the history
… eventually consistency sync up.
  • Loading branch information
normj committed Sep 5, 2024
1 parent 7e72cff commit ca14477
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -21,15 +22,15 @@ public CustomResponse(IntegrationTestContextFixture fixture)
[Fact]
public async Task OkResponseWithHeader_Returns200Status()
{
var response = await _fixture.HttpClient.GetAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/1");
var response = await GetWithRetryAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/1");
response.EnsureSuccessStatusCode();
Assert.Equal("All Good", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task OkResponseWithHeader_ReturnsValidationErrors()
{
var response = await _fixture.HttpClient.GetAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/hello");
var response = await GetWithRetryAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/hello");
Assert.Equal(400, (int)response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
var errorJson = JObject.Parse(content);
Expand All @@ -41,13 +42,43 @@ public async Task OkResponseWithHeader_ReturnsValidationErrors()
[Fact]
public async Task OkResponseWithCustomSerializer_Returns200Status()
{
var response = await _fixture.HttpClient.GetAsync($"{_fixture.HttpApiUrlPrefix}/okresponsewithcustomserializerasync/John/Doe");
var response = await GetWithRetryAsync($"{_fixture.HttpApiUrlPrefix}/okresponsewithcustomserializerasync/John/Doe");
response.EnsureSuccessStatusCode();

var content = await response.Content.ReadAsStringAsync();
var person = JObject.Parse(content);
Assert.Equal("John", person["FIRST_NAME"]);
Assert.Equal("Doe", person["LAST_NAME"]);
}

private async Task<HttpResponseMessage> GetWithRetryAsync(string path)
{
int MAX_ATTEMPTS = 10;
HttpResponseMessage response = null;
for (var retryAttempt = 0; retryAttempt < MAX_ATTEMPTS; retryAttempt++)
{
await Task.Delay(retryAttempt * 1000);
try
{
response = await _fixture.HttpClient.GetAsync(path);

// No tests are coded to return 403 Forbidden. If this is returned it is likely
// an eventual consistency issue.
if (response.StatusCode == System.Net.HttpStatusCode.Forbidden)
continue;

break;
}
catch
{
if (retryAttempt + 1 == MAX_ATTEMPTS)
{
throw;
}
}
}

return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public async Task InitializeAsync()
Assert.False(string.IsNullOrEmpty(RestApiUrlPrefix));

await LambdaHelper.WaitTillNotPending(LambdaFunctions.Select(x => x.Name).ToList());

// Wait an additional 10 seconds for any other eventually consistency state to finish up.
await Task.Delay(10000);
}

public async Task DisposeAsync()
Expand Down

0 comments on commit ca14477

Please sign in to comment.