-
Notifications
You must be signed in to change notification settings - Fork 735
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
Assert.Multiple throws 'The assertion scope was disposed out of order' when run in parallel #4814
Comments
@bording Thanks for reporting. Just because you didn't get an error in 4.1, because there was no checking, doesn't mean the code behaved as expected. Some background. In your example case, it looks like what should be 1000 independent Scenario 1: Reporting failures for the wrong iteration:
Scenario 2: Out of order.
Your code acts more or less like: Assert.Multiple(() =>
{
Parallel.For(1, 1000, i => Assert.That(x, Is.EqualTo(1)));
}); Parallel execution of So even though your code didn't throw Because several part of NUnit use |
So is there any way to get an It seems to me that a simpler version that doesn't need the counter and doesn't supported nested Multiple calls would be useful to have as well. |
I agree this might be be nice to have a thread-localized or thread-safe multiple scope context. Though I would also suggest that if something weren't thread-safe then it better to fail noisily than fail silently. @bording [Test]
[Parallelizable]
public void Repro([Range(1, 1000)] int x)
{
Assert.Multiple(() => {
Assert.That(x, Is.EqualTo(1)
});
} |
We recently upgraded from NUnit 3.14.0 to NUnit 4.1.0, and as part of that upgrade process we added In order to successfully upgrade to 4.2.2, I've gone ahead and removed |
Ah, I see! Thanks for showing an example. It looks like the test is itself testing that something can succeed when run in parallel. That is a good point too about analyzers prompting to convert towards @nunit/framework-team I think this might be a good thing for us to fix. |
I think another important point here is that, previously, the lack of thread safety in a multiple scope would only be visible in a failure case. Now, our disposal checks are making the lack of thread safety visible on the happy path too, when all assertions would otherwise pass |
@stevenaw I don't see this as a bug. I would go so far that a lot of NUnit doesn't support multi-threading in a single test. As far as the analyzers go suggestion |
Perhaps enhancement then? My thinking was that most asserts are thread safe, but the original design of Assert.Multiple() wasn't. It would be nice if that inconsistency were fixed so that consumers needn't worry about the difference. |
I have run into a problem that seems to have been introduced in NUnit 4.2.1 and is also a problem with 4.2.2.
We have some tests that are using
Parallel.For
to test some parallelization logic, and they are now throwingInvalidOperationException
with 4.2.x but are passing fine with 4.1.0.Given the following simplified example test:
This test works fine in 4.1.0, but with 4.2.1+ it throws the following error instead:
This seems to have been introduced as part #4758.
I see that protection against out of order disposal was included as part of that PR, but it feels like maybe this logic is incorrectly throwing here?
The text was updated successfully, but these errors were encountered: