diff --git a/src/Umbraco.Core/Scoping/Scope.cs b/src/Umbraco.Core/Scoping/Scope.cs index 24ef92278cb6..7015cee5ebd0 100644 --- a/src/Umbraco.Core/Scoping/Scope.cs +++ b/src/Umbraco.Core/Scoping/Scope.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Data; using Umbraco.Core.Cache; using Umbraco.Core.Composing; @@ -34,6 +35,13 @@ internal class Scope : IScope2 private ICompletable _fscope; private IEventDispatcher _eventDispatcher; + private object _dictionaryLocker; + + // ReadLocks and WriteLocks if we're the outer most scope it's those owned by the entire chain + // If we're a child scope it's those that we have requested. + internal readonly Dictionary ReadLocks; + internal readonly Dictionary WriteLocks; + // initializes a new scope private Scope(ScopeProvider scopeProvider, ILogger logger, FileSystems fileSystems, Scope parent, ScopeContext scopeContext, bool detachable, @@ -58,6 +66,10 @@ private Scope(ScopeProvider scopeProvider, Detachable = detachable; + _dictionaryLocker = new object(); + ReadLocks = new Dictionary(); + WriteLocks = new Dictionary(); + #if DEBUG_SCOPES _scopeProvider.RegisterScope(this); Console.WriteLine("create " + InstanceId.ToString("N").Substring(0, 8)); @@ -348,6 +360,23 @@ public void Dispose() #endif } + // Decrement the lock counters on the parent if any. + if (ParentScope != null) + { + lock (_dictionaryLocker) + { + foreach (var readLockPair in ReadLocks) + { + DecrementReadLock(readLockPair.Key, readLockPair.Value); + } + + foreach (var writeLockPair in WriteLocks) + { + DecrementWriteLock(writeLockPair.Key, writeLockPair.Value); + } + } + } + var parent = ParentScope; _scopeProvider.AmbientScope = parent; // might be null = this is how scopes are removed from context objects @@ -486,31 +515,259 @@ private static void TryFinally(int index, Action[] actions) private static bool LogUncompletedScopes => (_logUncompletedScopes ?? (_logUncompletedScopes = Current.Configs.CoreDebug().LogUncompletedScopes)).Value; + /// + /// Decrements the count of the ReadLocks with a specific lock object identifier we currently hold + /// + /// Lock object identifier to decrement + /// Amount to decrement the lock count with + public void DecrementReadLock(int lockId, int amountToDecrement) + { + // If we aren't the outermost scope, pass it on to the parent. + if (ParentScope != null) + { + ParentScope.DecrementReadLock(lockId, amountToDecrement); + return; + } + + lock (_dictionaryLocker) + { + ReadLocks[lockId] -= amountToDecrement; + } + } + + /// + /// Decrements the count of the WriteLocks with a specific lock object identifier we currently hold. + /// + /// Lock object identifier to decrement. + /// Amount to decrement the lock count with + public void DecrementWriteLock(int lockId, int amountToDecrement) + { + // If we aren't the outermost scope, pass it on to the parent. + if (ParentScope != null) + { + ParentScope.DecrementWriteLock(lockId, amountToDecrement); + return; + } + + lock (_dictionaryLocker) + { + WriteLocks[lockId] -= amountToDecrement; + } + } + + /// + /// Increment the count of the read locks we've requested + /// + /// + /// This should only be done on child scopes since it's then used to decrement the count later. + /// + /// + private void IncrementRequestedReadLock(params int[] lockIds) + { + // We need to keep track of what lockIds we have requested locks for to be able to decrement them. + if (ParentScope != null) + { + foreach (var lockId in lockIds) + { + lock (_dictionaryLocker) + { + if (ReadLocks.ContainsKey(lockId)) + { + ReadLocks[lockId] += 1; + } + else + { + ReadLocks[lockId] = 1; + } + } + } + } + } + + /// + /// Increment the count of the write locks we've requested + /// + /// + /// This should only be done on child scopes since it's then used to decrement the count later. + /// + /// + private void IncrementRequestedWriteLock(params int[] lockIds) + { + // We need to keep track of what lockIds we have requested locks for to be able to decrement them. + if (ParentScope != null) + { + foreach (var lockId in lockIds) + { + lock (_dictionaryLocker) + { + if (WriteLocks.ContainsKey(lockId)) + { + WriteLocks[lockId] += 1; + } + else + { + WriteLocks[lockId] = 1; + } + } + } + } + } + /// - public void ReadLock(params int[] lockIds) => Database.SqlContext.SqlSyntax.ReadLock(Database, lockIds); + public void ReadLock(params int[] lockIds) + { + IncrementRequestedReadLock(lockIds); + ReadLockInner(null, lockIds); + } /// public void ReadLock(TimeSpan timeout, int lockId) + { + IncrementRequestedReadLock(lockId); + ReadLockInner(timeout, lockId); + } + + /// + public void WriteLock(params int[] lockIds) + { + IncrementRequestedWriteLock(lockIds); + WriteLockInner(null, lockIds); + } + + /// + public void WriteLock(TimeSpan timeout, int lockId) + { + IncrementRequestedWriteLock(lockId); + WriteLockInner(timeout, lockId); + } + + /// + /// Handles acquiring a read lock, will delegate it to the parent if there are any. + /// + /// Optional database timeout in milliseconds. + /// Array of lock object identifiers. + internal void ReadLockInner(TimeSpan? timeout = null, params int[] lockIds) + { + if (ParentScope != null) + { + // Delegate acquiring the lock to the parent if any. + ParentScope.ReadLockInner(timeout, lockIds); + return; + } + + // If we are the parent, then handle the lock request. + foreach (var lockId in lockIds) + { + lock (_dictionaryLocker) + { + // Only acquire the lock if we haven't done so yet. + if (!ReadLocks.ContainsKey(lockId)) + { + if (timeout is null) + { + // We want a lock with a custom timeout + ObtainReadLock(lockId); + } + else + { + // We just want an ordinary lock. + ObtainTimoutReadLock(lockId, timeout.Value); + } + // Add the lockId as a key to the dict. + ReadLocks[lockId] = 0; + } + + ReadLocks[lockId] += 1; + } + } + } + + /// + /// Handles acquiring a write lock with a specified timeout, will delegate it to the parent if there are any. + /// + /// Optional database timeout in milliseconds. + /// Array of lock object identifiers. + internal void WriteLockInner(TimeSpan? timeout = null, params int[] lockIds) + { + if (ParentScope != null) + { + // If we have a parent we delegate lock creation to parent. + ParentScope.WriteLockInner(timeout, lockIds); + return; + } + + foreach (var lockId in lockIds) + { + lock (_dictionaryLocker) + { + // Only acquire lock if we haven't yet (WriteLocks not containing the key) + if (!WriteLocks.ContainsKey(lockId)) + { + if (timeout is null) + { + ObtainWriteLock(lockId); + } + else + { + ObtainTimeoutWriteLock(lockId, timeout.Value); + } + // Add the lockId as a key to the dict. + WriteLocks[lockId] = 0; + } + + // Increment count of the lock by 1. + WriteLocks[lockId] += 1; + } + } + } + + /// + /// Obtains an ordinary read lock. + /// + /// Lock object identifier to lock. + private void ObtainReadLock(int lockId) + { + Database.SqlContext.SqlSyntax.ReadLock(Database, lockId); + } + + /// + /// Obtains a read lock with a custom timeout. + /// + /// Lock object identifier to lock. + /// TimeSpan specifying the timout period. + private void ObtainTimoutReadLock(int lockId, TimeSpan timeout) { var syntax2 = Database.SqlContext.SqlSyntax as ISqlSyntaxProvider2; if (syntax2 == null) { throw new InvalidOperationException($"{Database.SqlContext.SqlSyntax.GetType()} is not of type {typeof(ISqlSyntaxProvider2)}"); } + syntax2.ReadLock(Database, timeout, lockId); } - /// - public void WriteLock(params int[] lockIds) => Database.SqlContext.SqlSyntax.WriteLock(Database, lockIds); + /// + /// Obtains an ordinary write lock. + /// + /// Lock object identifier to lock. + private void ObtainWriteLock(int lockId) + { + Database.SqlContext.SqlSyntax.WriteLock(Database, lockId); + } - /// - public void WriteLock(TimeSpan timeout, int lockId) + /// + /// Obtains a write lock with a custom timeout. + /// + /// Lock object identifier to lock. + /// TimeSpan specifying the timout period. + private void ObtainTimeoutWriteLock(int lockId, TimeSpan timeout) { var syntax2 = Database.SqlContext.SqlSyntax as ISqlSyntaxProvider2; if (syntax2 == null) { throw new InvalidOperationException($"{Database.SqlContext.SqlSyntax.GetType()} is not of type {typeof(ISqlSyntaxProvider2)}"); } + syntax2.WriteLock(Database, timeout, lockId); } } diff --git a/src/Umbraco.Tests/Persistence/LocksTests.cs b/src/Umbraco.Tests/Persistence/LocksTests.cs index 1c651b9040ac..88723292843b 100644 --- a/src/Umbraco.Tests/Persistence/LocksTests.cs +++ b/src/Umbraco.Tests/Persistence/LocksTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Data.SqlServerCe; using System.Linq; using System.Threading; @@ -280,7 +279,7 @@ public void NoDeadLockTest() [Test] public void Throws_When_Lock_Timeout_Is_Exceeded() - { + { var t1 = Task.Run(() => { using (var scope = ScopeProvider.CreateScope()) @@ -288,7 +287,7 @@ public void Throws_When_Lock_Timeout_Is_Exceeded() var realScope = (Scope)scope; Console.WriteLine("Write lock A"); - // This will acquire right away + // This will acquire right away realScope.WriteLock(TimeSpan.FromMilliseconds(2000), Constants.Locks.ContentTree); Thread.Sleep(6000); // Wait longer than the Read Lock B timeout scope.Complete(); @@ -349,7 +348,7 @@ public void Read_Lock_Waits_For_Write_Lock() var realScope = (Scope)scope; Console.WriteLine("Write lock A"); - // This will acquire right away + // This will acquire right away realScope.WriteLock(TimeSpan.FromMilliseconds(2000), Constants.Locks.ContentTree); Thread.Sleep(4000); // Wait less than the Read Lock B timeout scope.Complete(); @@ -377,7 +376,7 @@ public void Read_Lock_Waits_For_Write_Lock() scope.Complete(); Interlocked.Increment(ref locksCompleted); Console.WriteLine("Finished Read lock B"); - } + } }); var t3 = Task.Run(() => diff --git a/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs b/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs new file mode 100644 index 000000000000..32bd7e2afe5a --- /dev/null +++ b/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs @@ -0,0 +1,338 @@ +using System; +using Moq; +using NPoco; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Composing; +using Umbraco.Core.IO; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Scoping; + +namespace Umbraco.Tests.Scoping +{ + [TestFixture] + public class ScopeUnitTests + { + /// + /// Creates a ScopeProvider with mocked internals. + /// + /// The mock of the ISqlSyntaxProvider2, used to count method calls. + /// + private ScopeProvider GetScopeProvider(out Mock syntaxProviderMock) + { + var logger = Mock.Of(); + var fac = Mock.Of(); + var fileSystem = new FileSystems(fac, logger); + var databaseFactory = new Mock(); + var database = new Mock(); + var sqlContext = new Mock(); + syntaxProviderMock = new Mock(); + + // Setup mock of database factory to return mock of database. + databaseFactory.Setup(x => x.CreateDatabase()).Returns(database.Object); + + // Setup mock of database to return mock of sql SqlContext + database.Setup(x => x.SqlContext).Returns(sqlContext.Object); + + // Setup mock of ISqlContext to return syntaxProviderMock + sqlContext.Setup(x => x.SqlSyntax).Returns(syntaxProviderMock.Object); + + return new ScopeProvider(databaseFactory.Object, fileSystem, logger); + } + + [Test] + public void WriteLock_Acquired_Only_Once_Per_Key() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var outerScope = scopeProvider.CreateScope()) + { + outerScope.WriteLock(Constants.Locks.Domains); + outerScope.WriteLock(Constants.Locks.Languages); + + using (var innerScope1 = scopeProvider.CreateScope()) + { + innerScope1.WriteLock(Constants.Locks.Domains); + innerScope1.WriteLock(Constants.Locks.Languages); + + using (var innerScope2 = scopeProvider.CreateScope()) + { + innerScope2.WriteLock(Constants.Locks.Domains); + innerScope2.WriteLock(Constants.Locks.Languages); + innerScope2.Complete(); + } + innerScope1.Complete(); + } + outerScope.Complete(); + } + + syntaxProviderMock.Verify(x => x.WriteLock(It.IsAny(), Constants.Locks.Domains), Times.Once); + syntaxProviderMock.Verify(x => x.WriteLock(It.IsAny(), Constants.Locks.Languages), Times.Once); + } + + [Test] + public void WriteLock_With_Timeout_Acquired_Only_Once_Per_Key(){ + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + var timeout = TimeSpan.FromMilliseconds(10000); + + using (var outerScope = scopeProvider.CreateScope()) + { + var realScope = (Scope) outerScope; + realScope.WriteLock(timeout, Constants.Locks.Domains); + realScope.WriteLock(timeout, Constants.Locks.Languages); + + using (var innerScope1 = scopeProvider.CreateScope()) + { + var realInnerScope1 = (Scope) outerScope; + realInnerScope1.WriteLock(timeout, Constants.Locks.Domains); + realInnerScope1.WriteLock(timeout, Constants.Locks.Languages); + + using (var innerScope2 = scopeProvider.CreateScope()) + { + var realInnerScope2 = (Scope) innerScope2; + realInnerScope2.WriteLock(timeout, Constants.Locks.Domains); + realInnerScope2.WriteLock(timeout, Constants.Locks.Languages); + innerScope2.Complete(); + } + innerScope1.Complete(); + } + + outerScope.Complete(); + } + + syntaxProviderMock.Verify(x => x.WriteLock(It.IsAny(), timeout, Constants.Locks.Domains), Times.Once); + syntaxProviderMock.Verify(x => x.WriteLock(It.IsAny(), timeout, Constants.Locks.Languages), Times.Once); + } + + [Test] + public void ReadLock_Acquired_Only_Once_Per_Key() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var outerScope = scopeProvider.CreateScope()) + { + outerScope.ReadLock(Constants.Locks.Domains); + outerScope.ReadLock(Constants.Locks.Languages); + + using (var innerScope1 = scopeProvider.CreateScope()) + { + innerScope1.ReadLock(Constants.Locks.Domains); + innerScope1.ReadLock(Constants.Locks.Languages); + + using (var innerScope2 = scopeProvider.CreateScope()) + { + innerScope2.ReadLock(Constants.Locks.Domains); + innerScope2.ReadLock(Constants.Locks.Languages); + + innerScope2.Complete(); + } + + innerScope1.Complete(); + } + + outerScope.Complete(); + } + + syntaxProviderMock.Verify(x => x.ReadLock(It.IsAny(), Constants.Locks.Domains), Times.Once); + syntaxProviderMock.Verify(x => x.ReadLock(It.IsAny(), Constants.Locks.Languages), Times.Once); + } + + [Test] + public void ReadLock_With_Timeout_Acquired_Only_Once_Per_Key() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + var timeOut = TimeSpan.FromMilliseconds(10000); + + using (var outerScope = scopeProvider.CreateScope()) + { + var realOuterScope = (Scope) outerScope; + realOuterScope.ReadLock(timeOut, Constants.Locks.Domains); + realOuterScope.ReadLock(timeOut, Constants.Locks.Languages); + + using (var innerScope1 = scopeProvider.CreateScope()) + { + var realInnerScope1 = (Scope) innerScope1; + realInnerScope1.ReadLock(timeOut, Constants.Locks.Domains); + realInnerScope1.ReadLock(timeOut, Constants.Locks.Languages); + + using (var innerScope2 = scopeProvider.CreateScope()) + { + var realInnerScope2 = (Scope) innerScope2; + realInnerScope2.ReadLock(timeOut, Constants.Locks.Domains); + realInnerScope2.ReadLock(timeOut, Constants.Locks.Languages); + + innerScope2.Complete(); + } + + innerScope1.Complete(); + } + + outerScope.Complete(); + } + + syntaxProviderMock.Verify(x => x.ReadLock(It.IsAny(), timeOut, Constants.Locks.Domains), Times.Once); + syntaxProviderMock.Verify(x => x.ReadLock(It.IsAny(), timeOut, Constants.Locks.Languages), Times.Once); + } + + [Test] + public void WriteLocks_Count_correctly_If_Lock_Requested_Twice_In_Scope() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var outerscope = scopeProvider.CreateScope()) + { + var realOuterScope = (Scope) outerscope; + outerscope.WriteLock(Constants.Locks.ContentTree); + outerscope.WriteLock(Constants.Locks.ContentTree); + Assert.AreEqual(2, realOuterScope.WriteLocks[Constants.Locks.ContentTree]); + + using (var innerScope = scopeProvider.CreateScope()) + { + innerScope.WriteLock(Constants.Locks.ContentTree); + innerScope.WriteLock(Constants.Locks.ContentTree); + Assert.AreEqual(4, realOuterScope.WriteLocks[Constants.Locks.ContentTree]); + + innerScope.WriteLock(Constants.Locks.Languages); + innerScope.WriteLock(Constants.Locks.Languages); + Assert.AreEqual(2, realOuterScope.WriteLocks[Constants.Locks.Languages]); + innerScope.Complete(); + } + Assert.AreEqual(0, realOuterScope.WriteLocks[Constants.Locks.Languages]); + Assert.AreEqual(2, realOuterScope.WriteLocks[Constants.Locks.ContentTree]); + outerscope.Complete(); + } + } + + [Test] + public void ReadLocks_Count_correctly_If_Lock_Requested_Twice_In_Scope() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var outerscope = scopeProvider.CreateScope()) + { + var realOuterScope = (Scope) outerscope; + outerscope.ReadLock(Constants.Locks.ContentTree); + outerscope.ReadLock(Constants.Locks.ContentTree); + Assert.AreEqual(2, realOuterScope.ReadLocks[Constants.Locks.ContentTree]); + + using (var innerScope = scopeProvider.CreateScope()) + { + innerScope.ReadLock(Constants.Locks.ContentTree); + innerScope.ReadLock(Constants.Locks.ContentTree); + Assert.AreEqual(4, realOuterScope.ReadLocks[Constants.Locks.ContentTree]); + + innerScope.ReadLock(Constants.Locks.Languages); + innerScope.ReadLock(Constants.Locks.Languages); + Assert.AreEqual(2, realOuterScope.ReadLocks[Constants.Locks.Languages]); + innerScope.Complete(); + } + Assert.AreEqual(0, realOuterScope.ReadLocks[Constants.Locks.Languages]); + Assert.AreEqual(2, realOuterScope.ReadLocks[Constants.Locks.ContentTree]); + outerscope.Complete(); + } + } + + [Test] + public void Nested_Scopes_WriteLocks_Count_Correctly() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var outerScope = scopeProvider.CreateScope()) + { + var parentScope = (Scope) outerScope; + outerScope.WriteLock(Constants.Locks.ContentTree); + outerScope.WriteLock(Constants.Locks.ContentTypes); + + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.ContentTree], $"parentScope after locks acquired: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.ContentTypes], $"parentScope after locks acquired: {nameof(Constants.Locks.ContentTypes)}"); + + using (var innerScope1 = scopeProvider.CreateScope()) + { + innerScope1.WriteLock(Constants.Locks.ContentTree); + innerScope1.WriteLock(Constants.Locks.ContentTypes); + innerScope1.WriteLock(Constants.Locks.Languages); + + Assert.AreEqual(2, parentScope.WriteLocks[Constants.Locks.ContentTree], $"childScope1 after locks acquired: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(2, parentScope.WriteLocks[Constants.Locks.ContentTypes], $"childScope1 after locks acquired: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.Languages], $"childScope1 after locks acquired: {nameof(Constants.Locks.Languages)}"); + + using (var innerScope2 = scopeProvider.CreateScope()) + { + innerScope2.WriteLock(Constants.Locks.ContentTree); + innerScope2.WriteLock(Constants.Locks.MediaTypes); + + Assert.AreEqual(3, parentScope.WriteLocks[Constants.Locks.ContentTree], $"childScope2 after locks acquired: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(2, parentScope.WriteLocks[Constants.Locks.ContentTypes], $"childScope2 after locks acquired: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.Languages], $"childScope2 after locks acquired: {nameof(Constants.Locks.Languages)}"); + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.MediaTypes], $"childScope2 after locks acquired: {nameof(Constants.Locks.MediaTypes)}"); + + innerScope2.Complete(); + } + Assert.AreEqual(2, parentScope.WriteLocks[Constants.Locks.ContentTree], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(2, parentScope.WriteLocks[Constants.Locks.ContentTypes], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.Languages], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.Languages)}"); + Assert.AreEqual(0, parentScope.WriteLocks[Constants.Locks.MediaTypes], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.MediaTypes)}"); + + innerScope1.Complete(); + } + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.ContentTree], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(1, parentScope.WriteLocks[Constants.Locks.ContentTypes], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(0, parentScope.WriteLocks[Constants.Locks.Languages], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.Languages)}"); + Assert.AreEqual(0, parentScope.WriteLocks[Constants.Locks.MediaTypes], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.MediaTypes)}"); + + outerScope.Complete(); + } + } + + [Test] + public void Nested_Scopes_ReadLocks_Count_Correctly() + { + var scopeProvider = GetScopeProvider(out var syntaxProviderMock); + + using (var outerScope = scopeProvider.CreateScope()) + { + var parentScope = (Scope) outerScope; + outerScope.ReadLock(Constants.Locks.ContentTree); + outerScope.ReadLock(Constants.Locks.ContentTypes); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.ContentTree], $"parentScope after locks acquired: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.ContentTypes], $"parentScope after locks acquired: {nameof(Constants.Locks.ContentTypes)}"); + + using (var innserScope1 = scopeProvider.CreateScope()) + { + innserScope1.ReadLock(Constants.Locks.ContentTree); + innserScope1.ReadLock(Constants.Locks.ContentTypes); + innserScope1.ReadLock(Constants.Locks.Languages); + Assert.AreEqual(2, parentScope.ReadLocks[Constants.Locks.ContentTree], $"childScope1 after locks acquired: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(2, parentScope.ReadLocks[Constants.Locks.ContentTypes], $"childScope1 after locks acquired: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.Languages], $"childScope1 after locks acquired: {nameof(Constants.Locks.Languages)}"); + + using (var innerScope2 = scopeProvider.CreateScope()) + { + innerScope2.ReadLock(Constants.Locks.ContentTree); + innerScope2.ReadLock(Constants.Locks.MediaTypes); + Assert.AreEqual(3, parentScope.ReadLocks[Constants.Locks.ContentTree], $"childScope2 after locks acquired: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(2, parentScope.ReadLocks[Constants.Locks.ContentTypes], $"childScope2 after locks acquired: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.Languages], $"childScope2 after locks acquired: {nameof(Constants.Locks.Languages)}"); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.MediaTypes], $"childScope2 after locks acquired: {nameof(Constants.Locks.MediaTypes)}"); + + innerScope2.Complete(); + } + Assert.AreEqual(2, parentScope.ReadLocks[Constants.Locks.ContentTree], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(2, parentScope.ReadLocks[Constants.Locks.ContentTypes], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.Languages], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.Languages)}"); + Assert.AreEqual(0, parentScope.ReadLocks[Constants.Locks.MediaTypes], $"childScope1 after inner scope disposed: {nameof(Constants.Locks.MediaTypes)}"); + + innserScope1.Complete(); + } + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.ContentTree], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.ContentTree)}"); + Assert.AreEqual(1, parentScope.ReadLocks[Constants.Locks.ContentTypes], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.ContentTypes)}"); + Assert.AreEqual(0, parentScope.ReadLocks[Constants.Locks.Languages], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.Languages)}"); + Assert.AreEqual(0, parentScope.ReadLocks[Constants.Locks.MediaTypes], $"parentScope after inner scopes disposed: {nameof(Constants.Locks.MediaTypes)}"); + + outerScope.Complete(); + } + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 27fcebd41ddc..9d4bc4294c25 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -163,6 +163,7 @@ +