< Summary

Information
Class: IceDiscovery.LocatorRegistryI
Assembly: IceDiscovery
File(s): /home/runner/work/ice/ice/csharp/src/IceDiscovery/LocatorI.cs
Tag: 71_18251537082
Line coverage
94%
Covered lines: 63
Uncovered lines: 4
Coverable lines: 67
Total lines: 175
Line coverage: 94%
Branch coverage
96%
Covered branches: 31
Total branches: 32
Branch coverage: 96.8%
Method coverage
83%
Covered methods: 5
Total methods: 6
Method coverage: 83.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
setAdapterDirectProxyAsync(...)100%22100%
setReplicatedAdapterDirectProxyAsync(...)100%88100%
setServerProcessProxyAsync(...)100%210%
findObject(...)90%10.221086.96%
findAdapter(...)100%1212100%

File(s)

/home/runner/work/ice/ice/csharp/src/IceDiscovery/LocatorI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3namespace IceDiscovery;
 4
 5internal class LocatorRegistryI : Ice.LocatorRegistryDisp_
 6{
 17    public
 18    LocatorRegistryI(Ice.Communicator com) =>
 19        _wellKnownProxy = Ice.ObjectPrxHelper.createProxy(com, "p").ice_locator(null)
 110            .ice_router(null).ice_collocationOptimized(true);
 11
 12    public override Task
 13    setAdapterDirectProxyAsync(string adapterId, Ice.ObjectPrx proxy, Ice.Current current)
 14    {
 115        lock (_mutex)
 16        {
 117            if (proxy != null)
 18            {
 119                _adapters[adapterId] = proxy;
 20            }
 21            else
 22            {
 123                _adapters.Remove(adapterId);
 24            }
 125        }
 126        return Task.CompletedTask;
 27    }
 28
 29    public override Task
 30    setReplicatedAdapterDirectProxyAsync(
 31        string adapterId,
 32        string replicaGroupId,
 33        Ice.ObjectPrx proxy,
 34        Ice.Current current)
 35    {
 136        lock (_mutex)
 37        {
 138            if (proxy != null)
 39            {
 140                _adapters[adapterId] = proxy;
 141                if (!_replicaGroups.TryGetValue(replicaGroupId, out HashSet<string> adapterIds))
 42                {
 143                    adapterIds = new HashSet<string>();
 144                    _replicaGroups.Add(replicaGroupId, adapterIds);
 45                }
 146                adapterIds.Add(adapterId);
 47            }
 48            else
 49            {
 150                _adapters.Remove(adapterId);
 151                if (_replicaGroups.TryGetValue(replicaGroupId, out HashSet<string> adapterIds))
 52                {
 153                    adapterIds.Remove(adapterId);
 154                    if (adapterIds.Count == 0)
 55                    {
 156                        _replicaGroups.Remove(replicaGroupId);
 57                    }
 58                }
 59            }
 160        }
 161        return Task.CompletedTask;
 62    }
 63
 64    public override Task
 065    setServerProcessProxyAsync(string id, Ice.ProcessPrx process, Ice.Current current) => Task.CompletedTask;
 66
 67    internal Ice.ObjectPrx findObject(Ice.Identity id)
 68    {
 169        lock (_mutex)
 70        {
 171            if (id.name.Length == 0)
 72            {
 073                return null;
 74            }
 75
 176            Ice.ObjectPrx prx = _wellKnownProxy.ice_identity(id);
 77
 178            var adapterIds = new List<string>();
 179            foreach (KeyValuePair<string, HashSet<string>> entry in _replicaGroups)
 80            {
 81                try
 82                {
 183                    prx.ice_adapterId(entry.Key).ice_ping();
 184                    adapterIds.Add(entry.Key);
 185                }
 086                catch (Ice.Exception)
 87                {
 088                }
 89            }
 190            if (adapterIds.Count == 0)
 91            {
 192                foreach (KeyValuePair<string, Ice.ObjectPrx> entry in _adapters)
 93                {
 94                    try
 95                    {
 196                        prx.ice_adapterId(entry.Key).ice_ping();
 197                        adapterIds.Add(entry.Key);
 198                    }
 199                    catch (Ice.Exception)
 100                    {
 1101                    }
 102                }
 103            }
 104
 1105            if (adapterIds.Count == 0)
 106            {
 1107                return null;
 108            }
 1109            Ice.UtilInternal.Collections.Shuffle(ref adapterIds);
 1110            return prx.ice_adapterId(adapterIds[0]);
 111        }
 1112    }
 113
 114    internal Ice.ObjectPrx findAdapter(string adapterId, out bool isReplicaGroup)
 115    {
 1116        lock (_mutex)
 117        {
 1118            if (_adapters.TryGetValue(adapterId, out Ice.ObjectPrx result))
 119            {
 1120                isReplicaGroup = false;
 1121                return result;
 122            }
 123
 1124            if (_replicaGroups.TryGetValue(adapterId, out HashSet<string> adapterIds))
 125            {
 1126                var endpoints = new List<Ice.Endpoint>();
 1127                foreach (string a in adapterIds)
 128                {
 1129                    if (!_adapters.TryGetValue(a, out Ice.ObjectPrx proxy))
 130                    {
 131                        continue; // TODO: Inconsistency
 132                    }
 133
 1134                    result ??= proxy;
 135
 1136                    endpoints.AddRange(proxy.ice_getEndpoints());
 137                }
 138
 1139                if (result != null)
 140                {
 1141                    isReplicaGroup = true;
 1142                    return result.ice_endpoints(endpoints.ToArray());
 143                }
 144            }
 145
 1146            isReplicaGroup = false;
 1147            return null;
 148        }
 1149    }
 150
 151    private readonly Ice.ObjectPrx _wellKnownProxy;
 1152    private readonly Dictionary<string, Ice.ObjectPrx> _adapters = new Dictionary<string, Ice.ObjectPrx>();
 1153    private readonly Dictionary<string, HashSet<string>> _replicaGroups = new Dictionary<string, HashSet<string>>();
 1154    private readonly object _mutex = new();
 155}
 156
 157internal class LocatorI : Ice.LocatorDisp_
 158{
 159    public LocatorI(LookupI lookup, Ice.LocatorRegistryPrx registry)
 160    {
 161        _lookup = lookup;
 162        _registry = registry;
 163    }
 164
 165    public override Task<Ice.ObjectPrx>
 166    findObjectByIdAsync(Ice.Identity id, Ice.Current current) => _lookup.findObject(id);
 167
 168    public override Task<Ice.ObjectPrx>
 169    findAdapterByIdAsync(string adapterId, Ice.Current current) => _lookup.findAdapter(adapterId);
 170
 171    public override Ice.LocatorRegistryPrx getRegistry(Ice.Current current) => _registry;
 172
 173    private readonly LookupI _lookup;
 174    private readonly Ice.LocatorRegistryPrx _registry;
 175}