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); }
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(); }
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()); }
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()); }
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(); } }
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()); }