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

"Verify" on method called with Parallel.ForEach has the wrong number of calls #715

Closed
lucaslra opened this issue Nov 2, 2018 · 3 comments

Comments

@lucaslra
Copy link

lucaslra commented Nov 2, 2018

Hi,

I have a simple code that is called exactly 5 times through the use of Parallel.ForEach:

Parallel.ForEach(files, (file) => fileProcessor.UploadFile(file));

The method works exactly as expected, but I am facing a problem with my Moq verification of the call count.

I have this verification:

mockedfileProcessor.Verify(x => x.UploadFile(It.IsAny<File>()), **Times.Exactly(5)**);

But intermittently I get this errror:

Moq.MockException: 
Expected invocation on the mock exactly 5 times, but was 4 times: x => x.UploadFile(It.IsAny<File>())
No setups configured.

I reinforce that this error is intermittent, sometimes it succeeds... I get that this has relation with the parallel execution of the code, but there is a reliable way of fixing this?
I looked around in the internet but all the scenarios seem to be slightly different from mine.

Thanks in advance,

@stakx
Copy link
Contributor

stakx commented Nov 2, 2018

@lucaslra - Could you please post a minimally complete, self-contained, and executable code example that demonstrates this problem? (Perhaps not on every single run as this appears to be a threading issue, but it should be able to trigger the error every now and then.) From the little bit of code you've posted, it is hard to tell how exactly your real code looks. (For example, you have something called fileProcessor you use in the parallel loop, but you verify against mockedFileProcessor. It's these small details suggesting that one isn't getting the full picture here.)

@stakx
Copy link
Contributor

stakx commented Nov 2, 2018

P.S.: Could you please also state what version of Moq you're using? If you're not using the current version (4.10.0 at this time of writing), please make sure to update, re-run your code, and see if the problem persists.

@lucaslra
Copy link
Author

lucaslra commented Nov 6, 2018

Hi @stakx
I spent sometime trying to do what you've asked, but I really cannot create an isolated scenario that can reproduce the problem, which is pretty weird.
I guess it says that this is something on my setup.

I'll close the issue, but thanks for the help.

@lucaslra lucaslra closed this as completed Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants