New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BeEquivalentTo throws a NullReferenceException #1932
Comments
Please include a complete and minimal example that we can run, i.e. a snippet I can copy directly into an IDE an run. |
Sure thing, I've updated my question |
This is still not valid code. |
I tried this on my machine to reproduce the error, but no errors occurred. Parallel.For(0, 10000, new ParallelOptions { MaxDegreeOfParallelism = -1 }, _ =>
{
List<Option> expectedOptions = new List<Option>
{
new Option()
{
Id = "4b71bcd9",
Name = "AutoTest_123"
}
};
List<Option> actualOptions = new List<Option>
{
new Option()
{
Id = "4b71bcd9",
Name = "AutoTest_123"
}
};
for (var i = 0; i < expectedOptions.Count; i++)
{
expectedOptions[i].Should().BeEquivalentTo(actualOptions[i]);
}
}); Have you tried reproducing the error in a fresh project? The error indicates the problem lies somewhere, but nothing strikes me fluentassertions/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs Lines 730 to 814 in d77966d
|
Nope, no using |
We run thousands of tests in parallel using xUnit and FluentAssertions for the last 10 years. We never experienced anything like that. If there was a concurrency issue, the problem was always with the code under test. So to diagnose this, we need more information. It's also weird that I don't see line numbers in your stack trace. |
We have had a few bug fixes related the concurrency issues in the library itself 😉 Here are two cases where the problem was calling methods that are by-design not thread-safe. I'd love if you have time to help us dig into what is causing this issue for you. Is the problem reproducible for you or did it only happened once? |
@jnyrup, Unfortunately the problem is still reproducible on my end while running tests in parallel. I don't use any [Test]
[Category(TestCategories.Regression)]
[Category(TestCategories.Medium)]
public void VIS11111()
{
That.Given(_ => ApiSteps.TestMe())
.BDDfy("Test #1: BlaBlaBla");
} public void TestMe()
{
var expectedOptions = new List<Option>()
{
new Option()
{
Id = "123",
Name = "Test"
}
};
var actualOptions = new List<Option>()
{
new Option()
{
Id = "123",
Name = "Test"
}
};
for (var i = 0; i < expectedOptions.Count; i++)
{
expectedOptions[i].Should().BeEquivalentTo(actualOptions[i]);
}
} public class Option
{
public string Id { get; set; }
public string Name { get; set; }
} StackTrace (FYI @dennisdoomen )
Technologies: Windows Specifications: |
I'm trying to reconstruct some runnable code, but I can't see where |
@jnyrup I've updated the previous post and uploaded test framework skeleton |
Thanks for the complete example, it made it a lot easier to diagnose.
One way to solve it is to move the setup of Fluent Assertions into a [System.Runtime.CompilerServices.ModuleInitializer]
internal static void SetupFluentAssertions()
{
AssertionOptions.AssertEquivalencyUsing(options => options
.Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeSameDateAs(ctx.Expectation))
.WhenTypeIs<DateTimeOffset>()
.Using<DateTime>(ctx => ctx.Subject.Should().BeSameDateAs(ctx.Expectation))
.WhenTypeIs<DateTime>()
);
} |
@jnyrup, Thanks a lot for digging into the problem resolving. I appreciate your help. It works now! |
Maybe we could add some detection logic that throws when you try to call the |
We could add a lock inside I don't think we should serialize reading It valid to call My concern about this is how much it helps, as it might just hide that it's still unsafe to call multiple times as it can change the outcome of a test. |
I just got the same error and it nearly drove me crazy because it occurred only randomly. |
I only meant trying to detect concurrent calls. That should never happen. Successive calls are fine. |
Description
while running tests in parallel ( 2 threads) and when using
BeEquivalentTo()
to compare objects from two lists a null reference exception is thrown.However no NRE is thrown on using single thread
Complete minimal example reproducing the issue
Expected behavior:
The assertion should be true.
Actual behavior:
Versions
FluentAssertions Version="6.7.0"
The text was updated successfully, but these errors were encountered: