Skip to content
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

Let mock.Invocations.Clear() remove traces of earlier invocations more thoroughly #854

Merged
merged 3 commits into from
Aug 8, 2019

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Jun 30, 2019

Resolves #733.

@stakx stakx added this to the 4.12.1 milestone Jun 30, 2019
src/Moq/InvocationCollection.cs Show resolved Hide resolved
@@ -68,6 +76,9 @@ public void Clear()
this.invocations = null;
this.count = 0;
this.capacity = 0;

this.owner.Setups.UninvokeAll();
// ^ TODO: Currently this could cause a deadlock as another lock will be taken inside this one!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raises the question of whether Moq should generally perform more coarse-grained locking.

This would probably mean having just one lock object on the Mock instance that would be used both during setups and invocations.

Client code should probably perform all setups first, and only then start invoking the mocked object (possibly from several threads). Code that is structured that way already shouldn't be affected by a move to more coarse-grained locking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had already implemented this, but when I ran some benchmarking it turned out that more coarse-grained locking appears to be slower in general than if each collection kept its own locks.

It also appears that dead-locks currently cannot happen, because we don't have any cyclic call dependencies between SetupCollection and InvocationCollection (IIRC); but that could always change in the future if these collections are modified.

For now, let's leave this as a future TODO.

@stakx stakx modified the milestones: 4.12.1, 4.13.0 Jul 24, 2019
@stakx stakx force-pushed the issue-733 branch 2 times, most recently from d83cb8c to 86416a7 Compare August 8, 2019 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invocations.Clear() does not cause Verify to fail.
1 participant