< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.EndpointDispatcherTable
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/EndpointDispatcherTable.cs
Line coverage
64%
Covered lines: 41
Uncovered lines: 23
Coverable lines: 64
Total lines: 159
Line coverage: 64%
Branch coverage
54%
Covered branches: 24
Total branches: 44
Branch coverage: 54.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
AddEndpoint(...)50%8850%
RemoveEndpoint(...)0%660%
LookupInCache(...)75%202087.5%
Lookup(...)83.33%6687.5%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/EndpointDispatcherTable.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.Collections.Generic;
 5using CoreWCF.Channels;
 6using CoreWCF.Diagnostics;
 7
 8namespace CoreWCF.Dispatcher
 9{
 10    internal class EndpointDispatcherTable
 11    {
 12        private MessageFilterTable<EndpointDispatcher> _filters;
 13        private const int optimizationThreshold = 2;
 14        private List<EndpointDispatcher> _cachedEndpoints;
 15
 66316        public EndpointDispatcherTable(object thisLock)
 17        {
 66318            ThisLock = thisLock;
 66319        }
 20
 21        public int Count
 22        {
 23            get
 24            {
 025                return ((_cachedEndpoints != null) ? _cachedEndpoints.Count : 0) +
 026                    ((_filters != null) ? _filters.Count : 0);
 27            }
 28        }
 29
 66330        private object ThisLock { get; }
 31
 32        public void AddEndpoint(EndpointDispatcher endpoint)
 33        {
 66134            lock (ThisLock)
 35            {
 66136                MessageFilter filter = endpoint.EndpointFilter;
 66137                int priority = endpoint.FilterPriority;
 38
 66139                if (_filters == null)
 40                {
 66141                    if (_cachedEndpoints == null)
 42                    {
 66043                        _cachedEndpoints = new List<EndpointDispatcher>(optimizationThreshold);
 44                    }
 45
 66146                    if (_cachedEndpoints.Count < optimizationThreshold)
 47                    {
 66148                        _cachedEndpoints.Add(endpoint);
 49                    }
 50                    else
 51                    {
 052                        _filters = new MessageFilterTable<EndpointDispatcher>();
 053                        for (int i = 0; i < _cachedEndpoints.Count; i++)
 54                        {
 055                            int cachedPriority = _cachedEndpoints[i].FilterPriority;
 056                            MessageFilter cachedFilter = _cachedEndpoints[i].EndpointFilter;
 057                            _filters.Add(cachedFilter, _cachedEndpoints[i], cachedPriority);
 58                        }
 059                        _filters.Add(filter, endpoint, priority);
 060                        _cachedEndpoints = null;
 61                    }
 62                }
 63                else
 64                {
 065                    _filters.Add(filter, endpoint, priority);
 66                }
 067            }
 66168        }
 69
 70        public void RemoveEndpoint(EndpointDispatcher endpoint)
 71        {
 072            lock (ThisLock)
 73            {
 074                if (_filters == null)
 75                {
 076                    if (_cachedEndpoints != null && _cachedEndpoints.Contains(endpoint))
 77                    {
 078                        _cachedEndpoints.Remove(endpoint);
 79                    }
 80                }
 81                else
 82                {
 083                    MessageFilter filter = endpoint.EndpointFilter;
 084                    _filters.Remove(filter);
 85                }
 086            }
 087        }
 88
 89        private EndpointDispatcher LookupInCache(Message message, out bool addressMatched)
 90        {
 252091            EndpointDispatcher result = null;
 252092            int priority = int.MinValue;
 252093            bool duplicatePriority = false;
 252094            addressMatched = false;
 95
 252096            if (_cachedEndpoints != null && _cachedEndpoints.Count > 0)
 97            {
 1008098                for (int i = 0; i < _cachedEndpoints.Count; i++)
 99                {
 2520100                    EndpointDispatcher cachedEndpoint = _cachedEndpoints[i];
 2520101                    int cachedPriority = cachedEndpoint.FilterPriority;
 2520102                    MessageFilter cachedFilter = cachedEndpoint.EndpointFilter;
 103
 104                    bool matchResult;
 2520105                    if (cachedFilter is AndMessageFilter andFilter)
 106                    {
 2475107                        matchResult = andFilter.Match(message, out bool addressResult);
 2475108                        addressMatched |= addressResult;
 109                    }
 110                    else
 111                    {
 45112                        matchResult = cachedFilter.Match(message);
 113                    }
 114
 2520115                    if (matchResult)
 116                    {
 2516117                        addressMatched = true;
 2516118                        if (cachedPriority > priority || result == null)
 119                        {
 2516120                            result = cachedEndpoint;
 2516121                            priority = cachedPriority;
 2516122                            duplicatePriority = false;
 123                        }
 0124                        else if (cachedPriority == priority && result != null)
 125                        {
 0126                            duplicatePriority = true;
 127                        }
 128                    }
 129                }
 130            }
 131
 2520132            if (duplicatePriority)
 133            {
 0134                throw TraceUtility.ThrowHelperError(new MultipleFilterMatchesException(SR.FilterMultipleMatches), messag
 135            }
 136
 2520137            return result;
 138        }
 139
 140        public EndpointDispatcher Lookup(Message message, out bool addressMatched)
 141        {
 2518142            EndpointDispatcher data = LookupInCache(message, out addressMatched);
 2518143            if (data == null)
 144            {
 2145                lock (ThisLock)
 146                {
 2147                    data = LookupInCache(message, out addressMatched);
 148
 2149                    if (data == null && _filters != null)
 150                    {
 0151                        _filters.GetMatchingValue(message, out data, out addressMatched);
 152                    }
 2153                }
 154            }
 155
 2518156            return data;
 157        }
 158    }
 159}