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

Harden acceptance tests #254

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Azure.CosmosEventSourcingAcceptanceTests.Items;
using Microsoft.Azure.CosmosEventSourcingAcceptanceTests.Mappers;
using Microsoft.Azure.CosmosRepository.Extensions;
using Microsoft.Extensions.Logging;
using Polly;
using Xunit.Sdk;

Expand All @@ -25,10 +26,8 @@ public partial class AcceptanceTests
private readonly List<Guid> _atomicEventIds = new();

private readonly AsyncPolicy _defaultPolicy = Policy
.Handle<CosmosException>(x =>
x.StatusCode is HttpStatusCode.NotFound)
.Or<XunitException>()
.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(i * 2));
.Handle<Exception>()
.WaitAndRetryAsync(5, i => TimeSpan.FromSeconds(i * 2));


private async Task Execute()
Expand Down Expand Up @@ -118,6 +117,7 @@ private async Task CompleteTaskForItems(int taskId)

private async Task CheckTodoListsProjectionBuilder()
{
_logger.LogInformation("Checking todo list projections");
foreach (string name in _names)
{
TodoListItem list = await _todoListItemRepository.GetAsync(name, nameof(TodoListItem));
Expand All @@ -127,6 +127,7 @@ private async Task CheckTodoListsProjectionBuilder()

private async Task CheckTodoItemsProjectionBuilders(string taskTitle)
{
_logger.LogInformation("Checking todo items (complete/created) projections");
foreach (string name in _names)
{
IEnumerable<TodoCosmosItem> items =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.Azure.CosmosRepository.Options;
using Microsoft.Azure.CosmosRepositoryAcceptanceTests.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Polly;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -30,7 +32,7 @@ public class ChangeFeedBasicTests : CosmosRepositoryAcceptanceTest
builder.WithContainer(ProductsInfoContainer);
builder.WithPartitionKey(DefaultPartitionKey);
builder.WithContainerDefaultTimeToLive(TimeSpan.FromMinutes(10));
builder.WithChangeFeedMonitoring(feedOptions => feedOptions.PollInterval = TimeSpan.FromSeconds(1));
builder.WithChangeFeedMonitoring(feedOptions => feedOptions.PollInterval = TimeSpan.FromMilliseconds(500));
});

options.ContainerBuilder.Configure<RatingByCategory>(builder =>
Expand All @@ -41,6 +43,10 @@ public class ChangeFeedBasicTests : CosmosRepositoryAcceptanceTest
});
};

private readonly AsyncPolicy _readCountPolicy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(5, i => TimeSpan.FromSeconds(i * 2));

private readonly IRepository<RatingByCategory> _ratingsByCategoryRepository;
private readonly IChangeFeedService _changeFeedService;

Expand Down Expand Up @@ -76,13 +82,16 @@ public async Task Create_Rating_For_Product_Is_Replicated_To_Be_Partitioned_By_C

await _ratingsRepository.CreateAsync(tvRating);

await WaitFor(3);

IEnumerable<RatingByCategory> results = await _ratingsByCategoryRepository
.GetAsync(x => x.PartitionKey == TechnologyCategoryId &&
x.ProductId == product.Id);
await _readCountPolicy.ExecuteAsync(async () =>
{
_logger.LogInformation("Checking projections");
IEnumerable<RatingByCategory> results = await _ratingsByCategoryRepository
.GetAsync(x => x.PartitionKey == TechnologyCategoryId &&
x.ProductId == product.Id);

results.Count().Should().Be(1);
results.Count().Should().Be(1);
});
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ protected CosmosRepositoryAcceptanceTest(
_logger = _provider.GetRequiredService<ILogger<CosmosRepositoryAcceptanceTest>>();
}

protected static async Task WaitFor(int seconds)
{
int counter = 0;

while (counter < seconds)
{
counter++;
await Task.Delay(3000);
}
}

internal ICosmosClientProvider GetClient() =>
_provider.GetRequiredService<ICosmosClientProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down