-
Notifications
You must be signed in to change notification settings - Fork 606
Mechanism of operation aborting valid codepath #158
Comments
First, yes, gomock needs better documentation. Second, gomock uses the passed in TestReporter, which has Errorf and Fatalf. The most common way to use gomock is to pass in the testing.T for the test, which implements TestReporter. testing.T.Fatalf behaves a bit like panic (I think it actually calls runtime.Goexit). So that's where that comes from. You could implement your own TestHelper with a Fatalf that doesn't panic and isn't really fatal. Then execution will continue but it's not clear if that will be better because gomock doesn't know what to do for a function call that hasn't been registered. It's hard to come up with a general solution for this problem. One could easily write code that deadlocks if the tested function returns zero and the real implementation never returns zero. If the function call is not specified for the mock and execution is not aborted then the mock has to return some value, probably zero. Finally, I'm also not sure if I understand the negative consequences of this issue. In this case the test fails, which is what you would want. I imagine hanging tests would time out and then fail that way. Can you show me a case where the test is skipped? That would be bad. Or maybe elaborate on the negative consequences? Thanks for the report! |
Thanks for the response. To answer your last question(s): The issue is that, because the gomock uses exceptions for flow control, it bypasses the To make it more clear, change My use of A tester might spend several hours trying to track down the cause of the deadlock, thinking it might be in the code path being tested, when the error was actually introduced by the mocking framework. For example :-) |
gomock is jumping out of a normal code execution, causing code that would normally be executed a normal program to be bypassed. This means any clean-up after a failed mock call will have unexpected consequences such as:
The last point is used as an example below. The code being tested is a series of functions that use a global mutex for synchronization.
Code to be tested:
Test code
Command to generate mock:
Output
Expected behavior
The mutex would in practice only be left in a locked state if the mocked functions panic'd, which is not the behavior being tested. This results in incorrect execution of the tests, including in some cases tests hanging, tests being skipped, and an inability to properly execute tests.
If this is known behavior, and gomock is incompatible with some packages or requires special handling when using those functions, it'd be great to have this documented. E.g., a blurb saying that gomock uses
panic
to manage code flow would help people understand what can and can't be tested using gomock.The text was updated successfully, but these errors were encountered: