mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Added tests for DistributedService.
This commit is contained in:
@@ -269,6 +269,7 @@
|
||||
<Compile Include="Localization\DateTimePartsTests.cs" />
|
||||
<Compile Include="Localization\DefaultDateLocalizationServicesTests.cs" />
|
||||
<Compile Include="Localization\DefaultDateFormatterTests.cs" />
|
||||
<Compile Include="Stubs\StubDistributedLock.cs" />
|
||||
<Compile Include="Stubs\StubMachineNameProvider.cs" />
|
||||
<Compile Include="Stubs\StubCultureSelector.cs" />
|
||||
<Compile Include="Localization\TestHelpers.cs" />
|
||||
@@ -291,6 +292,7 @@
|
||||
<Compile Include="Stubs\StubVirtualPathMonitor.cs" />
|
||||
<Compile Include="Stubs\StubCacheManager.cs" />
|
||||
<Compile Include="Stubs\StubWebSiteFolder.cs" />
|
||||
<Compile Include="Tasks\DistributedLockServiceTests.cs" />
|
||||
<Compile Include="Time\TimeZoneSelectorTests.cs" />
|
||||
<Compile Include="UI\Resources\ResourceManagerTests.cs" />
|
||||
<Compile Include="UI\ShapeTests.cs" />
|
||||
|
22
src/Orchard.Tests/Stubs/StubDistributedLock.cs
Normal file
22
src/Orchard.Tests/Stubs/StubDistributedLock.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using Orchard.Tasks.Locking;
|
||||
|
||||
namespace Orchard.Tests.Stubs {
|
||||
public class StubDistributedLock : IDistributedLock {
|
||||
public static bool IsAcquired { get; private set; }
|
||||
|
||||
public bool IsDisposed { get; private set; }
|
||||
|
||||
public bool TryAcquire(string name, TimeSpan maxLifetime) {
|
||||
if (IsAcquired)
|
||||
return false;
|
||||
|
||||
return IsAcquired = true;
|
||||
}
|
||||
|
||||
|
||||
public void Dispose() {
|
||||
IsDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
64
src/Orchard.Tests/Tasks/DistributedLockServiceTests.cs
Normal file
64
src/Orchard.Tests/Tasks/DistributedLockServiceTests.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Tasks.Locking;
|
||||
using Orchard.Tests.Stubs;
|
||||
|
||||
namespace Orchard.Tests.Tasks {
|
||||
[TestFixture]
|
||||
public class DistributedLockServiceTests : ContainerTestBase {
|
||||
private DistributedLockService _distributedLockService;
|
||||
|
||||
protected override void Register(ContainerBuilder builder) {
|
||||
builder.RegisterType<StubMachineNameProvider>().As<IMachineNameProvider>();
|
||||
builder.RegisterType<StubDistributedLock>().As<IDistributedLock>();
|
||||
builder.RegisterType<DistributedLockService>().AsSelf();
|
||||
builder.RegisterInstance(new Work<IDistributedLock>(resolve => _container.Resolve<IDistributedLock>())).AsSelf();
|
||||
}
|
||||
|
||||
protected override void Resolve(ILifetimeScope container) {
|
||||
_distributedLockService = container.Resolve<DistributedLockService>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AcquiringLockSucceeds() {
|
||||
IDistributedLock @lock;
|
||||
var lockAcquired = _distributedLockService.TryAcquireLock("Test", TimeSpan.FromSeconds(60), TimeSpan.Zero, out @lock);
|
||||
|
||||
Assert.That(lockAcquired, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AcquiringLockTwiceFails() {
|
||||
IDistributedLock @lock;
|
||||
var attempt1 = _distributedLockService.TryAcquireLock("Test", TimeSpan.FromSeconds(60), TimeSpan.Zero, out @lock);
|
||||
var attempt2 = _distributedLockService.TryAcquireLock("Test", TimeSpan.FromSeconds(60), TimeSpan.Zero, out @lock);
|
||||
|
||||
Assert.That(attempt1, Is.True);
|
||||
Assert.That(attempt2, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleSimultaneousAcquisitionsShouldAllowOneLock() {
|
||||
var attempts = new List<bool>();
|
||||
var tasks = new List<Task>();
|
||||
|
||||
foreach (var index in Enumerable.Range(0, 10).AsParallel()) {
|
||||
var task = Task.Factory.StartNew(() => {
|
||||
IDistributedLock @lock;
|
||||
var attempt = _distributedLockService.TryAcquireLock("Test", TimeSpan.FromSeconds(60), TimeSpan.Zero, out @lock);
|
||||
attempts.Add(attempt);
|
||||
});
|
||||
|
||||
tasks.Add(task);
|
||||
}
|
||||
|
||||
Task.WaitAll(tasks.ToArray());
|
||||
Assert.That(attempts.Count(x => x == true), Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user