Exemple #1
0
        public void ForEachIteratesItemsInParallel()
        {
            var strings     = LangTestHelpers.RandomStrings(100, 50);
            var results     = new ConcurrentQueue <Tuple <int, string> >();
            var threadCount = 10;

            Parallel.ForEach(
                threadCount,
                CancellationToken.None,
                strings,
                it =>
            {
                Thread.Sleep(10);
                results.Enqueue(Tuple.Create(Environment.CurrentManagedThreadId, it));
            }
                );
            Assert.AreEqual(threadCount, results.Select(it => it.Item1).Distinct().Count());
            CollectionAssert.AreEquivalent(strings, results.Select(it => it.Item2));
        }
Exemple #2
0
        public void ForEachObeysCancellationToken()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            System.Threading.Tasks.Task.Run(() =>
            {
                Thread.Sleep(50);
                cancellationTokenSource.Cancel();
            });
            var itemsConsumed = new ConcurrentQueue <int>();
            var itemsFinished = new ConcurrentQueue <int>();

            Assert.Throws <OperationCanceledException>(() => Parallel.ForEach(
                                                           10,
                                                           cancellationTokenSource.Token,
                                                           Enumerable.Range(0, 1000).Select(it => { itemsConsumed.Enqueue(it); return(it); }),
                                                           it => { Thread.Sleep(5); itemsFinished.Enqueue(it); }
                                                           ));
            CollectionAssert.AreEquivalent(itemsConsumed, itemsFinished);
            Assert.GreaterOrEqual(itemsConsumed.Count, 10);
            Assert.LessOrEqual(itemsConsumed.Count, 100);
        }
Exemple #3
0
        public void ForEachTerminatesUponFailure()
        {
            var threadCount   = 12;
            int itemsReturned = 0;

            try
            {
                var throwExceptions = new CountdownEvent(threadCount);
                var threadsRun      = new ConcurrentDictionary <int, int>();
                Parallel.ForEach(
                    threadCount,
                    CancellationToken.None,
                    Infinite().Select(it => { itemsReturned = it; return(it); }),
                    i =>
                {
                    threadsRun.AddOrUpdate(
                        Thread.CurrentThread.ManagedThreadId,
                        k => { throwExceptions.Signal(); return(1); },
                        (k, v) => 1
                        );
                    Thread.Sleep(1);
                    if (throwExceptions.IsSet)
                    {
                        throw new InvalidOperationException("test exception");
                    }
                }
                    );
                Assert.Fail("Should have thrown an AggregateException");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(threadCount, e.InnerExceptions.Count);
                foreach (var inner in e.InnerExceptions)
                {
                    Assert.IsInstanceOf <InvalidOperationException>(inner);
                }
            }
        }