Skip to content

Different results when rerunning CoverageReportParser against same input files #690

@afscrome

Description

@afscrome

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

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:

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy