Exemple #1
0
    public static async Task EnumerateAsync <T>(this AsyncEnumerator <T> enumerator, Func <T, Task> asyncAction)
    {
        try
        {
            while (await enumerator.MoveNextAsync())
            {
                await asyncAction(enumerator.Current);
            }
        }
        catch
        {
            try
            {
                while (await enumerator.MoveNextAsync())
                {
                    ;
                }
            }
            catch
            {
                // Ignore
            }

            throw;
        }
    }
        public async void MoveNextAsyncMustReturnTrueWhenEnumeratorMovesNext()
        {
            AsyncEnumerator <int> enumerator = new AsyncEnumerator <int>(new List <int> {
                42
            }.GetEnumerator());

            Assert.True(await enumerator.MoveNextAsync());
            Assert.Equal(42, enumerator.Current);
        }
Exemple #3
0
        public void TestAllYieldTasksCompleteIfProducerFaster()
        {
            List <Task> yieldTasks = new List <Task>();

            AsyncEnumerator <int> enumerator = new AsyncEnumerator <int>();
            var consumer = enumerator.Consumer;
            int cnt      = 0;

            yieldTasks.Add(consumer.YieldAsync(cnt++));
            yieldTasks.Add(consumer.YieldAsync(cnt++));

            // 0
            enumerator.MoveNextAsync().AwaitSynchronously();

            // 1
            enumerator.MoveNextAsync().AwaitSynchronously();

            yieldTasks[0].Wait();
            Assert.IsFalse(yieldTasks[1].Wait(0));
            Assert.IsTrue(yieldTasks[0].Status == TaskStatus.RanToCompletion);
            Assert.IsTrue(yieldTasks[1].Status != TaskStatus.RanToCompletion);

            var moveTo3rdTask = enumerator.MoveNextAsync();

            yieldTasks[1].Wait();
            Assert.IsTrue(yieldTasks[1].Status == TaskStatus.RanToCompletion);

            for (int i = 0; i < 10; i++)
            {
                yieldTasks.Add(consumer.YieldAsync(cnt++));
            }

            consumer.Complete();

            while (enumerator.MoveNextAsync().AwaitSynchronously())
            {
                ;
            }

            Assert.IsTrue(yieldTasks.All(x => yieldTasks[1].Status == TaskStatus.RanToCompletion));
        }
        private async Task <IReadOnlyList <string> > GetMatchingCollections(Database database, string collectionNamePattern)
        {
            var result = new List <string>();

            using (var enumerator = new AsyncEnumerator <DocumentCollection>(
                       client.CreateDocumentCollectionQuery(database.CollectionsLink).AsDocumentQuery()))
            {
                while (await enumerator.MoveNextAsync())
                {
                    if (Regex.IsMatch(enumerator.Current.Id, collectionNamePattern))
                    {
                        result.Add(enumerator.Current.SelfLink);
                    }
                }
            }

            return(result);
        }
Exemple #5
0
        private async Task <IReadOnlyList <string> > GetMatchingCollections(Database database, string collectionNamePattern, CancellationToken cancellation)
        {
            var result = new List <string>();

            using (var enumerator = new AsyncEnumerator <DocumentCollection>(
                       client.CreateDocumentCollectionQuery(database.CollectionsLink).AsDocumentQuery()))
            {
                var collectionNameRegex = new Regex(collectionNamePattern, RegexOptions.Compiled);

                while (await enumerator.MoveNextAsync(cancellation))
                {
                    var match = collectionNameRegex.Match(enumerator.Current.Id);
                    // Make sure regex matches entire collection name and not just substring
                    if (match.Success && String.Equals(match.Value, enumerator.Current.Id, StringComparison.InvariantCulture))
                    {
                        result.Add(enumerator.Current.SelfLink);
                    }
                }
            }

            return(result);
        }
 public Task <bool> MoveNextAsync(CancellationToken cancellationToken = default(CancellationToken)) => _enumerator.MoveNextAsync(cancellationToken);
Exemple #7
0
        private void TestComplex(bool feedAhead, bool fail)
        {
            AsyncEnumerator <int> enumerator = new AsyncEnumerator <int>();
            var consumer = enumerator.Consumer;

            var src = Enumerable.Range(0, 3);

            List <Task> yieldTasks;

            if (feedAhead)
            {
                yieldTasks = consumer.FeedFast(src);

                if (fail)
                {
                    consumer.Fail(new TestUtils.SomeTestException());
                }
                else
                {
                    consumer.Complete();
                }

                VerifyYieldAfterLast(consumer);
                var actual = enumerator.Consume(src.Count());
            }
            else
            {
                yieldTasks = new List <Task>();
                foreach (var item in src)
                {
                    var moveNextTask = enumerator.MoveNextAsync();
                    yieldTasks.Add(consumer.YieldAsync(item));
                    moveNextTask.Wait();
                    Assert.IsTrue(moveNextTask.IsCompleted);
                    Assert.IsTrue(moveNextTask.AwaitSynchronously());
                }

                if (fail)
                {
                    consumer.Fail(new TestUtils.SomeTestException());
                }
                else
                {
                    consumer.Complete();
                }

                VerifyYieldAfterLast(consumer);
            }

            foreach (var yieldTask in yieldTasks.Take(yieldTasks.Count - 1))
            {
                yieldTask.Wait();
                Assert.IsTrue(yieldTask.IsCompleted);
            }
            Assert.IsFalse(yieldTasks.Last().IsCompleted);

            try
            {
                bool res = enumerator.MoveNextAsync().AwaitSynchronously();
                if (fail)
                {
                    Assert.Fail();
                }

                Assert.IsFalse(res);
            }
            catch (TestUtils.SomeTestException)
            {
                if (!fail)
                {
                    Assert.Fail();
                }
            }
        }