< Summary - NetSimpleLazyCache Coverage Report

Information
Class: SingleFactoryCaller<T>
Assembly: SimpleLazyCache
File(s): /home/runner/work/NetSimpleLazyCache/NetSimpleLazyCache/src/SimpleLazyCache/SingleFactoryCaller.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 38
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
GetOrAddAsync()100%22100%

File(s)

/home/runner/work/NetSimpleLazyCache/NetSimpleLazyCache/src/SimpleLazyCache/SingleFactoryCaller.cs

#LineLine coverage
 1
 2using System;
 3using System.Collections.Concurrent;
 4using System.Threading.Tasks;
 5
 6public class SingleFactoryCaller<T> where T : class
 7{
 318    private readonly ConcurrentDictionary<string, Lazy<Task<T>>> _pendingTasks
 319        = new ConcurrentDictionary<string, Lazy<Task<T>>>();
 10
 11    public async Task<T> GetOrAddAsync(string key, Func<Task<T>> valueFactory)
 12    {
 529813        if (string.IsNullOrEmpty(key))
 14        {
 215            throw new ArgumentException("Key cannot be null or empty.", nameof(key));
 16        }
 17
 529618        var lazyValue = _pendingTasks.GetOrAdd(key, k =>
 23819            new Lazy<Task<T>>(
 23820                async () =>
 23821                {
 23822                    try
 23823                    {
 23824                        return await valueFactory();
 23825                    }
 23826                    finally
 23827                    {
 23828                        // Remove the entry only after the task completes (success or failure)
 23829                        // This ensures all concurrent waiters get the same result before cleanup
 23830                        _pendingTasks.TryRemove(key, out _);
 23831                    }
 12832                }
 23833            )
 529634        );
 35
 529636        return await lazyValue.Value;
 518237    }
 38}

Methods/Properties

.ctor()
GetOrAddAsync()