< Summary

Class:Azure.Storage.StorageCollectionEnumerator`1
Assembly:Azure.Storage.Queues
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\StorageCollectionEnumerator.cs
Covered lines:44
Uncovered lines:16
Coverable lines:60
Total lines:218
Line coverage:73.3% (44 of 60)
Covered branches:29
Total branches:36
Branch coverage:80.5% (29 of 36)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ToSyncCollection(...)-100%100%
ToAsyncCollection(...)-100%100%
.ctor()-0%100%
.ctor(...)-100%100%
CanContinue(...)-100%100%
AsPages()-100%100%
GetEnumerator()-0%0%
.ctor()-0%100%
.ctor(...)-100%100%
CanContinue(...)-100%100%
AsPages()-100%83.33%
GetAsyncEnumerator()-100%94.44%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Generic;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Azure.Core.Pipeline;
 8
 9namespace Azure.Storage
 10{
 11    internal abstract class StorageCollectionEnumerator<T>
 12    {
 13        public abstract ValueTask<Page<T>> GetNextPageAsync(
 14            string continuationToken,
 15            int? pageSizeHint,
 16            bool async,
 17            CancellationToken cancellationToken);
 18
 19        public Pageable<T> ToSyncCollection(CancellationToken cancellationToken)
 20        {
 1821            return new StoragePageable(this, cancellationToken);
 22        }
 23
 24        public AsyncPageable<T> ToAsyncCollection(CancellationToken cancellationToken)
 25        {
 2026            return new StorageAsyncPageable(this, cancellationToken);
 27        }
 28
 29        /// <summary>
 30        /// Abstract the Storage pattern for async iteration
 31        /// </summary>
 32        private class StoragePageable : Pageable<T>
 33        {
 34            private StorageCollectionEnumerator<T> _enumerator;
 35
 36            // for mocking
 37            protected StoragePageable()
 038                : base()
 39            {
 040            }
 41
 42            public StoragePageable(StorageCollectionEnumerator<T> enumerator, CancellationToken cancellationToken)
 1843                : base(cancellationToken)
 44            {
 1845                _enumerator = enumerator;
 1846            }
 47
 48            /// <summary>
 49            /// Determine if the iteration can continue.
 50            /// </summary>
 51            /// <param name="continuationToken">
 52            /// The next continuation token provided with the last
 53            /// <see cref="Page{T}"/>.
 54            /// </param>
 55            /// <returns>
 56            /// True if the iteration can continue, false otherwise.
 57            /// </returns>
 58            protected virtual bool CanContinue(string continuationToken) =>
 1659                !string.IsNullOrEmpty(continuationToken);
 60
 61            /// <summary>
 62            /// Enumerate the values a <see cref="Page{T}"/> at a time.  This may
 63            /// make mutliple service requests.
 64            /// </summary>
 65            /// <param name="continuationToken">
 66            /// A continuation token indicating where to resume paging or null to
 67            /// begin paging from the beginning.
 68            /// </param>
 69            /// <param name="pageHintSize">
 70            /// The size of <see cref="Page{T}"/>s that should be requested (from
 71            /// service operations that support it).
 72            /// </param>
 73            /// <returns>
 74            /// An async sequence of <see cref="Page{T}"/>s.
 75            /// </returns>
 76            public override IEnumerable<Page<T>> AsPages(
 77                string continuationToken = default,
 78                int? pageHintSize = default)
 79            {
 80                do
 81                {
 1882                    Page<T> page = _enumerator.GetNextPageAsync(
 1883                        continuationToken,
 1884                        pageHintSize,
 1885                        async: false,
 1886                        cancellationToken: CancellationToken)
 1887                        .EnsureCompleted();
 1888                    continuationToken = page.ContinuationToken;
 1889                    yield return page;
 1690                } while (CanContinue(continuationToken));
 1691            }
 92
 93            /// <summary>
 94            /// Enumerate the values in the collection synchronously.  This may
 95            /// make mutliple service requests.
 96            /// </summary>
 97            /// <returns>A sequence of values.</returns>
 98            public override IEnumerator<T> GetEnumerator()
 99            {
 0100                string continuationToken = null;
 101                do
 102                {
 0103                    Page<T> page = _enumerator.GetNextPageAsync(
 0104                        continuationToken,
 0105                        null,
 0106                        async: false,
 0107                        cancellationToken: CancellationToken)
 0108                        .EnsureCompleted();
 0109                    continuationToken = page.ContinuationToken;
 0110                    foreach (T item in page.Values)
 111                    {
 0112                        yield return item;
 113                    }
 0114                } while (CanContinue(continuationToken));
 0115            }
 116        }
 117
 118        /// <summary>
 119        /// Abstract the Storage pattern for async iteration
 120        /// </summary>
 121        private class StorageAsyncPageable : AsyncPageable<T>
 122        {
 123            private StorageCollectionEnumerator<T> _enumerator;
 124
 125            // for mocking
 126            protected StorageAsyncPageable()
 0127                : base()
 128            {
 0129            }
 130
 131            public StorageAsyncPageable(StorageCollectionEnumerator<T> enumerator, CancellationToken cancellationToken)
 20132                : base(cancellationToken)
 133            {
 20134                _enumerator = enumerator;
 20135            }
 136
 137            /// <summary>
 138            /// Determine if the iteration can continue.
 139            /// </summary>
 140            /// <param name="continuationToken">
 141            /// The next continuation token provided with the last
 142            /// <see cref="Page{T}"/>.
 143            /// </param>
 144            /// <returns>
 145            /// True if the iteration can continue, false otherwise.
 146            /// </returns>
 147            protected virtual bool CanContinue(string continuationToken) =>
 14148                !string.IsNullOrEmpty(continuationToken);
 149
 150            /// <summary>
 151            /// Enumerate the values a <see cref="Page{T}"/> at a time.  This may
 152            /// make mutliple service requests.
 153            /// </summary>
 154            /// <param name="continuationToken">
 155            /// A continuation token indicating where to resume paging or null to
 156            /// begin paging from the beginning.
 157            /// </param>
 158            /// <param name="pageHintSize">
 159            /// The size of <see cref="Page{T}"/>s that should be requested (from
 160            /// service operations that support it).
 161            /// </param>
 162            /// <returns>
 163            /// An async sequence of <see cref="Page{T}"/>s.
 164            /// </returns>
 165            public override async IAsyncEnumerable<Page<T>> AsPages(
 166                string continuationToken = default,
 167                int? pageHintSize = default)
 168            {
 169                do
 170                {
 6171                    Page<T> page = await _enumerator.GetNextPageAsync(
 6172                        continuationToken,
 6173                        pageHintSize,
 6174                        async: true,
 6175                        cancellationToken: CancellationToken)
 6176                        .ConfigureAwait(false);
 4177                    continuationToken = page.ContinuationToken;
 4178                    yield return page;
 2179                } while (CanContinue(continuationToken));
 4180            }
 181
 182            /// <summary>
 183            /// Enumerate the values in the collection asynchronously.  This may
 184            /// make mutliple service requests.
 185            /// </summary>
 186            /// <param name="cancellationToken">
 187            /// The <see cref="CancellationToken"/> used for requests made while
 188            /// enumerating asynchronously.
 189            /// </param>
 190            /// <returns>An async sequence of values.</returns>
 191            public override async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
 192            {
 193                // This is the only method that takes its own CancellationToken, but
 194                // we'll still use the original CancellationToken if one wasn't passed.
 14195                if (cancellationToken == default)
 196                {
 14197                    cancellationToken = CancellationToken;
 198                }
 199
 14200                string continuationToken = null;
 201                do
 202                {
 14203                    Page<T> page = await _enumerator.GetNextPageAsync(
 14204                        continuationToken,
 14205                        null,
 14206                        async: true,
 14207                        cancellationToken: cancellationToken)
 14208                        .ConfigureAwait(false);
 14209                    continuationToken = page.ContinuationToken;
 62210                    foreach (T item in page.Values)
 211                    {
 18212                        yield return item;
 213                    }
 12214                } while (CanContinue(continuationToken));
 14215            }
 216        }
 217    }
 218}