-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fixes #116 # Problem If your mock an interface that matches the fmt.Stringer interface: ```go type Stringer interface { String() string } ``` Then your unit tests can deadlock if you provide that mock as an expected argument to a call that is not matched (i.e. fails the test). Because, when printing the error message for the call that was not matched, it calls `String()` on all arguments that support it, including the mock. But each call to a mock is protected with a mutex, and the previous call (that was not matched) has not yet exited. # Solution The solution has two parts 1. During mock code generation (an existing part of this library) add a unique method on mocks (ISGOMOCK) 1. During test execution, whenever we are stringifying something that might be a mock, check if it is a mock (ISGOMOCK) that implements the String() method. If it is, just use the type name as the string value, instead of calling String() (which would cause the deadlock).
- Loading branch information
Showing
6 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package gomock | ||
|
||
import "fmt" | ||
|
||
type mockInstance interface { | ||
ISGOMOCK() struct{} | ||
} | ||
type mockedStringer interface { | ||
fmt.Stringer | ||
mockInstance | ||
} | ||
|
||
// getString is a safe way to convert a value to a string for printing results | ||
// If the value is a a mock, getString avoids calling the mocked String() method, | ||
// which avoids potential deadlocks | ||
func getString(x any) string { | ||
switch v := x.(type) { | ||
case mockedStringer: | ||
return fmt.Sprintf("%T", v) | ||
case fmt.Stringer: | ||
return v.String() | ||
default: | ||
return fmt.Sprintf("%v", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters