mirror of https://github.com/abpframework/abp.git
16 changed files with 286 additions and 1 deletions
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
|||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
|||
<xs:element name="Weavers"> |
|||
<xs:complexType> |
|||
<xs:all> |
|||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
|||
<xs:complexType> |
|||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:all> |
|||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
|||
<xs:annotation> |
|||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\configureawait.props" /> |
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.DistributedLocking.Abstractions</AssemblyName> |
|||
<PackageId>Volo.Abp.DistributedLocking.Abstractions</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public class AbpDistributedLockingAbstractionsModule : AbpModule |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public interface IAbpDistributedLock |
|||
{ |
|||
/// <summary>
|
|||
/// Tries to acquire a named lock.
|
|||
/// Returns a disposable object to release the lock.
|
|||
/// It is suggested to use this method within a using block.
|
|||
/// Returns null if the lock could not be handled.
|
|||
/// </summary>
|
|||
/// <param name="name">The name of the lock</param>
|
|||
/// <param name="timeout">Timeout value</param>
|
|||
/// <param name="cancellationToken">Cancellation token</param>
|
|||
[ItemCanBeNull] |
|||
Task<IAbpDistributedLockHandle> TryAcquireAsync( |
|||
string name, |
|||
TimeSpan timeout = default, |
|||
CancellationToken cancellationToken = default |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public interface IAbpDistributedLockHandle : IAsyncDisposable |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency |
|||
{ |
|||
private readonly ConcurrentDictionary<string, SemaphoreSlim> _localSyncObjects = new(); |
|||
|
|||
public async Task<IAbpDistributedLockHandle> TryAcquireAsync( |
|||
string name, |
|||
TimeSpan timeout = default, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
var semaphore = _localSyncObjects.GetOrAdd(name, _ => new SemaphoreSlim(1, 1)); |
|||
|
|||
if (!await semaphore.WaitAsync(timeout, cancellationToken)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return new LocalAbpDistributedLockHandle(semaphore); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle |
|||
{ |
|||
private readonly SemaphoreSlim _semaphore; |
|||
|
|||
public LocalAbpDistributedLockHandle(SemaphoreSlim semaphore) |
|||
{ |
|||
_semaphore = semaphore; |
|||
} |
|||
|
|||
public ValueTask DisposeAsync() |
|||
{ |
|||
_semaphore.Release(); |
|||
return default; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.test.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net6.0</TargetFramework> |
|||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.DistributedLocking.Abstractions\Volo.Abp.DistributedLocking.Abstractions.csproj" /> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,12 @@ |
|||
using Volo.Abp.Testing; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public class AbpDistributedLockingAbstractionsTestBase : AbpIntegratedTest<AbpDistributedLockingAbstractionsTestModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpTestBaseModule), |
|||
typeof(AbpDistributedLockingAbstractionsModule), |
|||
typeof(AbpAutofacModule) |
|||
)] |
|||
public class AbpDistributedLockingAbstractionsTestModule : AbpModule |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.DistributedLocking |
|||
{ |
|||
public class LocalDistributedLock_Tests : AbpDistributedLockingAbstractionsTestBase |
|||
{ |
|||
private readonly IAbpDistributedLock _distributedLock; |
|||
|
|||
public LocalDistributedLock_Tests() |
|||
{ |
|||
_distributedLock = GetRequiredService<IAbpDistributedLock>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Be_Instance_Of_LocalAbpDistributedLock() |
|||
{ |
|||
_distributedLock.ShouldBeOfType<LocalAbpDistributedLock>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Lock_With_TryAcquire() |
|||
{ |
|||
await using (var handle = await _distributedLock.TryAcquireAsync("lock1")) |
|||
{ |
|||
handle.ShouldNotBeNull(); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Acquire_If_Already_Locked() |
|||
{ |
|||
await using (var handle = await _distributedLock.TryAcquireAsync("lock1")) |
|||
{ |
|||
handle.ShouldNotBeNull(); |
|||
|
|||
await Task.Run(async () => |
|||
{ |
|||
await using (var handle2 = await _distributedLock.TryAcquireAsync("lock1")) |
|||
{ |
|||
handle2.ShouldBeNull(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
await Task.Run(async () => |
|||
{ |
|||
await using (var handle = await _distributedLock.TryAcquireAsync("lock1")) |
|||
{ |
|||
handle.ShouldNotBeNull(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Obtain_Multiple_Locks() |
|||
{ |
|||
await using (var handle = await _distributedLock.TryAcquireAsync("lock1")) |
|||
{ |
|||
handle.ShouldNotBeNull(); |
|||
|
|||
await Task.Run(async () => |
|||
{ |
|||
await using (var handle2 = await _distributedLock.TryAcquireAsync("lock2")) |
|||
{ |
|||
handle2.ShouldNotBeNull(); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue