< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Runtime.SignalGate<T>
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: 9
Coverable lines: 9
Total lines: 122
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
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(...)100%110%
Unlock(...)0%220%

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
 13        public SignalGate()
 14        {
 15        }
 16
 17        internal bool IsLocked
 18        {
 19            get
 20            {
 21                return _state == GateState.Locked;
 22            }
 23        }
 24
 25        internal bool IsSignalled
 26        {
 27            get
 28            {
 29                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        {
 38            int lastState = _state;
 39            if (lastState == GateState.Locked)
 40            {
 41                lastState = Interlocked.CompareExchange(ref _state, GateState.SignalPending, GateState.Locked);
 42            }
 43            if (lastState == GateState.Unlocked)
 44            {
 45                _state = GateState.Signalled;
 46                return true;
 47            }
 48
 49            if (lastState != GateState.Locked)
 50            {
 51                ThrowInvalidSignalGateState();
 52            }
 53            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        {
 62            int lastState = _state;
 63            if (lastState == GateState.Locked)
 64            {
 65                lastState = Interlocked.CompareExchange(ref _state, GateState.Unlocked, GateState.Locked);
 66            }
 67            if (lastState == GateState.SignalPending)
 68            {
 69                _state = GateState.Signalled;
 70                return true;
 71            }
 72
 73            if (lastState != GateState.Locked)
 74            {
 75                ThrowInvalidSignalGateState();
 76            }
 77            return false;
 78        }
 79
 80        // This is factored out to allow Signal and Unlock to be inlined.
 81        private void ThrowInvalidSignalGateState()
 82        {
 83            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()
 0100            : base()
 101        {
 0102        }
 103
 104        public bool Signal(T result)
 105        {
 0106            _result = result;
 0107            return Signal();
 108        }
 109
 110        public bool Unlock(out T result)
 111        {
 0112            if (Unlock())
 113            {
 0114                result = _result;
 0115                return true;
 116            }
 117
 0118            result = default;
 0119            return false;
 120        }
 121    }
 122}

Methods/Properties

.ctor()
Signal(T)
Unlock(T&)