< Summary

Class:Azure.Storage.StorageCollectionEnumerator`1
Assembly:Azure.Storage.Blobs
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        {
 31421            return new StoragePageable(this, cancellationToken);
 22        }
 23
 24        public AsyncPageable<T> ToAsyncCollection(CancellationToken cancellationToken)
 25        {
 34226            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)
 31443                : base(cancellationToken)
 44            {
 31445                _enumerator = enumerator;
 31446            }
 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) =>
 29659                !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                {
 31482                    Page<T> page = _enumerator.GetNextPageAsync(
 31483                        continuationToken,
 31484                        pageHintSize,
 31485                        async: false,
 31486                        cancellationToken: CancellationToken)
 31487                        .EnsureCompleted();
 30888                    continuationToken = page.ContinuationToken;
 30889                    yield return page;
 29690                } while (CanContinue(continuationToken));
 29691            }
 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)
 342132                : base(cancellationToken)
 133            {
 342134                _enumerator = enumerator;
 342135            }
 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) =>
 312148                !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                {
 48171                    Page<T> page = await _enumerator.GetNextPageAsync(
 48172                        continuationToken,
 48173                        pageHintSize,
 48174                        async: true,
 48175                        cancellationToken: CancellationToken)
 48176                        .ConfigureAwait(false);
 42177                    continuationToken = page.ContinuationToken;
 42178                    yield return page;
 34179                } while (CanContinue(continuationToken));
 42180            }
 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.
 294195                if (cancellationToken == default)
 196                {
 294197                    cancellationToken = CancellationToken;
 198                }
 199
 294200                string continuationToken = null;
 201                do
 202                {
 294203                    Page<T> page = await _enumerator.GetNextPageAsync(
 294204                        continuationToken,
 294205                        null,
 294206                        async: true,
 294207                        cancellationToken: cancellationToken)
 294208                        .ConfigureAwait(false);
 290209                    continuationToken = page.ContinuationToken;
 11104210                    foreach (T item in page.Values)
 211                    {
 5268212                        yield return item;
 213                    }
 278214                } while (CanContinue(continuationToken));
 290215            }
 216        }
 217    }
 218}