< Summary

Class:Azure.Storage.StreamPartition
Assembly:Azure.Storage.Files.Shares
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\StreamPartition.cs
Covered lines:20
Uncovered lines:25
Coverable lines:45
Total lines:140
Line coverage:44.4% (20 of 45)
Covered branches:2
Total branches:8
Branch coverage:25% (2 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_CanRead()-100%100%
get_CanSeek()-100%100%
get_CanWrite()-0%100%
get_Length()-100%100%
get_Position()-0%100%
get_ParentPosition()-100%100%
get_DisposalTask()-0%100%
.ctor(...)-100%100%
DisposalTaskCore()-25%100%
Dispose(...)-0%0%
Flush()-0%100%
Read(...)-0%100%
Read(...)-100%100%
Seek(...)-50%50%
SetLength(...)-0%100%
Write(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\StreamPartition.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Buffers;
 6using System.Collections.Generic;
 7using System.Diagnostics;
 8using System.IO;
 9using System.Linq;
 10using System.Runtime.CompilerServices;
 11using System.Runtime.InteropServices;
 12using System.Threading;
 13using System.Threading.Tasks;
 14
 15namespace Azure.Storage
 16{
 17    internal sealed class StreamPartition : Stream
 18    {
 19        private Action _disposeAction;
 20        private ReadOnlyMemory<byte> _memory;
 21
 370222        public override bool CanRead => true;
 23
 370224        public override bool CanSeek => true;
 25
 026        public override bool CanWrite => false;
 27
 987228        public override long Length { get; }
 29
 30        private long _position;
 31
 032        public override long Position { get => _position; set => _position = value; }
 33
 123434        public long ParentPosition { get; }
 35
 036        public Task DisposalTask { get; }
 37
 38#pragma warning disable IDE0069 // Disposable fields should be disposed // disposed in DisposalTask
 39        //ManualResetEventSlim disposalTaskCompletionSource;
 40        private SemaphoreSlim _disposalTaskCompletionSource;
 41#pragma warning restore IDE0069 // Disposable fields should be disposed
 42
 123443        public StreamPartition(ReadOnlyMemory<byte> buffer, long parentPosition, int count, Action disposeAction, Cancel
 44        {
 123445            _memory = buffer;
 123446            ParentPosition = parentPosition;
 123447            Length = count;
 123448            _disposeAction = disposeAction;
 49            //this.disposalTaskCompletionSource = new ManualResetEventSlim(false);
 123450            _disposalTaskCompletionSource = new SemaphoreSlim(0);
 123451            DisposalTask = DisposalTaskCore(ct);
 123452        }
 53
 54#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
 55        private async Task DisposalTaskCore(CancellationToken ct)
 56        {
 57            //Console.WriteLine($"Waiting for partition {this.ParentPosition}");
 58
 59            //this.disposalTaskCompletionSource.Wait(ct);
 123460            await _disposalTaskCompletionSource.WaitAsync(ct).ConfigureAwait(false);
 61
 62            //Console.WriteLine($"Completed partition {this.ParentPosition}");
 63
 064            _disposalTaskCompletionSource.Dispose();
 065            _disposalTaskCompletionSource = default;
 066        }
 67#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
 68
 69        protected override void Dispose(bool disposing)
 70        {
 071            if (!_disposedValue)
 72            {
 073                if (disposing)
 74                {
 075                    base.Dispose(disposing);
 076                    _disposeAction();
 77                }
 78
 079                _memory = default;
 080                _disposeAction = default;
 81
 082                _disposedValue = true;
 83
 84                //this.disposalTaskCompletionSource.Set();
 085                _disposalTaskCompletionSource.Release();
 86            }
 087        }
 88
 89        private bool _disposedValue = false; // To detect redundant calls
 90
 91        public override void Flush()
 92        {
 93            // Flush is allowed on read-only stream
 094        }
 95
 96        public int Read(out ReadOnlyMemory<byte> buffer, int count)
 97        {
 098            var n = Math.Min(count, (int)(Length - _position));
 99
 0100            buffer = _memory.Slice((int)_position, n);
 101
 0102            Interlocked.Add(ref _position, n);
 103
 0104            return n;
 105        }
 106
 107        public override int Read(byte[] buffer, int offset, int count)
 108        {
 4936109            var n = Math.Min(count, (int)(Length - _position));
 110
 4936111            _memory.Slice((int)_position, n).CopyTo(new Memory<byte>(buffer, offset, count));
 112
 4936113            Interlocked.Add(ref _position, n);
 114
 4936115            return n;
 116        }
 117
 118        public override long Seek(long offset, SeekOrigin origin)
 119        {
 120            switch (origin)
 121            {
 122                case SeekOrigin.Begin:
 2468123                    Interlocked.Exchange(ref _position, offset);
 2468124                    break;
 125                case SeekOrigin.Current:
 0126                    Interlocked.Add(ref _position, offset);
 0127                    break;
 128                case SeekOrigin.End:
 0129                    Interlocked.Exchange(ref _position, Length - offset);
 130                    break;
 131            }
 132
 2468133            return Position;
 134        }
 135
 0136        public override void SetLength(long value) => throw Errors.NotImplemented();
 137
 0138        public override void Write(byte[] buffer, int offset, int count) => throw Errors.NotImplemented();
 139    }
 140}