Esempio 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));
        }