< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Runtime.SignalGate
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Runtime/SignalGate.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 122
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
Signal()0%660%
Unlock()0%660%
ThrowInvalidSignalGateState()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Runtime/SignalGate.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;
 6
 7namespace CoreWCF.Runtime
 8{
 9    internal class SignalGate
 10    {
 11        private int _state;
 12
 013        public SignalGate()
 14        {
 015        }
 16
 17        internal bool IsLocked
 18        {
 19            get
 20            {
 021                return _state == GateState.Locked;
 22            }
 23        }
 24
 25        internal bool IsSignalled
 26        {
 27            get
 28            {
 029                return _state == GateState.Signalled;
 30            }
 31        }
 32
 33        // Returns true if this brings the gate to the Signalled state.
 34        // Transitions - Locked -> SignalPending | Completed before it was unlocked
 35        //               Unlocked -> Signaled
 36        public bool Signal()
 37        {
 038            int lastState = _state;
 039            if (lastState == GateState.Locked)
 40            {
 041                lastState = Interlocked.CompareExchange(ref _state, GateState.SignalPending, GateState.Locked);
 42            }
 043            if (lastState == GateState.Unlocked)
 44            {
 045                _state = GateState.Signalled;
 046                return true;
 47            }
 48
 049            if (lastState != GateState.Locked)
 50            {
 051                ThrowInvalidSignalGateState();
 52            }
 053            return false;
 54        }
 55
 56        // Returns true if this brings the gate to the Signalled state.
 57        // Transitions - SignalPending -> Signaled | return the AsyncResult since the callback already
 58        //                                         | completed and provided the result on its thread
 59        //               Locked -> Unlocked
 60        public bool Unlock()
 61        {
 062            int lastState = _state;
 063            if (lastState == GateState.Locked)
 64            {
 065                lastState = Interlocked.CompareExchange(ref _state, GateState.Unlocked, GateState.Locked);
 66            }
 067            if (lastState == GateState.SignalPending)
 68            {
 069                _state = GateState.Signalled;
 070                return true;
 71            }
 72
 073            if (lastState != GateState.Locked)
 74            {
 075                ThrowInvalidSignalGateState();
 76            }
 077            return false;
 78        }
 79
 80        // This is factored out to allow Signal and Unlock to be inlined.
 81        private void ThrowInvalidSignalGateState()
 82        {
 083            throw Fx.Exception.AsError(new InvalidOperationException(SR.InvalidSemaphoreExit));
 84        }
 85
 86        private static class GateState
 87        {
 88            public const int Locked = 0;
 89            public const int SignalPending = 1;
 90            public const int Unlocked = 2;
 91            public const int Signalled = 3;
 92        }
 93    }
 94
 95    internal class SignalGate<T> : SignalGate
 96    {
 97        private T _result;
 98
 99        public SignalGate()
 100            : base()
 101        {
 102        }
 103
 104        public bool Signal(T result)
 105        {
 106            _result = result;
 107            return Signal();
 108        }
 109
 110        public bool Unlock(out T result)
 111        {
 112            if (Unlock())
 113            {
 114                result = _result;
 115                return true;
 116            }
 117
 118            result = default;
 119            return false;
 120        }
 121    }
 122}