-
Notifications
You must be signed in to change notification settings - Fork 10k
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
[Bug]: documented undefined behavior seems to make common use of fixtures illegal #4339
Comments
@dinord since you self-assigned this, any thoughts on this? |
We'll discuss this internally next week. |
We discovered that even some of GoogleTest's own unit tests depend on this kind of undefined behavior. Still, there's some debate as to whether defining this behavior would encourage undesired patterns and how to make this thread-safe. I'll get back to this issue once I have more internal feedback about those. |
We need to do some work internally before moving forward with this issue, and we haven't had the chance to prioritize it yet. |
Are you able to share any progress on this? |
I found this while trying to understand the reason behind the UB: googletest/googlemock/include/gmock/gmock-spec-builders.h Lines 226 to 236 in eff443c
Indeed during the EXPEC_CALL calls and the calls to the mocked method the lock is not acquired: googletest/googlemock/include/gmock/gmock-spec-builders.h Lines 1578 to 1580 in eff443c
googletest/googlemock/include/gmock/gmock-spec-builders.h Lines 1789 to 1793 in eff443c
@dinord can you at least confirm that EXPECT_CALL order restriction is to prevent UB due to data races? Many projects do not have multithreaded UTs and do not care at all about internal locks in gmock. If this is the case, then why don't simply state something like: "the Mock classes are not threadsafe, please handle locking on your own". |
Describe the issue
I first submitted this issue under discussions, but I think it is actually a bug. Unless I misunderstand the documentation, the defined behavior of EXPECT_CALL limits the usability of fixtures to the point of uselessness.
As the cookbook mentions it is good practice to verify one thing in one test. In order to do this, there is often some setup required before a test can perform the calls it is interested in.
The problem comes when that setup leads to calls on a mock that the test later wants to set EXPECTations on.
As the cookbook says: "gMock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined."
Is there a way to avoid undefined behavior and still have useful fixtures?
Things I have considered:
EXPECT_CALL()
s and calls to the mock function *really* undefined behavior? #2828 (comment) we could recreate the mocks in place to get around UB. This doesn't sound like the easiest possible solution.Steps to reproduce the problem
Given a class S that calls method f of class D
I could give code, but code can't be used to show that behavior is undefined.
What version of GoogleTest are you using?
I'm basing this on the latest documentation at https://google.github.io/googletest/gmock_for_dummies.html#using-mocks-in-tests
What operating system and version are you using?
n.a.
What compiler and version are you using?
n.a.
What build system are you using?
n.a.
Additional context
n.a.
The text was updated successfully, but these errors were encountered: