From 9768526b706b87960cd70933a3d0d20604920ae5 Mon Sep 17 00:00:00 2001 From: Lee Packham Date: Wed, 25 Mar 2015 12:27:39 +0000 Subject: [PATCH] Fix race condition with ExpectedCalls There is race condition caused when a method being tested calls a mocked method within a go routine. For example, caching might be done a go routine and that caching might be mocked. There is already a mutex protecting the lists on the Mock object - however Return() appends to ExpectedCalls and findExpectedCall could run at the same time. --- mock/mock.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mock/mock.go b/mock/mock.go index fa8747e..168bba2 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -112,6 +112,9 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Mock { // // Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2) func (m *Mock) Return(returnArguments ...interface{}) *Mock { + m.mutex.Lock() + defer m.mutex.Unlock() + m.ExpectedCalls = append(m.ExpectedCalls, Call{m.onMethodName, m.onMethodArguments, returnArguments, 0, nil, nil}) return m }