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

Moq still doesn't support multithreading #91

Closed
VitaliyKurokhtin opened this issue Feb 11, 2014 · 2 comments
Closed

Moq still doesn't support multithreading #91

VitaliyKurokhtin opened this issue Feb 11, 2014 · 2 comments

Comments

@VitaliyKurokhtin
Copy link

VitaliyKurokhtin commented Feb 11, 2014

Just came across following issue with Moq (4.2.1312.1622). Consider interface:

public IMyInterface
{
    void Do(int arg);
}

... setup:

var mock = Mock.Create<IMyInterface>();
mock.Setup(x => x.Do(0)).Verifiable();
mock.Setup(x => x.Do(1)).Verifiable();

... and invocation:

var obj = mock.Object;
var t1 = Task.Run(() => obj.Do(0));
var t2 = Task.Run(() => obj.Do(1));

Task.WhenAll(t1, t2).Wait();
mock.Verify();

Every now and then this verification will fail saying that one of two setups was not matched. But will succeed if changed to:

mock.Verify(x => x.Do(0), Times.Once);
mock.Verify(x => x.Do(1), Times.Once);

This happens because method call invocation logic inside Interceptor is not thread safe. When Interceptor.Intercept method is called it creates strategy pipeline, one of strategies in this pipeline - ExtractProxyCall - locates matched method call and places it into InterceptStrategyContext.CurrentCall. Which is later used by ExecuteCall strategy to do the actual call.

The problem here is that both executing threads will work with same InterceptorContext instance. So when executed in parallel it's possible of one ExtractProxyCall strategy to overwrite method call just located by another ExtractProxyCall strategy, that way both ExecuteCall strategies will end up executing same method call twice. This can easily be confirmed by attaching callbacks to original setups.

mock.Verify(x => ...) on the other hand does verification evaluating given expression against collected call contexts which, obviously, contains correct information.

@stakx
Copy link
Contributor

stakx commented Jun 22, 2017

@VitaliyKurokhtin: I am closing this issue as I strongly suspect that this has been fixed long ago (possibly by commit 1948508). Most of what you mentioned above no longer exists today in the same form. For instance, InterceptStrategyContext.CurrentCall appears to have been refactored into CurrentInterceptorContext.Call, and each interception works with a different copy of that.

I tried reproducing this issue (but didn't succeed).

@stakx stakx closed this as completed Jun 22, 2017
@VitaliyKurokhtin
Copy link
Author

Yes. I can confirm that I wasn't able to repro this at least on the last version. Thanks!

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

No branches or pull requests

2 participants