// Generalized test for testing Partitioner ForEach-loop results private static void PartitionerForEachPLRTest( Action <int, ParallelLoopState> body, string desc, bool excExpected, bool shouldComplete, bool shouldStop, bool shouldBreak) { List <int> list = new List <int>(); for (int i = 0; i < 20; i++) { list.Add(i); } MyPartitioner <int> mp = new MyPartitioner <int>(list); try { ParallelLoopResult plr = Parallel.ForEach(mp, body); Assert.False(excExpected); Assert.Equal(shouldComplete, plr.IsCompleted); Assert.Equal(shouldStop, plr.LowestBreakIteration == null); Assert.Equal(shouldBreak, plr.LowestBreakIteration != null); } catch (AggregateException) { Assert.True(excExpected); } }
public static void PartitionerForEachPLRTests() { // // Now try testing Partitionable, OrderablePartitionable // List <int> intlist = new List <int>(); for (int i = 0; i < 20; i++) { intlist.Add(i * i); } MyPartitioner <int> mp = new MyPartitioner <int>(intlist); ParallelLoopResult plr = Parallel.ForEach(mp, delegate(int item, ParallelLoopState ps) { if (item == 0) { ps.Stop(); } }); PLRcheck(plr, "Partitioner-ForEach-Stop", false, null); plr = Parallel.ForEach(mp, delegate(int item, ParallelLoopState ps) { }); PLRcheck(plr, "Partitioner-ForEach-Complete", true, null); }
public static void TestStringInZipFeature_Custom_Partitioner() { // We prepared 15 sample zips (1.zip - 15.zip) with text files // inside. Some of them contain the string 'test'. DirectoryInfo path = Directory.GetParent(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()))); List<ZipPackage> packages = new List<ZipPackage>(); for (int i = 1; i <= 15; i++) packages.Add(new ZipPackage(string.Format("{0}\\zips\\{1}.zip", path, i))); string str = "test"; // Select all files List<FileInZip> filesInZips = (from zipPackage in packages from file in zipPackage.Files select file).ToList(); // Custom partitioner MyPartitioner customPartitioner = new MyPartitioner(filesInZips); // PLINQ var query = (from file in customPartitioner.AsParallel() where file.ContainsString(str) select file).ToList(); Debug.WriteLine("Number of files containing '{0}': {1}", str, query.Count); }
// Generalized test for testing Partitioner ForEach-loop results private static void PartitionerForEachPLRTest( Action <int, ParallelLoopState> body, string desc, bool excExpected, bool shouldComplete, bool shouldStop, bool shouldBreak) { List <int> list = new List <int>(); for (int i = 0; i < 20; i++) { list.Add(i); } MyPartitioner <int> mp = new MyPartitioner <int>(list); try { ParallelLoopResult plr = Parallel.ForEach(mp, body); if (excExpected) { Logger.LogInformation("PartitionerForEach-PLRTest -- {0}: > failed. Expected an exception.", desc); } else if ((plr.IsCompleted != shouldComplete) || (shouldStop && (plr.LowestBreakIteration != null)) || (shouldBreak && (plr.LowestBreakIteration == null))) { Logger.LogInformation("PartitionerForEach-PLRTest -- {0} > failed. Complete={1}, LBI={2}", desc, plr.IsCompleted, plr.LowestBreakIteration); } } catch (AggregateException e) { if (!excExpected) { Logger.LogInformation("PartitionerForEach-PLRTest -- {0} > failed -- unexpected exception from loop. Exception: {1}", desc, e); } } }