Skip to content
Closed
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 @@ -58,7 +58,7 @@ public async Task Should_not_throw_if_specified_region_is_right()
var customer = new Customer { Id = 42, Name = "Theon" };

using var context = new CustomerContext(options);
await context.Database.EnsureCreatedAsync();
await CosmosTestStore.DatabaseEnsureCreated(context);

await context.AddAsync(customer);

Expand All @@ -76,7 +76,7 @@ public async Task Should_throw_if_specified_region_is_wrong()
var customer = new Customer { Id = 42, Name = "Theon" };

using var context = new CustomerContext(options);
await context.Database.EnsureCreatedAsync();
await CosmosTestStore.DatabaseEnsureCreated(context);

await context.AddAsync(customer);

Expand All @@ -100,7 +100,7 @@ public async Task Should_not_throw_if_specified_connection_mode_is_right()
var customer = new Customer { Id = 42, Name = "Theon" };

using var context = new CustomerContext(options);
await context.Database.EnsureCreatedAsync();
await CosmosTestStore.DatabaseEnsureCreated(context);

await context.AddAsync(customer);

Expand All @@ -119,7 +119,7 @@ public async Task Should_throw_if_specified_connection_mode_is_wrong()
var customer = new Customer { Id = 42, Name = "Theon" };

using var context = new CustomerContext(options);
await context.Database.EnsureCreatedAsync();
await CosmosTestStore.DatabaseEnsureCreated(context);

await context.AddAsync(customer);

Expand Down
119 changes: 61 additions & 58 deletions test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions test/EFCore.Cosmos.FunctionalTests/CosmosTriggersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public async Task Triggers_are_executed_on_SaveChanges()

private async Task CreateTriggersInCosmosAsync(TriggersContext context)
{
await context.Database.EnsureCreatedAsync();

var cosmosClient = context.Database.GetCosmosClient();
var databaseId = context.Database.GetCosmosDatabaseId();
var database = cosmosClient.GetDatabase(databaseId);
Expand Down
4 changes: 0 additions & 4 deletions test/EFCore.Cosmos.FunctionalTests/EndToEndCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,6 @@ public async Task Find_with_empty_resource_id_throws(bool transactionalBatch)

using (var context = CreateContext(contextFactory, transactionalBatch))
{
await context.Database.EnsureCreatedAsync();

var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await context.Set<CustomerWithResourceId>().FindAsync(1, 3.15m, ""));

Assert.Equal(CosmosStrings.InvalidResourceId, exception.Message);
Expand Down Expand Up @@ -1061,8 +1059,6 @@ public async Task Can_read_with_find_with_partition_key_not_part_of_primary_key(

using (var context = CreateContext(contextFactory, false))
{
await context.Database.EnsureCreatedAsync();

await context.AddAsync(customer);

await context.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ protected virtual async Task PartitionKeyTestAsync(

await using (var innerContext = CreateContext())
{
await innerContext.Database.EnsureCreatedAsync();

await innerContext.AddAsync(customer1);
await innerContext.AddAsync(customer2);
await innerContext.SaveChangesAsync();
Expand Down
2 changes: 0 additions & 2 deletions test/EFCore.Cosmos.FunctionalTests/PartitionKeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ protected virtual async Task PartitionKeyTestAsync(

await using (var innerContext = CreateContext())
{
await innerContext.Database.EnsureCreatedAsync();

await innerContext.AddAsync(customer1);
await innerContext.AddAsync(customer2);
await innerContext.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@

[assembly: CosmosDbConfiguredCondition]

// Waiting on Task causes deadlocks when run in parallel
[assembly: CollectionBehavior(DisableTestParallelization = true)]
// Emulator could experience performance degradation with more than 10 concurrent containers,
// Tests have shown that the emulator will stop responding for container creation requests after ~25 containers are created.
// Some tests might create multiple containers, so we only run 3 tests in parallel to avoid hitting the limit.
// No performance improvement was found with a higher number.
// See: https://learn.microsoft.com/en-us/azure/cosmos-db/emulator#differences-between-the-emulator-and-cloud-service
[assembly: CollectionBehavior(MaxParallelThreads = 3)]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task EnsureCreated_returns_true_when_database_does_not_exist()
using var context = new BloggingContext(testDatabase);
var creator = context.GetService<IDatabaseCreator>();
await creator.EnsureDeletedAsync();
Assert.True(await creator.EnsureCreatedAsync());
Assert.True(await EnsureCreatedAsync(creator));
}

[ConditionalFact]
Expand All @@ -28,7 +28,7 @@ public async Task EnsureCreated_returns_true_when_database_exists_but_collection

using var context = new BloggingContext(testDatabase);
var creator = context.GetService<IDatabaseCreator>();
Assert.True(await creator.EnsureCreatedAsync());
Assert.True(await EnsureCreatedAsync(creator));
}

[ConditionalTheory, MemberData(nameof(IsAsyncData))]
Expand All @@ -43,7 +43,7 @@ await testDatabase.InitializeAsync(
using var context = new BloggingContext(testDatabase);
var creator = context.GetService<IDatabaseCreator>();

Assert.False(a ? await creator.EnsureCreatedAsync() : creator.EnsureCreated());
Assert.False(a ? await EnsureCreatedAsync(creator) : creator.EnsureCreated());
});

[ConditionalTheory, MemberData(nameof(IsAsyncData))]
Expand All @@ -55,7 +55,7 @@ public Task EnsureDeleted_returns_true_when_database_exists(bool async)
using var context = new BloggingContext(testDatabase);
var creator = context.GetService<IDatabaseCreator>();

Assert.True(a ? await creator.EnsureDeletedAsync() : creator.EnsureDeleted());
Assert.True(a ? await EnsureCreatedAsync(creator) : creator.EnsureDeleted());
});

[ConditionalTheory, MemberData(nameof(IsAsyncData))]
Expand All @@ -81,6 +81,25 @@ public async Task EnsureCreated_throws_for_missing_seed()
(await Assert.ThrowsAsync<InvalidOperationException>(() => context.Database.EnsureCreatedAsync())).Message);
}

private async Task<bool> EnsureCreatedAsync(IDatabaseCreator creator)
{
if (TestEnvironment.IsEmulator)
{
await CosmosTestStore.EmulatorCreateCollectionSemaphore.WaitAsync();
}
try
{
return await creator.EnsureCreatedAsync();
}
finally
{
if (TestEnvironment.IsEmulator)
{
CosmosTestStore.EmulatorCreateCollectionSemaphore.Release();
}
}
}

private class BaseContext(CosmosTestStore testStore) : DbContext
{
private readonly string _connectionUri = testStore.ConnectionUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace Microsoft.EntityFrameworkCore.TestUtilities;

public class CosmosTestStore : TestStore
{
/// <summary>
/// The emulator has race conditions when creating containers concurrently, so we need to lock around container creation.
/// </summary>
public static readonly SemaphoreSlim EmulatorCreateCollectionSemaphore = new(1);

private static List<string>? _createdDatabases = [];
private readonly TestStoreContext _storeContext;
private readonly string? _dataFilePath;
private readonly Action<CosmosDbContextOptionsBuilder> _configureCosmos;
Expand Down Expand Up @@ -75,9 +81,25 @@ private CosmosTestStore(
}

private static string CreateName(string name)
=> TestEnvironment.IsEmulator || name == "Northwind" || name == "Northwind2" || name == "Northwind3"
? name
: name + _runId;
{
// Northwind database is static and huge so we don't want to create a new one for each test run.
if (name == "Northwind" || name == "Northwind2" || name == "Northwind3")
{
return name;
}

if (TestEnvironment.IsEmulator)
{
// We delete and recreate the database for each test run in the emulator.
// This is due to limitations in the emulator.
// Since the databases are deleted, they can't be shared across test runs.
// So we have to generate a new name for each test store, otherwhise we would have name conflicts when running tests in parallel.
// https://learn.microsoft.com/en-us/azure/cosmos-db/emulator#differences-between-the-emulator-and-cloud-service
return "EF-" + Guid.NewGuid().ToString();
}

return name + _runId;
}

public string ConnectionUri { get; }
public string AuthToken { get; }
Expand Down Expand Up @@ -108,6 +130,7 @@ public static async ValueTask<bool> IsConnectionAvailableAsync()
{
_connectionSemaphore.Release();
}

}

return _connectionAvailable.Value;
Expand All @@ -120,6 +143,32 @@ private static async Task<bool> TryConnectAsync()
{
testStore = await CreateInitializedAsync("NonExistent").ConfigureAwait(false);

if (TestEnvironment.IsEmulator)
{
// Clean up old emulator databases that tests might have left behind.
// We do this because the emulator will stop responding with much more than ~10 concurrent containers,
// and test runs might have been stopped in the middle leaving databases and containers behind.
var client = testStore.CreateDefaultContext().Database.GetCosmosClient();
using var iterator = client.GetDatabaseQueryIterator<DatabaseProperties>();

while (iterator.HasMoreResults)
{
var response = await iterator.ReadNextAsync();

// This code will run after the first fixtures are initialized,
// so we keep track of the databases we created in order to not delete them here.
foreach (var db in response.Where(x => x.Id.StartsWith("EF-") && !_createdDatabases!.Contains(x.Id)))
{
await client.GetDatabase(db.Id).DeleteAsync();
}
}

// We do not need to track created databases anymore,
// Since this code only runs once on startup.
_createdDatabases = null;
}


return true;
}
catch (AggregateException aggregate)
Expand Down Expand Up @@ -179,13 +228,34 @@ protected override async Task InitializeAsync(Func<DbContext> createContext, Fun
}
}

public static async Task<bool> DatabaseEnsureCreated(DbContext context)
{
if (TestEnvironment.IsEmulator)
{
// The emulator has a race condition when creating containers concurrently,
// so we need to lock around EnsureCreated
await EmulatorCreateCollectionSemaphore.WaitAsync();
}
try
{
return await context.Database.EnsureCreatedAsync().ConfigureAwait(false);
}
finally
{
if (TestEnvironment.IsEmulator)
{
EmulatorCreateCollectionSemaphore.Release();
}
}
}

private async Task CreateFromFile(DbContext context)
{
if (await EnsureCreatedAsync(context).ConfigureAwait(false))
{
if (!TestEnvironment.UseTokenCredential)
{
await context.Database.EnsureCreatedAsync().ConfigureAwait(false);
await DatabaseEnsureCreated(context).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -267,7 +337,15 @@ public async Task<bool> EnsureCreatedAsync(DbContext context, CancellationToken
if (!TestEnvironment.UseTokenCredential)
{
var cosmosClientWrapper = context.GetService<ICosmosClientWrapper>();
return await cosmosClientWrapper.CreateDatabaseIfNotExistsAsync(null, cancellationToken).ConfigureAwait(false);
var r = await cosmosClientWrapper.CreateDatabaseIfNotExistsAsync(null, cancellationToken).ConfigureAwait(false);
if (r)
{
// Keep track of created databases, to prevent them from being deleted in cleanup function,
// that could run after the creation of this database, due to xunit creating the first test fixtures before checking the availability of the connection.
_createdDatabases?.Add(Name);
}

return r;
}

var databaseAccount = await GetDBAccount(cancellationToken).ConfigureAwait(false);
Expand All @@ -293,7 +371,8 @@ public async Task<bool> EnsureCreatedAsync(DbContext context, CancellationToken
{
sqlDatabaseCreateUpdateContent.Options = new CosmosDBCreateUpdateConfig
{
Throughput = modelThroughput.Throughput, AutoscaleMaxThroughput = modelThroughput.AutoscaleMaxThroughput
Throughput = modelThroughput.Throughput,
AutoscaleMaxThroughput = modelThroughput.AutoscaleMaxThroughput
};
}

Expand Down Expand Up @@ -349,7 +428,8 @@ public override async Task CleanAsync(DbContext context, bool createTables = tru

if (!TestEnvironment.UseTokenCredential)
{
created = await context.Database.EnsureCreatedAsync().ConfigureAwait(false);
created = await DatabaseEnsureCreated(context).ConfigureAwait(false);

if (!created)
{
await SeedAsync(context).ConfigureAwait(false);
Expand Down Expand Up @@ -406,7 +486,8 @@ private async Task CreateContainersAsync(DbContext context)
{
content.Options = new CosmosDBCreateUpdateConfig
{
AutoscaleMaxThroughput = container.Throughput.AutoscaleMaxThroughput, Throughput = container.Throughput.Throughput
AutoscaleMaxThroughput = container.Throughput.AutoscaleMaxThroughput,
Throughput = container.Throughput.Throughput
};
}

Expand Down Expand Up @@ -556,6 +637,7 @@ private static async Task SeedAsync(DbContext context)

public override async ValueTask DisposeAsync()
{
// We don't delete the database if it was created from a data file, because importing it is really slow.
if (_initialized
&& _dataFilePath == null)
{
Expand All @@ -569,6 +651,8 @@ public override async ValueTask DisposeAsync()
GetTestStoreIndex(ServiceProvider).RemoveShared(GetType().Name + Name);
}

// We always delete the database because the emulator will stop responding with much more than ~10 concurrent containers,
// https://learn.microsoft.com/en-us/azure/cosmos-db/emulator#differences-between-the-emulator-and-cloud-service
await EnsureDeletedAsync(_storeContext).ConfigureAwait(false);
}

Expand Down
Loading