-
-
Notifications
You must be signed in to change notification settings - Fork 295
Description
If I re-run CoverageReportParser.ParseFiles
against the same input coverage files, I'm getting different total line counts.
string[] coverageFiles = { /*...*/ };
var parallelism = 1;
CoverageReportParser parser = new CoverageReportParser(parallelism, parallelism, new string[] { }, new DefaultFilter(new string[] { }),
new DefaultFilter(new string[] { }),
new DefaultFilter(new string[] { }));
ReadOnlyCollection<string> collection = new ReadOnlyCollection<string>(coverageFiles);
var results = parser.ParseFiles(collection);
var lines = results.Assemblies.Select(x => x.CoveredLines).Sum();
Console.WriteLine($"LINES: {lines}");
// The above is printing different results from run to run
I believe the problem is coming from some kind of concurrency race condition - after removing the parallelism from the following line, I start getting consistent results.
Parallel.ForEach(classNames, c => this.ProcessClass(modules, assembly, c.Name, c.DisplayName)); |
Digging further, I came across the following, and confirmed that my report files are ending up with multiple classes with the same name. When the earlier parallelism is enabled, these duplicates get added in different orders, meaning the FirstOrDefault
can return different results if re-run against the same inputs.
var existingClass = this.classes.FirstOrDefault(c => c.Name == @class.Name); |
At this point I'm getting a bit lost in the weeds, but I have two theories for the root cause.
The first is that the Equals
method for ClassNameParserResult
compares both Name
and DisplayName
, meaning we get all unique PAIRS of Name and DisplayName - giving us opportunities for later duplicates
ReportGenerator/src/ReportGenerator.Core/Parser/CoberturaParser.cs
Lines 122 to 128 in 4444f14
var classNames = modules | |
.Where(m => m.Attribute("name").Value.Equals(assemblyName)) | |
.Elements("classes") | |
.Elements("class") | |
.Select(c => ClassNameParser.ParseClassName(c.Attribute("name").Value, this.RawMode)) | |
.Where(c => c.Include) | |
.Distinct() |
The other possible cause is a mismatch between the following bits of logic in filtering for elements for a given class:
ReportGenerator/src/ReportGenerator.Core/Parser/CoberturaParser.cs
Lines 122 to 128 in 4444f14
var classNames = modules | |
.Where(m => m.Attribute("name").Value.Equals(assemblyName)) | |
.Elements("classes") | |
.Elements("class") | |
.Select(c => ClassNameParser.ParseClassName(c.Attribute("name").Value, this.RawMode)) | |
.Where(c => c.Include) | |
.Distinct() |
ReportGenerator/src/ReportGenerator.Core/Parser/CoberturaParser.cs
Lines 149 to 157 in 4444f14
var files = modules | |
.Where(m => m.Attribute("name").Value.Equals(assembly.Name)) | |
.Elements("classes") | |
.Elements("class") | |
.Where(c => c.Attribute("name").Value.Equals(className) | |
|| (!this.RawMode | |
&& (c.Attribute("name").Value.StartsWith(className + "$", StringComparison.Ordinal) | |
|| c.Attribute("name").Value.StartsWith(className + "/", StringComparison.Ordinal) | |
|| c.Attribute("name").Value.StartsWith(className + ".", StringComparison.Ordinal)))) |
ReportGenerator/src/ReportGenerator.Core/Parser/CoberturaParser.cs
Lines 190 to 198 in 4444f14
var classes = modules | |
.Where(m => m.Attribute("name").Value.Equals(@class.Assembly.Name)) | |
.Elements("classes") | |
.Elements("class") | |
.Where(c => c.Attribute("name").Value.Equals(className) | |
|| (!this.RawMode | |
&& (c.Attribute("name").Value.StartsWith(className + "$", StringComparison.Ordinal) | |
|| c.Attribute("name").Value.StartsWith(className + "/", StringComparison.Ordinal) | |
|| c.Attribute("name").Value.StartsWith(className + ".", StringComparison.Ordinal)))) |