< Summary

Information
Class: Ice.SharedImplicitContext
Assembly: Ice
File(s): /_/csharp/src/Ice/ImplicitContextI.cs
Tag: 125_37167941578
Line coverage
100%
Covered lines: 53
Uncovered lines: 0
Coverable lines: 53
Total lines: 339
Line coverage: 100%
Branch coverage
76%
Covered branches: 23
Total branches: 30
Branch coverage: 76.6%
Method coverage
100%
Covered methods: 9
Fully covered methods: 9
Total methods: 9
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
getContext()100%11100%
setContext(...)100%44100%
containsKey(...)50%22100%
get(...)66.67%66100%
put(...)66.67%66100%
remove(...)66.67%66100%
write(...)100%44100%
combine(...)100%22100%
.ctor()100%11100%

File(s)

/_/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            return _context.TryGetValue(key, out string val) ? val ?? "" : "";
 87        }
 188    }
 89
 90    public override string put(string key, string value)
 91    {
 192        lock (_mutex)
 93        {
 194            key ??= "";
 195            value ??= "";
 96
 197            _context.TryGetValue(key, out string oldVal);
 198            oldVal ??= "";
 199            _context[key] = value;
 100
 1101            return oldVal;
 102        }
 1103    }
 104
 105    public override string remove(string key)
 106    {
 1107        lock (_mutex)
 108        {
 1109            key ??= "";
 110
 1111            if (_context.TryGetValue(key, out string val))
 112            {
 1113                _context.Remove(key);
 1114                return val ?? "";
 115            }
 116
 1117            return "";
 118        }
 1119    }
 120
 121    public override void write(Dictionary<string, string> prxContext, OutputStream os)
 122    {
 1123        if (prxContext.Count == 0)
 124        {
 1125            lock (_mutex)
 126            {
 1127                ContextHelper.write(os, _context);
 1128            }
 129        }
 130        else
 131        {
 1132            Dictionary<string, string> ctx = null;
 1133            lock (_mutex)
 134            {
 1135                ctx = _context.Count == 0 ? prxContext : combine(prxContext);
 1136            }
 1137            ContextHelper.write(os, ctx);
 138        }
 1139    }
 140
 141    internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext)
 142    {
 1143        lock (_mutex)
 144        {
 1145            var combined = new Dictionary<string, string>(prxContext);
 1146            foreach (KeyValuePair<string, string> e in _context)
 147            {
 148                try
 149                {
 1150                    combined.Add(e.Key, e.Value);
 1151                }
 1152                catch (System.ArgumentException)
 153                {
 154                    // Ignore.
 1155                }
 156            }
 1157            return combined;
 158        }
 1159    }
 160
 1161    private Dictionary<string, string> _context = new Dictionary<string, string>();
 1162    private readonly object _mutex = new();
 163}
 164
 165internal class PerThreadImplicitContext : ImplicitContextI
 166{
 167    public override Dictionary<string, string> getContext()
 168    {
 169        Thread currentThread = Thread.CurrentThread;
 170        lock (_mutex)
 171        {
 172            if (_map.TryGetValue(currentThread, out Dictionary<string, string> value))
 173            {
 174                return new Dictionary<string, string>(value);
 175            }
 176        }
 177
 178        return new Dictionary<string, string>();
 179    }
 180
 181    public override void setContext(Dictionary<string, string> context)
 182    {
 183        if (context == null || context.Count == 0)
 184        {
 185            lock (_mutex)
 186            {
 187                _map.Remove(Thread.CurrentThread);
 188            }
 189        }
 190        else
 191        {
 192            var threadContext = new Dictionary<string, string>(context);
 193
 194            lock (_mutex)
 195            {
 196                _map[Thread.CurrentThread] = threadContext;
 197            }
 198        }
 199    }
 200
 201    public override bool containsKey(string key)
 202    {
 203        key ??= "";
 204
 205        Dictionary<string, string> threadContext = null;
 206        lock (_mutex)
 207        {
 208            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 209            {
 210                return false;
 211            }
 212        }
 213
 214        return threadContext.ContainsKey(key);
 215    }
 216
 217    public override string get(string key)
 218    {
 219        key ??= "";
 220
 221        Dictionary<string, string> threadContext = null;
 222        lock (_mutex)
 223        {
 224            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 225            {
 226                return "";
 227            }
 228        }
 229
 230        return threadContext.TryGetValue(key, out string val) ? val ?? "" : "";
 231    }
 232
 233    public override string put(string key, string value)
 234    {
 235        key ??= "";
 236        value ??= "";
 237
 238        Dictionary<string, string> threadContext = null;
 239        lock (_mutex)
 240        {
 241            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 242            {
 243                threadContext = new Dictionary<string, string>();
 244                _map.Add(Thread.CurrentThread, threadContext);
 245            }
 246        }
 247
 248        if (!threadContext.TryGetValue(key, out string oldVal))
 249        {
 250            oldVal = "";
 251        }
 252
 253        threadContext[key] = value;
 254        return oldVal;
 255    }
 256
 257    public override string remove(string key)
 258    {
 259        key ??= "";
 260
 261        Dictionary<string, string> threadContext = null;
 262        lock (_mutex)
 263        {
 264            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 265            {
 266                return "";
 267            }
 268        }
 269
 270        if (!threadContext.TryGetValue(key, out string val))
 271        {
 272            val = "";
 273        }
 274        else
 275        {
 276            threadContext.Remove(key);
 277        }
 278        return val;
 279    }
 280
 281    public override void write(Dictionary<string, string> prxContext, OutputStream os)
 282    {
 283        Dictionary<string, string> threadContext = null;
 284        lock (_mutex)
 285        {
 286            _map.TryGetValue(Thread.CurrentThread, out threadContext);
 287        }
 288
 289        if (threadContext == null || threadContext.Count == 0)
 290        {
 291            ContextHelper.write(os, prxContext);
 292        }
 293        else if (prxContext.Count == 0)
 294        {
 295            ContextHelper.write(os, threadContext);
 296        }
 297        else
 298        {
 299            var combined = new Dictionary<string, string>(prxContext);
 300            foreach (KeyValuePair<string, string> e in threadContext)
 301            {
 302                try
 303                {
 304                    combined.Add(e.Key, e.Value);
 305                }
 306                catch (System.ArgumentException)
 307                {
 308                    // Ignore.
 309                }
 310            }
 311            ContextHelper.write(os, combined);
 312        }
 313    }
 314
 315    internal override Dictionary<string, string> combine(Dictionary<string, string> prxContext)
 316    {
 317        Dictionary<string, string> threadContext = null;
 318        lock (_mutex)
 319        {
 320            if (!_map.TryGetValue(Thread.CurrentThread, out threadContext))
 321            {
 322                return new Dictionary<string, string>(prxContext);
 323            }
 324        }
 325
 326        var combined = new Dictionary<string, string>(prxContext);
 327        foreach (KeyValuePair<string, string> e in threadContext)
 328        {
 329            combined.Add(e.Key, e.Value);
 330        }
 331        return combined;
 332    }
 333
 334    //
 335    //  map Thread -> Context
 336    //
 337    private readonly Dictionary<Thread, Dictionary<string, string>> _map = new();
 338    private readonly object _mutex = new();
 339}