< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Utilities.KestrunLockRegistry
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/KestrunLockRegistry.cs
Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4
Line coverage
100%
Covered lines: 8
Uncovered lines: 0
Coverable lines: 8
Total lines: 49
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 04/08/2026 - 23:41:36 Line coverage: 100% (8/8) Total lines: 49 Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
GetOrCreate(...)100%11100%
TryGet(...)100%11100%
get_Default()100%11100%
Create()100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/KestrunLockRegistry.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2
 3namespace Kestrun.Utilities;
 4
 5/// <summary>
 6/// Provides a registry of named <see cref="SemaphoreSlim"/> instances used to
 7/// synchronize access to shared resources within the current process.
 8/// The same key always returns the same semaphore instance.
 9/// </summary>
 10public static class KestrunLockRegistry
 11{
 112    private static readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new() { ["default"] = new SemaphoreSlim
 13
 14    /// <summary>
 15    /// Gets an existing semaphore for the specified key, or creates a new one with an initial and maximum count of 1.
 16    /// </summary>
 17    /// <param name="key">The resource key.</param>
 18    /// <returns>A <see cref="SemaphoreSlim"/> that can be awaited to serialize access to the resource identified by <pa
 19    /// <exception cref="ArgumentException">Thrown when <paramref name="key"/> is empty, or whitespace.</exception>
 20    /// <exception cref="ArgumentNullException">Thrown when <paramref name="key"/> is null.</exception>
 21    public static SemaphoreSlim GetOrCreate(string key)
 22    {
 1123        ArgumentException.ThrowIfNullOrWhiteSpace(key);
 1124        return _locks.GetOrAdd(key.Trim().ToLowerInvariant(), static _ => Create());
 25    }
 26
 27    /// <summary>
 28    /// Attempts to get the semaphore associated with the specified key without creating a new one if it doesn't exist.
 29    /// </summary>
 30    /// <param name="key">The resource key.</param>
 31    /// <param name="semaphore">The semaphore associated with the specified key, if it exists.</param>
 32    /// <returns>True if the semaphore exists; otherwise, false.</returns>
 33    /// <exception cref="ArgumentException">Thrown when <paramref name="key"/> is empty, or whitespace.</exception>
 34    /// <exception cref="ArgumentNullException">Thrown when <paramref name="key"/> is null.</exception>
 35    public static bool TryGet(string key, out SemaphoreSlim? semaphore)
 36    {
 537        ArgumentException.ThrowIfNullOrWhiteSpace(key);
 138        key = key.Trim().ToLowerInvariant();
 139        return _locks.TryGetValue(key, out semaphore);
 40    }
 41
 42    /// <summary>
 43    /// Gets a default semaphore instance that can be used for general synchronization needs when a specific key is not 
 44    /// This is useful for simple scenarios where a single lock is sufficient to protect a critical section of code or a
 45    /// </summary>
 146    public static SemaphoreSlim Default => _locks["default"];
 47
 448    private static SemaphoreSlim Create() => new(1, 1);
 49}