/// <summary> /// Heavily exercises OrderBy, but only throws one user delegate exception to simulate an occasional failure. /// </summary> private static void OrderBy_OnlyOneExceptionCore(int range) { for (int dop = 1; dop <= 30; dop++) { int indexForException = range / (2 * dop); // eg 1000 items on 4-core, throws on item 125. AggregateException caughtAggregateException = null; try { var query = Enumerable.Range(0, range) .AsParallel().WithDegreeOfParallelism(dop) .OrderBy(i => { UserDelegateException.ThrowIf(i == indexForException); return(i); } ); foreach (int i in query) { } } catch (AggregateException e) { caughtAggregateException = e; } Assert.NotNull(caughtAggregateException); Assert.True(caughtAggregateException.InnerExceptions.Count == 1, string.Format("PLinqDelegateExceptions.OrderBy_OnlyOneException Range: {0}: AggregateException.InnerExceptions != 1.", range)); } }
/// <summary> /// Heavily exercises OrderBy, but only throws one user delegate exception to simulate an occassional failure. /// /// </summary> internal static bool OrderBy_OnlyOneException(int range) { TestHarness.TestLog(String.Format("* OrderBy_OnlyOneException({0})", range)); bool success = true; Console.Write(" DOP: "); for (int dop = 1; dop <= 30; dop++) { Console.Write(dop.ToString("00") + ".. "); if (dop % 10 == 0) { Console.Write(Environment.NewLine + " "); } int indexForException = range / (2 * dop); // eg 1000 items on 4-core, throws on item 125. AggregateException caughtAggregateException = null; try { var query = Enumerable.Range(0, range) .AsParallel().WithDegreeOfParallelism(dop) .OrderBy(i => { UserDelegateException.ThrowIf(i == indexForException); return(i); } ); foreach (int i in query) { } } catch (AggregateException e) { caughtAggregateException = e; } success &= (caughtAggregateException != null); success &= (caughtAggregateException.InnerExceptions.Count == 1); } Console.WriteLine(); return(success); }