< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.InstanceContextManager
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/InstanceContextManager.cs
Line coverage
38%
Covered lines: 42
Uncovered lines: 66
Coverable lines: 108
Total lines: 268
Line coverage: 38.8%
Branch coverage
35%
Covered branches: 14
Total branches: 40
Branch coverage: 35%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
Add(...)75%8880%
AddItem(...)100%11100%
CloseInitiateAsync()0%660%
CloseInputAsync()0%220%
ContinueCloseInstanceContext()100%110%
GrowItems()100%44100%
InitItems(...)100%22100%
OnAbort()0%220%
OnCloseAsync()100%110%
Remove(...)50%4478.57%
ToArray()0%12120%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/InstanceContextManager.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using CoreWCF.Channels;
 9
 10namespace CoreWCF.Dispatcher
 11{
 12    internal interface IInstanceContextManager
 13    {
 14        void Abort();
 15        void Add(InstanceContext instanceContext);
 16        Task CloseAsync(CancellationToken token);
 17        Task CloseInputAsync(CancellationToken token);
 18        bool Remove(InstanceContext instanceContext);
 19        InstanceContext[] ToArray();
 20    }
 21
 22    internal class InstanceContextManager : LifetimeManager, IInstanceContextManager
 23    {
 24        private int _firstFreeIndex;
 25        private Item[] _items;
 26
 27        public InstanceContextManager(object mutex)
 59128            : base(mutex)
 29        {
 59130        }
 31
 32        public void Add(InstanceContext instanceContext)
 33        {
 246334            bool added = false;
 35
 246336            lock (ThisLock)
 37            {
 246338                if (State == LifetimeState.Opened)
 39                {
 246340                    if (instanceContext.InstanceContextManagerIndex != 0)
 41                    {
 042                        return;
 43                    }
 44
 246345                    if (_firstFreeIndex == 0)
 46                    {
 50547                        GrowItems();
 48                    }
 49
 246350                    AddItem(instanceContext);
 246351                    base.IncrementBusyCountWithoutLock();
 246352                    added = true;
 53                }
 246354            }
 55
 246356            if (!added)
 57            {
 058                instanceContext.Abort();
 059                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().ToString
 60            }
 246361        }
 62
 63        private void AddItem(InstanceContext instanceContext)
 64        {
 264865            int index = _firstFreeIndex;
 264866            _firstFreeIndex = _items[index].nextFreeIndex;
 264867            _items[index].instanceContext = instanceContext;
 264868            instanceContext.InstanceContextManagerIndex = index;
 264869        }
 70
 71        private async Task CloseInitiateAsync(CancellationToken token)
 72        {
 073            InstanceContext[] instances = ToArray();
 074            for (int index = 0; index < instances.Length; index++)
 75            {
 076                InstanceContext instance = instances[index];
 77                try
 78                {
 079                    if (instance.State == CommunicationState.Opened)
 80                    {
 081                        Task result = instance.CloseAsync(token);
 082                        if (!result.IsCompleted)
 83                        {
 084                            ContinueCloseInstanceContext(result);
 085                            continue;
 86                        }
 87
 088                        await result;
 89                    }
 90                    else
 91                    {
 092                        instance.Abort();
 93                    }
 094                }
 95                catch (ObjectDisposedException e)
 96                {
 097                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 098                }
 99                catch (InvalidOperationException e)
 100                {
 0101                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0102                }
 103                catch (CommunicationException e)
 104                {
 0105                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0106                }
 107                catch (TimeoutException e)
 108                {
 109                    //if (TD.CloseTimeoutIsEnabled())
 110                    //{
 111                    //    TD.CloseTimeout(e.Message);
 112                    //}
 0113                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0114                }
 115            }
 0116        }
 117
 118        public async Task CloseInputAsync(CancellationToken token)
 119        {
 0120            InstanceContext[] instances = ToArray();
 0121            for (int index = 0; index < instances.Length; index++)
 122            {
 0123                await instances[index].CloseInputAsync(token);
 124            }
 0125        }
 126
 127        private static async void ContinueCloseInstanceContext(Task result)
 128        {
 129            try
 130            {
 0131                await result;
 0132            }
 133            catch (ObjectDisposedException e)
 134            {
 0135                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0136            }
 137            catch (InvalidOperationException e)
 138            {
 0139                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0140            }
 141            catch (CommunicationException e)
 142            {
 0143                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0144            }
 145            catch (TimeoutException e)
 146            {
 147                //if (TD.CloseTimeoutIsEnabled())
 148                //{
 149                //    TD.CloseTimeout(e.Message);
 150                //}
 0151                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
 0152            }
 0153        }
 154
 155        private void GrowItems()
 156        {
 505157            Item[] existingItems = _items;
 505158            if (existingItems != null)
 159            {
 15160                InitItems(existingItems.Length * 2);
 400161                for (int i = 1; i < existingItems.Length; i++)
 162                {
 185163                    AddItem(existingItems[i].instanceContext);
 164                }
 165            }
 166            else
 167            {
 490168                InitItems(4);
 169            }
 490170        }
 171
 172        private void InitItems(int count)
 173        {
 505174            _items = new Item[count];
 3710175            for (int i = count - 2; i > 0; i--)
 176            {
 1350177                _items[i].nextFreeIndex = i + 1;
 178            }
 505179            _firstFreeIndex = 1;
 505180        }
 181
 182        protected override void OnAbort()
 183        {
 0184            InstanceContext[] instances = ToArray();
 0185            for (int index = 0; index < instances.Length; index++)
 186            {
 0187                instances[index].Abort();
 188            }
 189
 0190            base.OnAbort();
 0191        }
 192
 193        protected override async Task OnCloseAsync(CancellationToken token)
 194        {
 0195            await CloseInitiateAsync(token);
 0196            await base.OnCloseAsync(token);
 0197        }
 198
 199        public bool Remove(InstanceContext instanceContext)
 200        {
 2390201            if (instanceContext == null)
 202            {
 0203                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(instanceContext));
 204            }
 205
 2390206            lock (ThisLock)
 207            {
 2390208                int index = instanceContext.InstanceContextManagerIndex;
 2390209                if (index == 0)
 210                {
 0211                    return false;
 212                }
 213
 2390214                instanceContext.InstanceContextManagerIndex = 0;
 2390215                _items[index].nextFreeIndex = _firstFreeIndex;
 2390216                _items[index].instanceContext = null;
 2390217                _firstFreeIndex = index;
 2390218            }
 219
 2390220            DecrementBusyCount();
 2390221            return true;
 0222        }
 223
 224        public InstanceContext[] ToArray()
 225        {
 0226            if (_items == null)
 227            {
 0228                return Array.Empty<InstanceContext>();
 229            }
 230
 0231            lock (ThisLock)
 232            {
 0233                int count = 0;
 0234                for (int i = 1; i < _items.Length; i++)
 235                {
 0236                    if (_items[i].instanceContext != null)
 237                    {
 0238                        count++;
 239                    }
 240                }
 241
 0242                if (count == 0)
 243                {
 0244                    return Array.Empty<InstanceContext>();
 245                }
 246
 0247                InstanceContext[] array = new InstanceContext[count];
 0248                count = 0;
 0249                for (int i = 1; i < _items.Length; i++)
 250                {
 0251                    InstanceContext instanceContext = _items[i].instanceContext;
 0252                    if (instanceContext != null)
 253                    {
 0254                        array[count++] = instanceContext;
 255                    }
 256                }
 257
 0258                return array;
 259            }
 0260        }
 261
 262        private struct Item
 263        {
 264            public int nextFreeIndex;
 265            public InstanceContext instanceContext;
 266        }
 267    }
 268}