Skip to content

Concurrency issue with ResultFormatters and ResultAssertions<T> #256

Description

@MIJamie

The tests in my project are randomly failing with the error: System.InvalidOperationException : Collection was modified. After tracing the error I believe this is caused by a concurrency issue in Extensions.FluentAssertions in the static constructors for ResultAssertions and ResultAssertions<T>. These constructors are clearly designed to run once since Formatter.AddFormatter from FluentAssertions is not thread safe. Unfortunately static constructors for generic types are run for every concrete type used. This causes my tests to randomly fail when the constructors end up being run concurrently.

Reproducing the error:

using FluentAssertions;
using FluentResults;
using FluentResults.Extensions.FluentAssertions;

var successA = Result.Ok(new TypeA());
var successB = Result.Ok(new TypeB());
var successC = Result.Ok();

successA.Should().BeSuccess();
successB.Should().BeSuccess();
successC.Should().BeSuccess();

class TypeA { }
class TypeB { }

This is unlikely to cause the concurrency exception but it does demonstrate the issue. ResultFormatters.Register() will be called three times, and an instance of ErrorListValueFormatter will be added to FluentAssertion's Formatter.CustomFormatters three times. It should only be called once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions