< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.TimeoutStream
Assembly: CoreWCF.Http
File(s): /home/runner/work/CoreWCF/CoreWCF/src/Common/src/DuplexChannels/CoreWCF/Channels/TimeoutStream.cs
Line coverage
85%
Covered lines: 24
Uncovered lines: 4
Coverable lines: 28
Total lines: 87
Line coverage: 85.7%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%2287.5%
Read(...)100%11100%
ReadByte()100%22100%
ReadAsync()100%11100%
ReadAsyncInternal()50%22100%
Write(...)100%11100%
WriteByte(...)100%110%
WriteAsync()100%11100%
WriteAsyncInternal()100%22100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/Common/src/DuplexChannels/CoreWCF/Channels/TimeoutStream.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.IO;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using CoreWCF.Runtime;
 8
 9namespace CoreWCF.Channels
 10{
 11    // Enforces an overall timeout based on the TimeoutHelper passed in
 12    internal class TimeoutStream : DelegatingStream
 13    {
 1214        private readonly byte[] _oneByteArray = new byte[1];
 15        private CancellationToken _cancellationToken;
 16
 17        public TimeoutStream(Stream stream, CancellationToken token)
 1218            : base(stream)
 19        {
 1220            if (!stream.CanTimeout)
 21            {
 022                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(stream), SR.StreamDoesNotSupportTime
 23            }
 24
 1225            _cancellationToken = token;
 1226            ReadTimeout = (int)TimeoutHelper.GetOriginalTimeout(token).TotalMilliseconds;
 1227            WriteTimeout = ReadTimeout;
 1228        }
 29
 30        public override int Read(byte[] buffer, int offset, int count)
 31        {
 24532            return ReadAsyncInternal(buffer, offset, count, CancellationToken.None).WaitForCompletion();
 33        }
 34
 35        public override int ReadByte()
 36        {
 16337            int r = Read(_oneByteArray, 0, 1);
 16338            if (r == 0)
 39            {
 1040                return -1;
 41            }
 42
 15343            return _oneByteArray[0];
 44        }
 45
 46        public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo
 47        {
 48            // Supporting a passed in cancellationToken as well as honoring the timeout token in this class would requir
 49            // creating a linked token source on every call which is extra allocation and needs disposal. As this is an
 50            // internal class, it's okay to add this extra constraint to usage of this method.
 51            Fx.Assert(!cancellationToken.CanBeCanceled, "cancellationToken shouldn't be cancellable");
 24552            return await base.ReadAsync(buffer, offset, count, _cancellationToken);
 24553        }
 54
 55        private async Task<int> ReadAsyncInternal(byte[] buffer, int offset, int count, CancellationToken cancellationTo
 56        {
 24557            await TaskHelpers.EnsureDefaultTaskScheduler();
 24558            return await ReadAsync(buffer, offset, count, cancellationToken);
 24559        }
 60
 61        public override void Write(byte[] buffer, int offset, int count)
 62        {
 1263            WriteAsyncInternal(buffer, offset, count, CancellationToken.None).WaitForCompletion();
 1264        }
 65
 66        public override void WriteByte(byte value)
 67        {
 068            _oneByteArray[0] = value;
 069            Write(_oneByteArray, 0, 1);
 070        }
 71
 72        public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 73        {
 74            // Supporting a passed in cancellationToken as well as honoring the timeout token in this class would requir
 75            // creating a linked token source on every call which is extra allocation and needs disposal. As this is an
 76            // internal class, it's okay to add this extra constraint to usage of this method.
 77            Fx.Assert(!cancellationToken.CanBeCanceled, "cancellationToken shouldn't be cancellable");
 1278            await base.WriteAsync(buffer, offset, count, _cancellationToken);
 1279        }
 80
 81        private async Task WriteAsyncInternal(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 82        {
 1283            await TaskHelpers.EnsureDefaultTaskScheduler();
 1284            await WriteAsync(buffer, offset, count, cancellationToken);
 1285        }
 86    }
 87}