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.
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 forResultAssertionsandResultAssertions<T>. These constructors are clearly designed to run once sinceFormatter.AddFormatterfrom 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:
This is unlikely to cause the concurrency exception but it does demonstrate the issue.
ResultFormatters.Register()will be called three times, and an instance ofErrorListValueFormatterwill be added to FluentAssertion'sFormatter.CustomFormattersthree times. It should only be called once.