Removed unnecessary lock statements and updated tests.

The updated test needs to refresh the lock record entity since the DB record is updated using different connections (from child lifetime scopes).
This commit is contained in:
Sipke Schoorstra
2015-08-22 20:15:20 +01:00
parent 5f4cd14937
commit c5b0cac24a
2 changed files with 38 additions and 29 deletions

View File

@@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Autofac; using Autofac;
using NHibernate.Linq;
using NUnit.Framework; using NUnit.Framework;
using Orchard.Data; using Orchard.Data;
using Orchard.Environment; using Orchard.Environment;
@@ -18,6 +20,8 @@ namespace Orchard.Tests.Tasks {
private StubMachineNameProvider _machineNameProvider; private StubMachineNameProvider _machineNameProvider;
private StubThreadProvider _threadProvider; private StubThreadProvider _threadProvider;
private IRepository<DistributedLockRecord> _distributedLockRepository; private IRepository<DistributedLockRecord> _distributedLockRepository;
private ITransactionManager _transactionManager;
protected override IEnumerable<Type> DatabaseTypes { protected override IEnumerable<Type> DatabaseTypes {
get { yield return typeof(DistributedLockRecord); } get { yield return typeof(DistributedLockRecord); }
@@ -36,6 +40,7 @@ namespace Orchard.Tests.Tasks {
_machineNameProvider = (StubMachineNameProvider)_container.Resolve<IMachineNameProvider>(); _machineNameProvider = (StubMachineNameProvider)_container.Resolve<IMachineNameProvider>();
_threadProvider = (StubThreadProvider)_container.Resolve<IThreadProvider>(); _threadProvider = (StubThreadProvider)_container.Resolve<IThreadProvider>();
_distributedLockRepository = _container.Resolve<IRepository<DistributedLockRecord>>(); _distributedLockRepository = _container.Resolve<IRepository<DistributedLockRecord>>();
_transactionManager = _container.Resolve<ITransactionManager>();
} }
[Test] [Test]
@@ -77,9 +82,11 @@ namespace Orchard.Tests.Tasks {
var lockRecord = _distributedLockRepository.Get(lockId); var lockRecord = _distributedLockRepository.Get(lockId);
_distributedLockService.ReleaseLock(@lock); _distributedLockService.ReleaseLock(@lock);
_session.Refresh(lockRecord);
Assert.That(lockRecord.Count, Is.EqualTo(1)); Assert.That(lockRecord.Count, Is.EqualTo(1));
_distributedLockService.ReleaseLock(@lock); _distributedLockService.ReleaseLock(@lock);
_session.Refresh(lockRecord);
Assert.That(lockRecord.Count, Is.EqualTo(0)); Assert.That(lockRecord.Count, Is.EqualTo(0));
} }
@@ -193,6 +200,7 @@ namespace Orchard.Tests.Tasks {
}; };
_distributedLockRepository.Create(record); _distributedLockRepository.Create(record);
_transactionManager.RequireNew();
return record; return record;
} }

View File

@@ -16,7 +16,6 @@ namespace Orchard.Tasks.Locking.Services {
private readonly IMachineNameProvider _machineNameProvider; private readonly IMachineNameProvider _machineNameProvider;
private readonly ILifetimeScope _lifetimeScope; private readonly ILifetimeScope _lifetimeScope;
private readonly IClock _clock; private readonly IClock _clock;
private readonly object _transactionManagerLock = new object();
private readonly IThreadProvider _threadProvider; private readonly IThreadProvider _threadProvider;
public DistributedLockService(IMachineNameProvider machineNameProvider, IThreadProvider threadProvider, ILifetimeScope lifetimeScope, IClock clock) { public DistributedLockService(IMachineNameProvider machineNameProvider, IThreadProvider threadProvider, ILifetimeScope lifetimeScope, IClock clock) {
@@ -43,7 +42,6 @@ namespace Orchard.Tasks.Locking.Services {
} }
public void ReleaseLock(DistributedLock @lock) { public void ReleaseLock(DistributedLock @lock) {
lock (_transactionManagerLock) {
var childLifetimeScope = CreateChildLifetimeScope(@lock.Name); var childLifetimeScope = CreateChildLifetimeScope(@lock.Name);
try { try {
@@ -69,14 +67,12 @@ namespace Orchard.Tasks.Locking.Services {
childLifetimeScope.Dispose(); childLifetimeScope.Dispose();
} }
} }
}
private bool TryAcquireLock(string name, TimeSpan maxValidFor, TimeSpan? timeout, string machineName, int? threadId, out DistributedLock @lock) { private bool TryAcquireLock(string name, TimeSpan maxValidFor, TimeSpan? timeout, string machineName, int? threadId, out DistributedLock @lock) {
@lock = AcquireLockInternal(name, maxValidFor, machineName, threadId, timeout ?? TimeSpan.Zero); @lock = AcquireLockInternal(name, maxValidFor, machineName, threadId, timeout ?? TimeSpan.Zero);
if (@lock != null) if (@lock != null)
return true; return true;
Logger.Debug("Could not acquire a lock named '{0}'.", name);
return false; return false;
} }
@@ -85,7 +81,7 @@ namespace Orchard.Tasks.Locking.Services {
if (@lock != null) if (@lock != null)
return @lock; return @lock;
throw new TimeoutException("Could not acquire a lock within the specified amount of time."); throw new TimeoutException(String.Format("Failed to acquire a lock named '{0}' within the specified timeout ('{1}').", name, timeout));
} }
private DistributedLock AcquireLockInternal(string name, TimeSpan maxValidFor, string machineName, int? threadId, TimeSpan? timeout = null) { private DistributedLock AcquireLockInternal(string name, TimeSpan maxValidFor, string machineName, int? threadId, TimeSpan? timeout = null) {
@@ -105,11 +101,16 @@ namespace Orchard.Tasks.Locking.Services {
throw; throw;
} }
Logger.Debug(timeout == null
? "Failed to acquire a lock named '{0}'."
: "Failed to acquire a lock named '{0}' within the specified timeout ('{1}')."
, name, timeout);
return null; return null;
} }
private DistributedLockRecord AcquireLockRecord(string name, TimeSpan maxValidFor, string machineName, int? threadId) { private DistributedLockRecord AcquireLockRecord(string name, TimeSpan maxValidFor, string machineName, int? threadId) {
lock (_transactionManagerLock) { //lock (_transactionManagerLock) {
var childLifetimeScope = CreateChildLifetimeScope(name); var childLifetimeScope = CreateChildLifetimeScope(name);
try { try {
@@ -166,7 +167,7 @@ namespace Orchard.Tasks.Locking.Services {
finally { finally {
childLifetimeScope.Dispose(); childLifetimeScope.Dispose();
} }
} //}
} }
/// <summary> /// <summary>