예제 #1
0
        public static void CTT_NonSorting_AsynchronousMergerEnumeratorDispose()
        {
            int size = 10000;
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            Exception caughtException           = null;

            IEnumerator <int>   enumerator = null;
            ParallelQuery <int> query      = null;

            query = Enumerable.Range(1, size).AsParallel()
                    .WithCancellation(tokenSource.Token)
                    .Select(i =>
            {
                enumerator.Dispose();
                return(i);
            });

            enumerator = query.GetEnumerator();
            try
            {
                for (int j = 0; j < 1000; j++)
                {
                    enumerator.MoveNext();
                }
            }
            catch (Exception ex)
            {
                caughtException = ex;
            }

            Assert.NotNull(caughtException);
        }
예제 #2
0
        public static void ImmediateDispose(Labeled <ParallelQuery <int> > labeled, int count)
        {
            // Regression test for an issue causing ODE if a queryEnumerator is disposed before moveNext is called.
            ParallelQuery <int> query      = labeled.Item;
            IEnumerator <int>   enumerator = query.GetEnumerator();

            Assert.NotNull(enumerator);
            enumerator.Dispose();
        }
예제 #3
0
        public static void GetEnumerator_MoveNextAfterQueryOpeningFailsIsIllegal(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item.Select <int, int>(x => { throw new DeliberateTestException(); }).OrderBy(x => x);

            IEnumerator <int> enumerator = query.GetEnumerator();

            //moveNext will cause queryOpening to fail (no element generated)
            Functions.AssertThrowsWrapped <DeliberateTestException>(() => enumerator.MoveNext());

            //moveNext after queryOpening failed
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
        }
예제 #4
0
        private static void DisposedEnumerator(ParallelQuery <int> query, bool delay = false)
        {
            query = query.WithCancellation(new CancellationTokenSource().Token).Select(x => x);

            IEnumerator <int> enumerator = query.GetEnumerator();

            enumerator.MoveNext();
            if (delay)
            {
                Task.Delay(10).Wait();
            }
            enumerator.MoveNext();
            enumerator.Dispose();

            Assert.Throws <ObjectDisposedException>(() => enumerator.MoveNext());
        }
예제 #5
0
        public static void Repeat_Reset()
        {
            const int Value      = 42;
            const int Iterations = 3;

            ParallelQuery <int> q = ParallelEnumerable.Repeat(Value, Iterations);

            IEnumerator <int> e = q.GetEnumerator();

            for (int i = 0; i < 2; i++)
            {
                int count = 0;
                while (e.MoveNext())
                {
                    Assert.Equal(Value, e.Current);
                    count++;
                }
                Assert.False(e.MoveNext());
                Assert.Equal(Iterations, count);

                e.Reset();
            }
        }
예제 #6
0
        private static void DisposedEnumerator(ParallelQuery<int> query, bool delay = false)
        {
            query = query.WithCancellation(new CancellationTokenSource().Token).Select(x => x);

            IEnumerator<int> enumerator = query.GetEnumerator();

            enumerator.MoveNext();
            if (delay) Task.Delay(10).Wait();
            enumerator.MoveNext();
            enumerator.Dispose();

            Assert.Throws<ObjectDisposedException>(() => enumerator.MoveNext());
        }