Skip to content

Commit

Permalink
Use the new locker methods
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Nov 10, 2024
1 parent 47b3e52 commit 5b16024
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 592 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Entity Framework Core Second Level Caching Library.</Description>
<VersionPrefix>4.8.6</VersionPrefix>
<VersionPrefix>4.8.7</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
7 changes: 3 additions & 4 deletions src/EFCoreSecondLevelCacheInterceptor/ILockProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AsyncKeyedLock;
using System;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -13,10 +12,10 @@ public interface ILockProvider : IDisposable
/// <summary>
/// Tries to enter the sync lock
/// </summary>
AsyncNonKeyedLockReleaser Lock(CancellationToken cancellationToken = default);
IDisposable? Lock(CancellationToken cancellationToken = default);

/// <summary>
/// Tries to enter the async lock
/// </summary>
ValueTask<AsyncNonKeyedLockReleaser> LockAsync(CancellationToken cancellationToken = default);
ValueTask<IDisposable?> LockAsync(CancellationToken cancellationToken = default);
}
15 changes: 7 additions & 8 deletions src/EFCoreSecondLevelCacheInterceptor/LockProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@ namespace EFCoreSecondLevelCacheInterceptor;
public sealed class LockProvider : ILockProvider
{
private readonly AsyncNonKeyedLocker _lock = new();
private readonly TimeSpan _timeout = TimeSpan.FromSeconds(value: 7);

/// <summary>
/// Tries to enter the sync lock
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public AsyncNonKeyedLockReleaser Lock(CancellationToken cancellationToken = default) => _lock.Lock(cancellationToken);
public IDisposable? Lock(CancellationToken cancellationToken = default)
=> _lock.LockOrNull(_timeout, cancellationToken);

/// <summary>
/// Tries to enter the async lock
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ValueTask<AsyncNonKeyedLockReleaser> LockAsync(CancellationToken cancellationToken = default) =>
_lock.LockAsync(cancellationToken);
public ValueTask<IDisposable?> LockAsync(CancellationToken cancellationToken = default)
=> _lock.LockOrNullAsync(_timeout, cancellationToken);

/// <summary>
/// Disposes the lock
/// </summary>
public void Dispose()
{
_lock.Dispose();
}
/// </summary>
public void Dispose() => _lock.Dispose();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using AsyncKeyedLock;

namespace EFCoreSecondLevelCacheInterceptor.UnitTests;

public class LockProviderTests
Expand All @@ -14,7 +12,7 @@ public void Lock_ReturnsNonNullReleaser()
var releaser = lockProvider.Lock();

// Assert
Assert.IsType<AsyncNonKeyedLockReleaser>(releaser);
Assert.IsAssignableFrom<IDisposable>(releaser);
}

[Fact]
Expand All @@ -27,7 +25,7 @@ public async Task LockAsync_ReturnsNonNullReleaser()
var releaser = await lockProvider.LockAsync();

// Assert
Assert.IsType<AsyncNonKeyedLockReleaser>(releaser);
Assert.IsAssignableFrom<IDisposable>(releaser);
}

[Fact]
Expand All @@ -38,10 +36,10 @@ public void Lock_CanBeDisposed()
var releaser = lockProvider.Lock();

// Act
releaser.Dispose();
releaser?.Dispose();

// Assert
Assert.True(true); // If no exception is thrown, the test passes
Assert.True(condition: true); // If no exception is thrown, the test passes
}

[Fact]
Expand All @@ -52,10 +50,10 @@ public async Task LockAsync_CanBeDisposed()
var releaser = await lockProvider.LockAsync();

// Act
releaser.Dispose();
releaser?.Dispose();

// Assert
Assert.True(true); // If no exception is thrown, the test passes
Assert.True(condition: true); // If no exception is thrown, the test passes
}

[Fact]
Expand All @@ -68,6 +66,6 @@ public void Dispose_DisposesLockProvider()
lockProvider.Dispose();

// Assert
Assert.True(true); // If no exception is thrown, the test passes
Assert.True(condition: true); // If no exception is thrown, the test passes
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;

public class MockDisposable : IDisposable
{
private bool _disposed;

public void Dispose()
{
Dispose(disposing: true);

// tell the GC not to finalize
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// Not in destructor, OK to reference other objects
}

// perform cleanup for this object
}

_disposed = true;
}

~MockDisposable() => Dispose(disposing: false);
}
Loading

0 comments on commit 5b16024

Please sign in to comment.