fix race conditions related to mutation of expectedCall.Repeatability

This commit is contained in:
Levi Corcoran 2015-10-07 16:34:11 -05:00
parent 2b15294402
commit 3c35d25e9b

View File

@ -205,7 +205,9 @@ func (self *Mock) On(methodName string, arguments ...interface{}) *Call {
// */ // */
func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) { func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) {
for i, call := range m.expectedCalls() { m.mutex.Lock()
defer m.mutex.Unlock()
for i, call := range m.ExpectedCalls {
if call.Method == method && call.Repeatability > -1 { if call.Method == method && call.Repeatability > -1 {
_, diffCount := call.Arguments.Diff(arguments) _, diffCount := call.Arguments.Diff(arguments)
@ -349,16 +351,19 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
// iterate through each expectation // iterate through each expectation
expectedCalls := m.expectedCalls() expectedCalls := m.expectedCalls()
for _, expectedCall := range expectedCalls { for _, expectedCall := range expectedCalls {
switch { if !m.methodWasCalled(expectedCall.Method, expectedCall.Arguments) {
case !m.methodWasCalled(expectedCall.Method, expectedCall.Arguments):
somethingMissing = true somethingMissing = true
failedExpectations++ failedExpectations++
t.Logf("\u274C\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String()) t.Logf("\u274C\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
case expectedCall.Repeatability > 0: } else {
somethingMissing = true m.mutex.Lock()
failedExpectations++ if expectedCall.Repeatability > 0 {
default: somethingMissing = true
t.Logf("\u2705\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String()) failedExpectations++
} else {
t.Logf("\u2705\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
}
m.mutex.Unlock()
} }
} }