Skip to content

Commit b5ce165

Browse files
author
Edward Raigosa
authored
fixing panic in calls to assertion with nil m.mutex (#1212)
* fixing panic in calls to assertion with nil m.mutex This reverts a change that was made in #1182 The PR makes m.mutex a pointer which now needs to be checked but it's not checked for nil everywhere. This should also help with these issues: - #1208 - #1210 * Revert throwing out the lock because other concurrent calls can already have it locked * fix go vet copy lock by using pointer * fix obj assignment for passing test
1 parent c206b2e commit b5ce165

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

mock/mock.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ type Mock struct {
255255
// this data completely allowing you to do whatever you like with it.
256256
testData objx.Map
257257

258-
mutex *sync.Mutex
258+
mutex sync.Mutex
259259
}
260260

261261
// String provides a %v format string for Mock.
@@ -282,10 +282,6 @@ func (m *Mock) TestData() objx.Map {
282282

283283
// Test sets the test struct variable of the mock object
284284
func (m *Mock) Test(t TestingT) {
285-
if m.mutex == nil {
286-
m.mutex = &sync.Mutex{}
287-
}
288-
289285
m.mutex.Lock()
290286
defer m.mutex.Unlock()
291287
m.test = t
@@ -316,9 +312,6 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
316312
}
317313
}
318314

319-
// Since we start mocks with the .On() function, m.mutex should be reset
320-
m.mutex = &sync.Mutex{}
321-
322315
m.mutex.Lock()
323316
defer m.mutex.Unlock()
324317
c := newCall(m, methodName, assert.CallerInfo(), arguments...)
@@ -526,9 +519,9 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
526519
h.Helper()
527520
}
528521
for _, obj := range testObjects {
529-
if m, ok := obj.(Mock); ok {
522+
if m, ok := obj.(*Mock); ok {
530523
t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)")
531-
obj = &m
524+
obj = m
532525
}
533526
m := obj.(assertExpectationser)
534527
if !m.AssertExpectations(t) {
@@ -545,9 +538,6 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
545538
if h, ok := t.(tHelper); ok {
546539
h.Helper()
547540
}
548-
if m.mutex == nil {
549-
m.mutex = &sync.Mutex{}
550-
}
551541

552542
m.mutex.Lock()
553543
defer m.mutex.Unlock()

0 commit comments

Comments
 (0)