< Summary

Information
Class: NGql.Core.Features.MergeMemoTracker
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Features/MergeMemoScope.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 48
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Attach(...)100%1414100%
Invalidate()100%88100%

File(s)

/home/runner/work/NGql/NGql/src/Core/Features/MergeMemoScope.cs

#LineLine coverage
 1namespace NGql.Core.Features;
 2
 3internal sealed class MergeMemoScope
 4{
 5    private long _version;
 6
 7    internal long Version => Interlocked.Read(ref _version);
 8
 9    internal void Invalidate() => Interlocked.Increment(ref _version);
 10}
 11
 12internal sealed class MergeMemoTracker
 13{
 14    private MergeMemoScope? _primary;
 15    private object? _gate;
 16    private volatile List<WeakReference<MergeMemoScope>>? _others;
 17
 18    internal void Attach(MergeMemoScope scope)
 19    {
 332420        if (ReferenceEquals(Volatile.Read(ref _primary), scope)) return;
 119421        var primary = Interlocked.CompareExchange(ref _primary, scope, null);
 222622        if (primary is null || ReferenceEquals(primary, scope)) return;
 16223        lock (LazyInitializer.EnsureInitialized(ref _gate))
 24        {
 16225            _others ??= new();
 250226            for (var i = _others.Count - 1; i >= 0; i--)
 27            {
 116428                if (!_others[i].TryGetTarget(out var target)) _others.RemoveAt(i);
 120329                else if (ReferenceEquals(target, scope)) return;
 30            }
 9931            _others.Add(new WeakReference<MergeMemoScope>(scope));
 9932        }
 16233    }
 34
 35    internal void Invalidate()
 36    {
 108937        Volatile.Read(ref _primary)?.Invalidate();
 216038        if (_others is null) return;
 1839        lock (_gate!)
 40        {
 21041            for (var i = _others.Count - 1; i >= 0; i--)
 42            {
 16243                if (_others[i].TryGetTarget(out var scope)) scope.Invalidate();
 1244                else _others.RemoveAt(i);
 45            }
 1846        }
 1847    }
 48}