< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.InterruptibleWaitObject
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/ReliableMessagingHelpers.cs
Line coverage
37%
Covered lines: 27
Uncovered lines: 45
Coverable lines: 72
Total lines: 180
Line coverage: 37.5%
Branch coverage
15%
Covered branches: 5
Total branches: 32
Branch coverage: 15.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
Abort(...)0%440%
Fault(...)0%440%
GetException()0%220%
InternalSet()50%2283.33%
Reset()50%4487.5%
Set()100%11100%
WaitAsync(...)100%110%
WaitAsync()12.5%161621.73%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/ReliableMessagingHelpers.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.Threading.Tasks;
 6using CoreWCF.Runtime;
 7
 8namespace CoreWCF.Channels
 9{
 10    internal class InterruptibleWaitObject
 11    {
 12        private bool _aborted = false;
 13        private CommunicationObject _communicationObject;
 14        private bool _set;
 15        private int _syncWaiters;
 416        private object _thisLock = new object();
 417        private bool _throwTimeoutByDefault = true;
 18        private TaskCompletionSource<object> _tcs;
 19
 20        public InterruptibleWaitObject(bool signaled)
 421            : this(signaled, true)
 22        {
 423        }
 24
 425        public InterruptibleWaitObject(bool signaled, bool throwTimeoutByDefault)
 26        {
 427            _set = signaled;
 428            _throwTimeoutByDefault = throwTimeoutByDefault;
 429        }
 30
 31        public void Abort(CommunicationObject communicationObject)
 32        {
 033            if (communicationObject == null)
 34            {
 035                throw Fx.AssertAndThrow("Argument communicationObject cannot be null.");
 36            }
 37
 038            lock (_thisLock)
 39            {
 040                if (_aborted)
 41                {
 042                    return;
 43                }
 44
 045                _communicationObject = communicationObject;
 46
 047                _aborted = true;
 048                InternalSet();
 049            }
 050        }
 51
 52        public void Fault(CommunicationObject communicationObject)
 53        {
 054            if (communicationObject == null)
 55            {
 056                throw Fx.AssertAndThrow("Argument communicationObject cannot be null.");
 57            }
 58
 059            lock (_thisLock)
 60            {
 061                if (_aborted)
 62                {
 063                    return;
 64                }
 65
 066                _communicationObject = communicationObject;
 67
 068                _aborted = false;
 069                InternalSet();
 070            }
 071        }
 72
 73        private Exception GetException()
 74        {
 075            if (_communicationObject == null)
 76            {
 77                Fx.Assert("Caller is attempting to retrieve an exception from a null communicationObject.");
 78            }
 79
 080            return _aborted
 081                ? _communicationObject.CreateAbortedException()
 082                : _communicationObject.GetPendingException();
 83        }
 84
 85        private void InternalSet()
 86        {
 487            lock (_thisLock)
 88            {
 489                _set = true;
 90
 491                if (_tcs != null)
 92                {
 093                    _tcs.TrySetResult(null);
 94                }
 495            }
 496        }
 97
 98        public void Reset()
 99        {
 2100            lock (_thisLock)
 101            {
 2102                _communicationObject = null;
 2103                _aborted = false;
 2104                _set = false;
 105
 2106                if (_tcs != null && _tcs.Task.IsCompleted)
 107                {
 0108                    _tcs = new TaskCompletionSource<object>();
 109                }
 2110            }
 2111        }
 112
 113        public void Set()
 114        {
 4115            InternalSet();
 4116        }
 117
 118        public Task<bool> WaitAsync(TimeSpan timeout)
 119        {
 0120            return WaitAsync(timeout, _throwTimeoutByDefault);
 121        }
 122
 123        public async Task<bool> WaitAsync(TimeSpan timeout, bool throwTimeoutException)
 124        {
 4125            lock (_thisLock)
 126            {
 4127                if (_set)
 128                {
 4129                    if (_communicationObject != null)
 130                    {
 0131                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(GetException());
 132                    }
 133
 4134                    return true;
 135                }
 136
 0137                if (_tcs == null)
 138                {
 0139                    _tcs = new TaskCompletionSource<object>();
 140                }
 141
 0142                _syncWaiters++;
 0143            }
 144
 145            try
 146            {
 0147                if (!await _tcs.Task.AwaitWithTimeout(timeout))
 148                {
 0149                    if (throwTimeoutException)
 150                    {
 0151                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.Time
 152                    }
 153                    else
 154                    {
 0155                        return false;
 156                    }
 157                }
 0158            }
 159            finally
 160            {
 0161                lock (_thisLock)
 162                {
 163                    // Last one out turns off the light.
 0164                    _syncWaiters--;
 0165                    if (_syncWaiters == 0 && _tcs.Task.IsCompleted)
 166                    {
 0167                        _tcs = null;
 168                    }
 0169                }
 170            }
 171
 0172            if (_communicationObject != null)
 173            {
 0174                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(GetException());
 175            }
 176
 0177            return true;
 4178        }
 179    }
 180}