< Summary

Information
Class: Ice.PerThreadImplicitContext
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/ImplicitContextI.cs
Tag: 71_18251537082
Line coverage
77%
Covered lines: 66
Uncovered lines: 19
Coverable lines: 85
Total lines: 348
Line coverage: 77.6%
Branch coverage
65%
Covered branches: 29
Total branches: 44
Branch coverage: 65.9%
Method coverage
88%
Covered methods: 8
Total methods: 9
Method coverage: 88.8%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
getContext()100%44100%
setContext(...)100%44100%
containsKey(...)50%4.25475%
get(...)50%6.39677.78%
put(...)62.5%8.3883.33%
remove(...)50%6.73672.73%
write(...)100%88100%
combine(...)0%2040%
.ctor()100%11100%

File(s)

/home/runner/work/ice/ice/csharp/src/Ice/ImplicitContextI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3namespace Ice;
 4
 5// The base class for all ImplicitContext implementations
 6public abstract class ImplicitContextI : ImplicitContext
 7{
 8    public static ImplicitContextI create(string kind)
 9    {
 10        if (kind == "None" || kind.Length == 0)
 11        {
 12            return null;
 13        }
 14        else if (kind == "Shared")
 15        {
 16            return new SharedImplicitContext();
 17        }
 18        else if (kind == "PerThread")
 19        {
 20            return new PerThreadImplicitContext();
 21        }
 22        else
 23        {
 24            throw new InitializationException("'" + kind + "' is not a valid value for Ice.ImplicitContext");
 25        }
 26    }
 27
 28    public abstract Dictionary<string, string> getContext();
 29
 30    public abstract void setContext(Dictionary<string, string> newContext);
 31
 32    public abstract bool containsKey(string key);
 33
 34    public abstract string get(string key);
 35
 36    public abstract string put(string key, string value);
 37
 38    public abstract string remove(string key);
 39
 40    public abstract void write(Dictionary<string, string> prxContext, OutputStream os);
 41
 42    internal abstract Dictionary<string, string> combine(Dictionary<string, string> prxContext);
 43}
 44
 45internal class SharedImplicitContext : ImplicitContextI
 46{
 47    public override Dictionary<string, string> getContext()
 48    {
 49        lock (_mutex)
 50        {
 51            return new Dictionary<string, string>(_context);
 52        }
 53    }
 54
 55    public override void setContext(Dictionary<string, string> context)
 56    {
 57        lock (_mutex)
 58        {
 59            if (context != null && context.Count != 0)
 60            {
 61                _context = new Dictionary<string, string>(context);
 62            }
 63            else
 64            {
 65                _context.Clear();
 66            }
 67        }
 68    }
 69
 70    public override bool containsKey(string key)
 71    {
 72        lock (_mutex)
 73        {
 74            key ??= "";
 75
 76            return _context.ContainsKey(key);
 77        }
 78    }
 79
 80    public override string get(string key)
 81    {
 82        lock (_mutex)
 83        {
 84            key ??= "";
 85
 86            string val = _context[key] ?? "";
 87            return val;
 88        }
 89    }
 90
 91    public override string put(string key, string value)
 92    {
 93        lock (_mutex)
 94        {
 95            key ??= "";
 96            value ??= "";
 97
 98            _context.TryGetValue(key, out string oldVal);
 99            oldVal ??= "";
 100            _context[key] = value;
 101
 102            return oldVal;
 103        }
 104    }
 105
 106    public override string remove(string key)
 107    {
 108        lock (_mutex)
 109        {
 110            key ??= "";
 111
 112            string val = _context[key];
 113
 114            if (val == null)
 115            {
 116                val = "";
 117            }
 118            else
 119            {
 120                _context.Remove(key);
 121            }
 122
 123            return val;
 124        }
 125    }
 126
 127    public override void write(Dictionary<string, string> prxContext, OutputStream os)
 128    {
 129        if (prxContext.Count == 0)
 130        {
 131            lock (_mutex)
 132            {
 133                ContextHelper.write(os, _context);
 134            }
 135        }
 136        else
 137        {
 138            Dictionary<string, string> ctx = null;
 139            lock (_mutex)
 140            {
 141                ctx = _context.Count == 0 ? prxContext : combine(prxContext);
 142            }
 143            ContextHelper.write(os, ctx);
 144        }
 145    }
 146
 147    internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext)
 148    {
 149        lock (_mutex)
 150        {
 151            var combined = new Dictionary<string, string>(prxContext);
 152            foreach (KeyValuePair<string, string> e in _context)
 153            {
 154                try
 155                {
 156                    combined.Add(e.Key, e.Value);
 157                }
 158                catch (System.ArgumentException)
 159                {
 160                    // Ignore.
 161                }
 162            }
 163            return combined;
 164        }
 165    }
 166
 167    private Dictionary<string, string> _context = new Dictionary<string, string>();
 168    private readonly object _mutex = new();
 169}
 170
 171internal class PerThreadImplicitContext : ImplicitContextI
 172{
 173    public override Dictionary<string, string> getContext()
 174    {
 1175        Dictionary<string, string> threadContext = null;
 1176        Thread currentThread = Thread.CurrentThread;
 1177        lock (_mutex)
 178        {
 1179            if (_map.TryGetValue(currentThread, out Dictionary<string, string> value))
 180            {
 1181                threadContext = value;
 182            }
 1183        }
 184
 1185        threadContext ??= new Dictionary<string, string>();
 1186        return threadContext;
 187    }
 188
 189    public override void setContext(Dictionary<string, string> context)
 190    {
 1191        if (context == null || context.Count == 0)
 192        {
 1193            lock (_mutex)
 194            {
 1195                _map.Remove(Thread.CurrentThread);
 1196            }
 197        }
 198        else
 199        {
 1200            var threadContext = new Dictionary<string, string>(context);
 201
 1202            lock (_mutex)
 203            {
 1204                _map.Add(Thread.CurrentThread, threadContext);
 1205            }
 206        }
 1207    }
 208
 209    public override bool containsKey(string key)
 210    {
 1211        key ??= "";
 212
 1213        Dictionary<string, string> threadContext = null;
 1214        lock (_mutex)
 215        {
 1216            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 217            {
 0218                return false;
 219            }
 1220        }
 221
 1222        return threadContext.ContainsKey(key);
 0223    }
 224
 225    public override string get(string key)
 226    {
 1227        key ??= "";
 228
 1229        Dictionary<string, string> threadContext = null;
 1230        lock (_mutex)
 231        {
 1232            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 233            {
 0234                return "";
 235            }
 1236        }
 237
 1238        string val = threadContext[key] ?? "";
 1239        return val;
 0240    }
 241
 242    public override string put(string key, string value)
 243    {
 1244        key ??= "";
 1245        value ??= "";
 246
 1247        Dictionary<string, string> threadContext = null;
 1248        lock (_mutex)
 249        {
 1250            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 251            {
 0252                threadContext = new Dictionary<string, string>();
 0253                _map.Add(Thread.CurrentThread, threadContext);
 254            }
 1255        }
 256
 1257        if (!threadContext.TryGetValue(key, out string oldVal))
 258        {
 1259            oldVal = "";
 260        }
 261
 1262        threadContext[key] = value;
 1263        return oldVal;
 264    }
 265
 266    public override string remove(string key)
 267    {
 1268        key ??= "";
 269
 1270        Dictionary<string, string> threadContext = null;
 1271        lock (_mutex)
 272        {
 1273            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 274            {
 0275                return "";
 276            }
 1277        }
 278
 1279        if (!threadContext.TryGetValue(key, out string val))
 280        {
 0281            val = "";
 282        }
 283        else
 284        {
 1285            threadContext.Remove(key);
 286        }
 1287        return val;
 0288    }
 289
 290    public override void write(Dictionary<string, string> prxContext, OutputStream os)
 291    {
 1292        Dictionary<string, string> threadContext = null;
 1293        lock (_mutex)
 294        {
 1295            _map.TryGetValue(Thread.CurrentThread, out threadContext);
 1296        }
 297
 1298        if (threadContext == null || threadContext.Count == 0)
 299        {
 1300            ContextHelper.write(os, prxContext);
 301        }
 1302        else if (prxContext.Count == 0)
 303        {
 1304            ContextHelper.write(os, threadContext);
 305        }
 306        else
 307        {
 1308            var combined = new Dictionary<string, string>(prxContext);
 1309            foreach (KeyValuePair<string, string> e in threadContext)
 310            {
 311                try
 312                {
 1313                    combined.Add(e.Key, e.Value);
 1314                }
 1315                catch (System.ArgumentException)
 316                {
 317                    // Ignore.
 1318                }
 319            }
 1320            ContextHelper.write(os, combined);
 321        }
 1322    }
 323
 324    internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext)
 325    {
 0326        Dictionary<string, string> threadContext = null;
 0327        lock (_mutex)
 328        {
 0329            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 330            {
 0331                return new Dictionary<string, string>(prxContext);
 332            }
 0333        }
 334
 0335        var combined = new Dictionary<string, string>(prxContext);
 0336        foreach (KeyValuePair<string, string> e in threadContext)
 337        {
 0338            combined.Add(e.Key, e.Value);
 339        }
 0340        return combined;
 0341    }
 342
 343    //
 344    //  map Thread -> Context
 345    //
 1346    private readonly Dictionary<Thread, Dictionary<string, string>> _map = new();
 1347    private readonly object _mutex = new();
 348}