* Add UmbracoEFCore project * Add EFCore composer * Add Locking Mechanisms * Add scope interfaces * Add excecute scalar extension method * fix up query in locking mechanism * Add scoping * Add scoping * Add test DbContext classes * add locking test of EFCore * Creat ScopedFileSystemsTests * Add EFCoreScopeInfrastructureScopeLockTests * Add EFCoreScopeInfrastructureScopeTests * Add EFCoreScopeNotificationsTest.cs * Add EFCoreScopeTest.cs * Remake AddUmbracoEFCoreContext to use connection string * Remove unused code from extension method * Reference EFCore reference to Cms.csproj * Remove unused parameter * Dont have default implementation, breaking change instead * Add compatability suppression file * Updated EFCore packages * Use timespan for timeout * Allow overriding default EF Core actions * Option lifetime needs to be singleton * Use given timeout in database call * dont use timespan.zero, use null instead * Use variable timeout * Update test to use locking mechanism * Remove unneccesary duplicate code * Change to catch proper exception number --------- Co-authored-by: Zeegaan <nge@umbraco.dk> Co-authored-by: Bjarke Berg <mail@bergmania.dk>
209 lines
8.3 KiB
C#
209 lines
8.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using NUnit.Framework;
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
|
using Umbraco.Cms.Persistence.EFCore.Scoping;
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
|
using Umbraco.Cms.Tests.Integration.Umbraco.Persistence.EFCore.DbContext;
|
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Persistence.EFCore.Scoping;
|
|
|
|
[TestFixture]
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
|
|
public class EFCoreScopeInfrastructureScopeTests : UmbracoIntegrationTest
|
|
{
|
|
private IEFCoreScopeProvider<TestUmbracoDbContext> EfCoreScopeProvider =>
|
|
GetRequiredService<IEFCoreScopeProvider<TestUmbracoDbContext>>();
|
|
|
|
private IScopeProvider InfrastructureScopeProvider =>
|
|
GetRequiredService<IScopeProvider>();
|
|
|
|
private EFCoreScopeAccessor<TestUmbracoDbContext> EfCoreScopeAccessor => (EFCoreScopeAccessor<TestUmbracoDbContext>)GetRequiredService<IEFCoreScopeAccessor<TestUmbracoDbContext>>();
|
|
|
|
private IScopeAccessor InfrastructureScopeAccessor => GetRequiredService<IScopeAccessor>();
|
|
|
|
[Test]
|
|
public void CanCreateNestedInfrastructureScope()
|
|
{
|
|
Assert.IsNull(EfCoreScopeAccessor.AmbientScope);
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
Assert.IsInstanceOf<EFCoreScope<TestUmbracoDbContext>>(scope);
|
|
Assert.IsNotNull(EfCoreScopeAccessor.AmbientScope);
|
|
Assert.IsNotNull(InfrastructureScopeAccessor.AmbientScope);
|
|
Assert.AreSame(scope, EfCoreScopeAccessor.AmbientScope);
|
|
using (var infrastructureScope = InfrastructureScopeProvider.CreateScope())
|
|
{
|
|
Assert.AreSame(infrastructureScope, InfrastructureScopeAccessor.AmbientScope);
|
|
}
|
|
|
|
Assert.IsNotNull(InfrastructureScopeAccessor.AmbientScope);
|
|
}
|
|
|
|
Assert.IsNull(EfCoreScopeAccessor.AmbientScope);
|
|
Assert.IsNull(InfrastructureScopeAccessor.AmbientScope);
|
|
}
|
|
|
|
[Test]
|
|
public async Task? TransactionWithEfCoreScopeAsParent()
|
|
{
|
|
using (IEfCoreScope<TestUmbracoDbContext> parentScope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await parentScope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
await database.Database.ExecuteSqlAsync($"CREATE TABLE tmp3 (id INT, name NVARCHAR(64))");
|
|
});
|
|
|
|
// This should be using same transaction, so insert data into table we're creating
|
|
using (IScope childScope = InfrastructureScopeProvider.CreateScope())
|
|
{
|
|
childScope.Database.Execute("INSERT INTO tmp3 (id, name) VALUES (1, 'a')");
|
|
string n = ScopeAccessor.AmbientScope.Database.ExecuteScalar<string>(
|
|
"SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", n);
|
|
childScope.Complete();
|
|
}
|
|
|
|
await parentScope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", result);
|
|
});
|
|
|
|
|
|
parentScope.Complete();
|
|
}
|
|
|
|
// Check that its not rolled back
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.IsNotNull(result);
|
|
});
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task? TransactionWithInfrastructureScopeAsParent()
|
|
{
|
|
using (IScope parentScope = InfrastructureScopeProvider.CreateScope())
|
|
{
|
|
parentScope.Database.Execute("CREATE TABLE tmp3 (id INT, name NVARCHAR(64))");
|
|
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
await database.Database.ExecuteSqlAsync($"INSERT INTO tmp3 (id, name) VALUES (1, 'a')");
|
|
|
|
string? result =
|
|
await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", result);
|
|
});
|
|
|
|
scope.Complete();
|
|
}
|
|
|
|
parentScope.Complete();
|
|
}
|
|
|
|
// Check that its not rolled back
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.IsNotNull(result);
|
|
});
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task EFCoreAsParent_DontCompleteWhenChildScopeDoesNotComplete()
|
|
{
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
await database.Database.ExecuteSqlAsync($"CREATE TABLE tmp3 (id INT, name NVARCHAR(64))");
|
|
});
|
|
scope.Complete();
|
|
}
|
|
|
|
using (IEfCoreScope<TestUmbracoDbContext> parentScope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
using (IScope scope = InfrastructureScopeProvider.CreateScope())
|
|
{
|
|
scope.Database.Execute("INSERT INTO tmp3 (id, name) VALUES (1, 'a')");
|
|
string n = ScopeAccessor.AmbientScope.Database.ExecuteScalar<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", n);
|
|
}
|
|
|
|
await parentScope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
// Should still be in transaction and not rolled back yet
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", result);
|
|
});
|
|
|
|
parentScope.Complete();
|
|
}
|
|
|
|
// Check that its rolled back
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
// Should still be in transaction and not rolled back yet
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.IsNull(result);
|
|
});
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task InfrastructureScopeAsParent_DontCompleteWhenChildScopeDoesNotComplete()
|
|
{
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
await database.Database.ExecuteSqlAsync($"CREATE TABLE tmp3 (id INT, name NVARCHAR(64))");
|
|
});
|
|
|
|
scope.Complete();
|
|
}
|
|
|
|
using (IScope parentScope = InfrastructureScopeProvider.CreateScope())
|
|
{
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
await database.Database.ExecuteSqlAsync($"INSERT INTO tmp3 (id, name) VALUES (1, 'a')");
|
|
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", result);
|
|
});
|
|
|
|
string n = parentScope.Database.ExecuteScalar<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.AreEqual("a", n);
|
|
}
|
|
|
|
parentScope.Complete();
|
|
}
|
|
|
|
// Check that its rolled back
|
|
using (IEfCoreScope<TestUmbracoDbContext> scope = EfCoreScopeProvider.CreateScope())
|
|
{
|
|
await scope.ExecuteWithContextAsync<Task>(async database =>
|
|
{
|
|
string? result = await database.Database.ExecuteScalarAsync<string>("SELECT name FROM tmp3 WHERE id=1");
|
|
Assert.IsNull(result);
|
|
});
|
|
}
|
|
}
|
|
}
|