< Summary

Information
Class: Ice.SharedImplicitContext
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/ImplicitContextI.cs
Tag: 71_18251537082
Line coverage
98%
Covered lines: 54
Uncovered lines: 1
Coverable lines: 55
Total lines: 348
Line coverage: 98.1%
Branch coverage
73%
Covered branches: 19
Total branches: 26
Branch coverage: 73%
Method coverage
100%
Covered methods: 9
Total methods: 9
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
getContext()100%11100%
setContext(...)100%44100%
containsKey(...)50%22100%
get(...)50%44100%
put(...)66.67%66100%
remove(...)50%4.03487.5%
write(...)100%44100%
combine(...)100%22100%
.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    {
 149        lock (_mutex)
 50        {
 151            return new Dictionary<string, string>(_context);
 52        }
 153    }
 54
 55    public override void setContext(Dictionary<string, string> context)
 56    {
 157        lock (_mutex)
 58        {
 159            if (context != null && context.Count != 0)
 60            {
 161                _context = new Dictionary<string, string>(context);
 62            }
 63            else
 64            {
 165                _context.Clear();
 66            }
 167        }
 168    }
 69
 70    public override bool containsKey(string key)
 71    {
 172        lock (_mutex)
 73        {
 174            key ??= "";
 75
 176            return _context.ContainsKey(key);
 77        }
 178    }
 79
 80    public override string get(string key)
 81    {
 182        lock (_mutex)
 83        {
 184            key ??= "";
 85
 186            string val = _context[key] ?? "";
 187            return val;
 88        }
 189    }
 90
 91    public override string put(string key, string value)
 92    {
 193        lock (_mutex)
 94        {
 195            key ??= "";
 196            value ??= "";
 97
 198            _context.TryGetValue(key, out string oldVal);
 199            oldVal ??= "";
 1100            _context[key] = value;
 101
 1102            return oldVal;
 103        }
 1104    }
 105
 106    public override string remove(string key)
 107    {
 1108        lock (_mutex)
 109        {
 1110            key ??= "";
 111
 1112            string val = _context[key];
 113
 1114            if (val == null)
 115            {
 0116                val = "";
 117            }
 118            else
 119            {
 1120                _context.Remove(key);
 121            }
 122
 1123            return val;
 124        }
 1125    }
 126
 127    public override void write(Dictionary<string, string> prxContext, OutputStream os)
 128    {
 1129        if (prxContext.Count == 0)
 130        {
 1131            lock (_mutex)
 132            {
 1133                ContextHelper.write(os, _context);
 1134            }
 135        }
 136        else
 137        {
 1138            Dictionary<string, string> ctx = null;
 1139            lock (_mutex)
 140            {
 1141                ctx = _context.Count == 0 ? prxContext : combine(prxContext);
 1142            }
 1143            ContextHelper.write(os, ctx);
 144        }
 1145    }
 146
 147    internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext)
 148    {
 1149        lock (_mutex)
 150        {
 1151            var combined = new Dictionary<string, string>(prxContext);
 1152            foreach (KeyValuePair<string, string> e in _context)
 153            {
 154                try
 155                {
 1156                    combined.Add(e.Key, e.Value);
 1157                }
 1158                catch (System.ArgumentException)
 159                {
 160                    // Ignore.
 1161                }
 162            }
 1163            return combined;
 164        }
 1165    }
 166
 1167    private Dictionary<string, string> _context = new Dictionary<string, string>();
 1168    private readonly object _mutex = new();
 169}
 170
 171internal class PerThreadImplicitContext : ImplicitContextI
 172{
 173    public override Dictionary<string, string> getContext()
 174    {
 175        Dictionary<string, string> threadContext = null;
 176        Thread currentThread = Thread.CurrentThread;
 177        lock (_mutex)
 178        {
 179            if (_map.TryGetValue(currentThread, out Dictionary<string, string> value))
 180            {
 181                threadContext = value;
 182            }
 183        }
 184
 185        threadContext ??= new Dictionary<string, string>();
 186        return threadContext;
 187    }
 188
 189    public override void setContext(Dictionary<string, string> context)
 190    {
 191        if (context == null || context.Count == 0)
 192        {
 193            lock (_mutex)
 194            {
 195                _map.Remove(Thread.CurrentThread);
 196            }
 197        }
 198        else
 199        {
 200            var threadContext = new Dictionary<string, string>(context);
 201
 202            lock (_mutex)
 203            {
 204                _map.Add(Thread.CurrentThread, threadContext);
 205            }
 206        }
 207    }
 208
 209    public override bool containsKey(string key)
 210    {
 211        key ??= "";
 212
 213        Dictionary<string, string> threadContext = null;
 214        lock (_mutex)
 215        {
 216            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 217            {
 218                return false;
 219            }
 220        }
 221
 222        return threadContext.ContainsKey(key);
 223    }
 224
 225    public override string get(string key)
 226    {
 227        key ??= "";
 228
 229        Dictionary<string, string> threadContext = null;
 230        lock (_mutex)
 231        {
 232            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 233            {
 234                return "";
 235            }
 236        }
 237
 238        string val = threadContext[key] ?? "";
 239        return val;
 240    }
 241
 242    public override string put(string key, string value)
 243    {
 244        key ??= "";
 245        value ??= "";
 246
 247        Dictionary<string, string> threadContext = null;
 248        lock (_mutex)
 249        {
 250            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 251            {
 252                threadContext = new Dictionary<string, string>();
 253                _map.Add(Thread.CurrentThread, threadContext);
 254            }
 255        }
 256
 257        if (!threadContext.TryGetValue(key, out string oldVal))
 258        {
 259            oldVal = "";
 260        }
 261
 262        threadContext[key] = value;
 263        return oldVal;
 264    }
 265
 266    public override string remove(string key)
 267    {
 268        key ??= "";
 269
 270        Dictionary<string, string> threadContext = null;
 271        lock (_mutex)
 272        {
 273            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 274            {
 275                return "";
 276            }
 277        }
 278
 279        if (!threadContext.TryGetValue(key, out string val))
 280        {
 281            val = "";
 282        }
 283        else
 284        {
 285            threadContext.Remove(key);
 286        }
 287        return val;
 288    }
 289
 290    public override void write(Dictionary<string, string> prxContext, OutputStream os)
 291    {
 292        Dictionary<string, string> threadContext = null;
 293        lock (_mutex)
 294        {
 295            _map.TryGetValue(Thread.CurrentThread, out threadContext);
 296        }
 297
 298        if (threadContext == null || threadContext.Count == 0)
 299        {
 300            ContextHelper.write(os, prxContext);
 301        }
 302        else if (prxContext.Count == 0)
 303        {
 304            ContextHelper.write(os, threadContext);
 305        }
 306        else
 307        {
 308            var combined = new Dictionary<string, string>(prxContext);
 309            foreach (KeyValuePair<string, string> e in threadContext)
 310            {
 311                try
 312                {
 313                    combined.Add(e.Key, e.Value);
 314                }
 315                catch (System.ArgumentException)
 316                {
 317                    // Ignore.
 318                }
 319            }
 320            ContextHelper.write(os, combined);
 321        }
 322    }
 323
 324    internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext)
 325    {
 326        Dictionary<string, string> threadContext = null;
 327        lock (_mutex)
 328        {
 329            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 330            {
 331                return new Dictionary<string, string>(prxContext);
 332            }
 333        }
 334
 335        var combined = new Dictionary<string, string>(prxContext);
 336        foreach (KeyValuePair<string, string> e in threadContext)
 337        {
 338            combined.Add(e.Key, e.Value);
 339        }
 340        return combined;
 341    }
 342
 343    //
 344    //  map Thread -> Context
 345    //
 346    private readonly Dictionary<Thread, Dictionary<string, string>> _map = new();
 347    private readonly object _mutex = new();
 348}