Example #1
0
        public void TestGroupByException()
        {
            var srcAsync = AsyncEnum.Enumerate <int>(async consumer =>
            {
                await consumer.YieldAsync(1);
                throw new TestUtils.SomeTestException();
            });

            var asyncGroupsEnum = srcAsync.GroupBy(x => x).GetEnumerator();

            Assert.IsTrue(asyncGroupsEnum.MoveNextAsync().AwaitSynchronously());
            var asyncGroup1Enum = asyncGroupsEnum.Current.GetEnumerator();

            try
            {
                asyncGroupsEnum.MoveNextAsync().AwaitSynchronously();
                Assert.Fail();
            }
            catch (TestUtils.SomeTestException)
            { }

            Assert.IsTrue(asyncGroup1Enum.MoveNextAsync().AwaitSynchronously());
            Assert.AreEqual(1, asyncGroup1Enum.Current);

            try
            {
                asyncGroup1Enum.MoveNextAsync().AwaitSynchronously();
                Assert.Fail();
            }
            catch (TestUtils.SomeTestException)
            { }
        }
Example #2
0
        public void TestMultithreadedFeed()
        {
            IEnumerable <IEnumerable <Tuple <object, int> > > expectedItemGroups = null;
            var items = AsyncEnum.Enumerate <Tuple <object, int> >(async consumer =>
            {
                List <Task <IEnumerable <Tuple <object, int> > > > tasks = new List <Task <IEnumerable <Tuple <object, int> > > >();
                for (int i = 0; i < 10; i++)
                {
                    tasks.Add(Task.Factory.StartNew(threadIdx =>
                    {
                        List <Tuple <object, int> > threadItems = new List <Tuple <object, int> >();
                        for (int j = 0; j < 1000; j++)
                        {
                            var item = Tuple.Create(threadIdx, j);
                            consumer.YieldAsync(item);
                            threadItems.Add(item);
                        }

                        return((IEnumerable <Tuple <object, int> >)threadItems);
                    }, i));
                }
                expectedItemGroups = await Task.WhenAll(tasks);
            });

            var actualItems = items.ToList();

            var match = from actualItem in actualItems
                        group actualItem by actualItem.Item1 into actualGroup
                        join expectedItemGroup in expectedItemGroups on actualGroup.Key equals expectedItemGroup.First().Item1
                        select new { Actual = actualGroup, Expected = expectedItemGroup };

            Assert.IsTrue(match.All(x => x.Actual.SequenceEqual(x.Expected)));
        }
Example #3
0
 public static IAsyncEnumerable <T> GetFailingEnumerator <T>()
 {
     return(AsyncEnum.Enumerate <T>(async consumer =>
     {
         throw new SomeTestException();
     }));
 }
Example #4
0
 public static IAsyncEnumerable <T> EnumerateNothingForever <T>()
 {
     return(AsyncEnum.Enumerate <T>(async consumer =>
     {
         while (true)
         {
             await Task.Delay(TimeSpan.FromHours(1));
         }
     }));
 }
Example #5
0
 public static IAsyncEnumerable <T> EnumerateWithDelay <T>(IEnumerable <T> source, int delayMs)
 {
     return(AsyncEnum.Enumerate <T>(async consumer =>
     {
         foreach (T item in source)
         {
             await Task.Delay(delayMs);
             await consumer.YieldAsync(item);
         }
     }));
 }
Example #6
0
        private async Task TestGroupByRandomAccessAsync()
        {
            Func <int, int> groupFunction = k => k % 10;
            int             n             = 10000;

            var rnd            = new Random(0);
            var src            = Enumerable.Range(0, n).Select(x => rnd.Next(1000)).ToList();
            var expectedGroups = src.GroupBy(groupFunction);
            var expectedKeys   = expectedGroups.Select(g => g.Key);

            var asyncSrc = AsyncEnum.Enumerate <int>(async consumer =>
            {
                foreach (var srcItem in src)
                {
                    //if (rnd.Next(10) == 0)
                    //    await TaskEx.Delay(rnd.Next(10) * 10);
                    var awaitDemandTask = consumer.YieldAsync(srcItem);
                    if (rnd.Next(4) == 0)
                    {
                        await awaitDemandTask;
                    }
                }
            });

            var asyncGroupsEnumerator =
                asyncSrc.GroupBy(groupFunction).GetEnumerator();

            var actualKeys = new List <int>();

            var actualAsyncEnumerators  = new List <IAsyncEnumerator <int> >();
            var pendingAsyncEnumerators = new List <Tuple <List <int>, IAsyncEnumerator <int> > >();
            var actualGroupValues       = new List <List <int> >();

            int foundValues = 0;

            while (foundValues < n)
            {
                if ((pendingAsyncEnumerators.Count == 0) || (rnd.Next(2000) == 0))
                {
                    if (await asyncGroupsEnumerator.MoveNextAsync())
                    {
                        actualKeys.Add(asyncGroupsEnumerator.Current.Key);
                        var innerAsyncEnumerator = asyncGroupsEnumerator.Current.GetEnumerator();
                        actualAsyncEnumerators.Add(innerAsyncEnumerator);
                        var values = new List <int>();
                        pendingAsyncEnumerators.Add(Tuple.Create(values, innerAsyncEnumerator));
                        actualGroupValues.Add(values);
                    }
                }
                else
                {
                    int nextGroupIdx = rnd.Next(pendingAsyncEnumerators.Count);
                    var pendingGroup = pendingAsyncEnumerators[nextGroupIdx];
                    if (await pendingGroup.Item2.MoveNextAsync())
                    {
                        pendingGroup.Item1.Add(pendingGroup.Item2.Current);
                        foundValues++;
                    }
                    else
                    {
                        pendingAsyncEnumerators.RemoveAt(nextGroupIdx);
                    }
                }
            }

            Assert.IsTrue(expectedKeys.SequenceEqual(actualKeys));
            Assert.AreEqual(expectedGroups.Count(), actualGroupValues.Count);
            foreach (var tuple in expectedGroups.Zip(actualGroupValues, Tuple.Create))
            {
                Assert.IsTrue(tuple.Item1.SequenceEqual(tuple.Item2));
            }
        }
Example #7
0
 public static IAsyncEnumerable <T> EnumerateSyncNoWait <T>(IEnumerable <T> source)
 {
     return(AsyncEnum.Enumerate <T>(consumer => GetSyncNoWaitIterator(source, consumer)));
 }