< Summary

Information
Class: Anichron.Infrastructure.Data.AdvisoryLockMigrator
Assembly: Anichron.Infrastructure
File(s): /home/runner/work/anichron/anichron/src/Anichron.Infrastructure/Data/AdvisoryLockMigrator.cs
Tag: 19_45211875487
Line coverage
95%
Covered lines: 19
Uncovered lines: 1
Coverable lines: 20
Total lines: 37
Line coverage: 95%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%11100%
RunAsync()100%6694.44%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Infrastructure/Data/AdvisoryLockMigrator.cs

#LineLine coverage
 1namespace Anichron.Infrastructure.Data;
 2
 3public interface IAdvisoryLock
 4{
 5    Task<bool> TryAcquireAsync(CancellationToken ct);
 6    Task ReleaseAsync(CancellationToken ct);
 7}
 8
 49public sealed class AdvisoryLockMigrator(IAdvisoryLock advisoryLock, TimeProvider timeProvider, int maxAttempts)
 10{
 111    private static readonly TimeSpan pollInterval = TimeSpan.FromSeconds(1);
 12
 13    public async Task RunAsync(Func<CancellationToken, Task> migrate, CancellationToken ct)
 414    {
 1615        for (var attempt = 1; attempt <= maxAttempts; attempt++)
 716        {
 717            if (await advisoryLock.TryAcquireAsync(ct))
 318            {
 19                try
 320                {
 321                    await migrate(ct);
 222                    return;
 23                }
 24                finally
 325                {
 326                    await advisoryLock.ReleaseAsync(ct);
 327                }
 028            }
 29
 430            if (attempt < maxAttempts)
 331                await Task.Delay(pollInterval, timeProvider, ct);
 432        }
 33
 134        throw new TimeoutException(
 135            $"Could not acquire the migration advisory lock after {maxAttempts} attempts.");
 236    }
 37}