Exemplo n.º 1
0
        public async Task TestSequentialEnumeratorAsync()
        {
            var start           = MAX_ITEMS;
            var completionState = new Int32[start];
            var r = new Random();
            MoveNextAsyncDelegate <Int32> moveNext = async() =>
            {
                var decremented = Interlocked.Decrement(ref start);
                await Task.Delay(r.Next(100, 500));

                return(decremented >= 0, MAX_ITEMS - decremented - 1);
            };

            var enumerable = AsyncEnumerationFactory.CreateSequentialEnumerable(() => AsyncEnumerationFactory.CreateSequentialStartInfo(
                                                                                    moveNext,
                                                                                    null
                                                                                    ),
                                                                                DefaultAsyncProvider.Instance);
            Func <Int32, Task> callback = async idx =>
            {
                await Task.Delay(r.Next(100, 900));

                Assert.IsTrue(completionState.Take(idx).All(s => s == 1));
                Interlocked.Increment(ref completionState[idx]);
            };
            var itemsEncountered = await enumerable.EnumerateAsync(callback);

            Assert.AreEqual(itemsEncountered, completionState.Length);
            Assert.IsTrue(completionState.All(s => s == 1));
        }
Exemplo n.º 2
0
 /// <summary>
 /// This extension method will wrap this <see cref="IEnumerable{T}"/> into <see cref="IAsyncEnumerable{T}"/>.
 /// </summary>
 /// <typeparam name="T">The type of <see cref="IEnumerable{T}"/> elements.</typeparam>
 /// <param name="enumerable">This <see cref="IEnumerable{T}"/>.</param>
 /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/>.</param>
 /// <returns><see cref="IAsyncEnumerable{T}"/> which will enumerate over this <see cref="IEnumerable{T}"/>.</returns>
 /// <exception cref="NullReferenceException">If this <see cref="IEnumerable{T}"/> is <c>null</c>.</exception>
 public static IAsyncEnumerable <T> AsAsyncEnumerable <T>(
     this IEnumerable <T> enumerable,
     IAsyncProvider asyncProvider
     ) => AsyncEnumerationFactory.FromGeneratorCallback(ArgumentValidator.ValidateNotNullReference(enumerable), e => new SynchronousEnumerableEnumerator <T>(e.GetEnumerator()), asyncProvider);
Exemplo n.º 3
0
 /// <summary>
 /// This extension method will wrap this array into <see cref="IAsyncEnumerable{T}"/>.
 /// </summary>
 /// <typeparam name="T">The type of array elements.</typeparam>
 /// <param name="array">This array.</param>
 /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/>.</param>
 /// <returns><see cref="IAsyncEnumerable{T}"/> which will enumerate over the contents of the array.</returns>
 /// <exception cref="NullReferenceException">If this array is <c>null</c>.</exception>
 public static IAsyncEnumerable <T> AsAsyncEnumerable <T>(
     this T[] array,
     IAsyncProvider asyncProvider
     ) => AsyncEnumerationFactory.FromGeneratorCallback(ArgumentValidator.ValidateNotNullReference(array), a => new ArrayEnumerator <T>(a), asyncProvider);