예제 #1
0
        public async Task Predicate_Enumerable_PredicateError()
        {
            var list = new List <int> {
                1, 2, 3, 4, 5
            };
            var enumerable     = new TestEnumerable <int>(list);
            var predicate      = new TestPredicate <int>(x => x < 10);
            var predicateError = new TestPredicate <int>(x => x < 10, x => x > 2);

            try
            {
                await enumerable.WhereAsync(x => predicate.Predicate(x)).Where(x => predicateError.Predicate(x)).ToList();

                throw new Exception("Oops!");
            }
            catch (Exception ex)
            {
                // MUST throw an error
                Assert.AreEqual("TestPredicate;ErrorPredicate;Value=3", ex.Message);

                // MUST have 2 iterations for enumerable
                Assert.AreEqual(2, enumerable.CurrentIndex);

                // MUST have 3 iterations for predicate
                Assert.AreEqual(3, predicate.Count);

                // MUST have 3 iterations for predicateError
                Assert.AreEqual(3, predicateError.Count);
            }
        }
        public async Task PredicateAsync_ToList_PredicateAsyncError()
        {
            var list = new List <int> {
                1, 2, 3, 4, 5
            };
            var enumerable     = new TestEnumerable <int>(list);
            var predicate      = new TestPredicateAsync <int>(x => Task.FromResult(true));
            var predicateError = new TestPredicateAsync <int>(x => Task.FromResult(x < 10), x => Task.FromResult(x > 2));

            try
            {
                await enumerable.WhereAsync(x => predicate.Predicate(x)).ToList().Where(x => predicateError.Predicate(x)).ToList();

                throw new Exception("Oops!");
            }
            catch (AggregateException ex)
            {
                // MUST throw an error
                Assert.AreEqual("TestPredicateAsync;ErrorPredicateAsync;Value=3", ex.Flatten().InnerExceptions[0].Message);

                // MUST have 1 exception
                Assert.AreEqual(1, ex.Flatten().InnerExceptions.Count);

                // MUST have 5 iterations for enumerable
                Assert.AreEqual(4, enumerable.CurrentIndex);

                // MUST have 5 iterations for predicate
                Assert.AreEqual(5, predicate.Count);

                // MUST have 3 iterations for predicateError
                Assert.AreEqual(3, predicateError.Count);
            }
        }
        public async Task Manager_IsTrue_Order_IsFalse()
        {
            var list = new List <int> {
                1, 2, 3, 4, 5
            };
            var enumerable     = new TestEnumerable <int>(list);
            var predicateAsync = new TestPredicateAsync <int>(i =>
            {
                switch (i)
                {
                case 1:
                    return(Task.Delay(600).ContinueWith(task => true));

                case 2:
                    return(Task.Delay(200).ContinueWith(task => true));

                case 3:
                    return(Task.Delay(800).ContinueWith(task => true));

                case 4:
                    return(Task.Delay(500).ContinueWith(task => true));

                case 5:
                    return(Task.Delay(300).ContinueWith(task => true));

                default:
                    throw new Exception("Oops!");
                }
            });

            var defaultValue = LinqAsyncManager.DefaultValue.OrderByPredicateCompletion;

            try
            {
                LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = true;

                var result = await enumerable.WhereAsync(x => predicateAsync.Predicate(x)).OrderByPredicateCompletion(false).ToList();

                // MUST have 5 iterations for enumerable
                Assert.AreEqual(4, enumerable.CurrentIndex);

                // MUST be ordered with original order
                Assert.AreEqual(1, result[0]);
                Assert.AreEqual(2, result[1]);
                Assert.AreEqual(3, result[2]);
                Assert.AreEqual(4, result[3]);
                Assert.AreEqual(5, result[4]);
            }
            finally
            {
                LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = defaultValue;
            }
        }
예제 #4
0
        public async Task IsFalse_Default()
        {
            var resultPredicate = new List <int>();

            var predicateAsync = new TestPredicateAsync <int>(i =>
            {
                switch (i)
                {
                case 1:
                    return(Task.Delay(600).ContinueWith(task =>
                    {
                        resultPredicate.Add(i);
                        return true;
                    }));

                case 2:
                    return(Task.Delay(200).ContinueWith(task =>
                    {
                        resultPredicate.Add(i);
                        return true;
                    }));

                case 3:
                    return(Task.Delay(800).ContinueWith(task =>
                    {
                        resultPredicate.Add(i);
                        return true;
                    }));

                case 4:
                    return(Task.Delay(500).ContinueWith(task =>
                    {
                        resultPredicate.Add(i);
                        return true;
                    }));

                case 5:
                    return(Task.Delay(300).ContinueWith(task =>
                    {
                        resultPredicate.Add(i);
                        return true;
                    }));

                default:
                    throw new Exception("Oops!");
                }
            });

            // Default
            {
                var list = new List <int> {
                    1, 2, 3, 4, 5
                };
                var enumerable = new TestEnumerable <int>(list);

                var result = await enumerable.WhereAsync(x => predicateAsync.Predicate(x)).ToList();

                // MUST have 5 iterations for enumerable
                Assert.AreEqual(4, enumerable.CurrentIndex);

                // MUST Keep the original order
                Assert.AreEqual(1, result[0]);
                Assert.AreEqual(2, result[1]);
                Assert.AreEqual(3, result[2]);
                Assert.AreEqual(4, result[3]);
                Assert.AreEqual(5, result[4]);

                // MUST Keep the original order for predicate
                Assert.AreEqual(1, resultPredicate[0]);
                Assert.AreEqual(2, resultPredicate[1]);
                Assert.AreEqual(3, resultPredicate[2]);
                Assert.AreEqual(4, resultPredicate[3]);
                Assert.AreEqual(5, resultPredicate[4]);
            }

            resultPredicate.Clear();

            // IsFalse
            {
                var list = new List <int> {
                    1, 2, 3, 4, 5
                };
                var enumerable = new TestEnumerable <int>(list);

                var result = await enumerable.WhereAsync(x => predicateAsync.Predicate(x)).StartPredicateConcurrently(false).ToList();

                // MUST have 5 iterations for enumerable
                Assert.AreEqual(4, enumerable.CurrentIndex);

                // MUST be ordered with original order
                Assert.AreEqual(1, result[0]);
                Assert.AreEqual(2, result[1]);
                Assert.AreEqual(3, result[2]);
                Assert.AreEqual(4, result[3]);
                Assert.AreEqual(5, result[4]);

                // MUST Keep the original order for predicate
                Assert.AreEqual(1, resultPredicate[0]);
                Assert.AreEqual(2, resultPredicate[1]);
                Assert.AreEqual(3, resultPredicate[2]);
                Assert.AreEqual(4, resultPredicate[3]);
                Assert.AreEqual(5, resultPredicate[4]);
            }
        }