< Summary

Information
Class: System.Net.WebSockets.WebSocketStateHelper
Assembly: System.Net.WebSockets
File(s): D:\runner\runtime\src\libraries\System.Net.WebSockets\src\System\Net\WebSockets\WebSocketStateHelper.cs
Line coverage
78%
Covered lines: 15
Uncovered lines: 4
Coverable lines: 19
Total lines: 50
Line coverage: 78.9%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
IsValidSendState(...)100%110%
ThrowIfInvalidState(...)75%4476.92%
ToFlag(...)100%11100%

File(s)

D:\runner\runtime\src\libraries\System.Net.WebSockets\src\System\Net\WebSockets\WebSocketStateHelper.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.Diagnostics;
 5
 6namespace System.Net.WebSockets
 7{
 8    internal static class WebSocketStateHelper
 9    {
 10        /// <summary>Valid states to be in when calling SendAsync.</summary>
 11        internal const ManagedWebSocketStates ValidSendStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.Clo
 12        /// <summary>Valid states to be in when calling ReceiveAsync.</summary>
 13        internal const ManagedWebSocketStates ValidReceiveStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.
 14        /// <summary>Valid states to be in when calling CloseOutputAsync.</summary>
 15        internal const ManagedWebSocketStates ValidCloseOutputStates = ManagedWebSocketStates.Open | ManagedWebSocketSta
 16        /// <summary>Valid states to be in when calling CloseAsync.</summary>
 17        internal const ManagedWebSocketStates ValidCloseStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.Cl
 18
 019        internal static bool IsValidSendState(WebSocketState state) => (ValidSendStates & ToFlag(state)) != 0;
 20
 21        internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, Exception? innerException
 27095322        {
 27095323            ManagedWebSocketStates state = ToFlag(currentState);
 24
 27095325            if ((state & validStates) == 0)
 12326            {
 12327                string invalidStateMessage = SR.Format(
 12328                    SR.net_WebSockets_InvalidState, currentState, validStates);
 29
 12330                throw new WebSocketException(WebSocketError.InvalidState, invalidStateMessage, innerException);
 31            }
 32
 27083033            if (innerException is not null)
 034            {
 035                Debug.Assert(state == ManagedWebSocketStates.Aborted);
 036                throw new OperationCanceledException(nameof(WebSocketState.Aborted), innerException);
 37            }
 38
 39            // Ordering is important to maintain .NET 4.5 WebSocket implementation exception behavior.
 27083040            ObjectDisposedException.ThrowIf(isDisposed, typeof(WebSocket));
 27083041        }
 42
 43        private static ManagedWebSocketStates ToFlag(WebSocketState value)
 27095344        {
 27095345            ManagedWebSocketStates flag = (ManagedWebSocketStates)(1 << (int)value);
 27095346            Debug.Assert(Enum.IsDefined(flag));
 27095347            return flag;
 27095348        }
 49    }
 50}