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

Mock can be deadlocked by a panic #1157

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion mock/mock.go
Expand Up @@ -812,7 +812,16 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
}

if matcher, ok := expected.(argumentMatcher); ok {
if matcher.Matches(actual) {
var matches bool
func() {
defer func() {
dolmen marked this conversation as resolved.
Show resolved Hide resolved
if r := recover(); r != nil {
actualFmt = fmt.Sprintf("panic in argument matcher: %v", r)
}
}()
matches = matcher.Matches(actual)
}()
if matches {
output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher)
} else {
differences++
Expand Down
26 changes: 26 additions & 0 deletions mock/mock_test.go
Expand Up @@ -233,6 +233,32 @@ func Test_Mock_On_WithIntArgMatcher(t *testing.T) {
})
}

func Test_Mock_On_WithArgMatcherThatPanics(t *testing.T) {
var mockedService TestExampleImplementation

mockedService.On("TheExampleMethod2", MatchedBy(func(_ interface{}) bool {
panic("try to lock mockedService")
})).Return()

defer func() {
assertedExpectations := make(chan struct{})
go func() {
tt := new(testing.T)
mockedService.AssertExpectations(tt)
close(assertedExpectations)
}()
select {
case <-assertedExpectations:
case <-time.After(time.Second):
t.Fatal("AssertExpectations() deadlocked, did the panic leave mockedService locked?")
}
}()

assert.Panics(t, func() {
mockedService.TheExampleMethod2(false)
})
}

func TestMock_WithTest(t *testing.T) {
var (
mockedService TestExampleImplementation
Expand Down