Move mutexes inside mock struct (#128)

Better to not be constrained by global locks when having multiple moq
instances.

Co-authored-by: Diego Bernardes <diego.bernardes@outlook.com>
This commit is contained in:
Suhas Karanth 2020-08-16 12:03:17 +05:30 committed by GitHub
parent 80177adba8
commit c5b1da6fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 49 deletions

View File

@ -38,9 +38,9 @@ func TestMoq(t *testing.T) {
"func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)",
"panic(\"PersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")", "panic(\"PersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")",
"panic(\"PersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")", "panic(\"PersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")",
"lockPersonStoreMockGet.Lock()", "mock.lockGet.Lock()",
"mock.calls.Get = append(mock.calls.Get, callInfo)", "mock.calls.Get = append(mock.calls.Get, callInfo)",
"lockPersonStoreMockGet.Unlock()", "mock.lockGet.Unlock()",
"// ID is the id argument value", "// ID is the id argument value",
} }
for _, str := range strs { for _, str := range strs {
@ -72,9 +72,9 @@ func TestMoqWithStaticCheck(t *testing.T) {
"func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)",
"panic(\"PersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")", "panic(\"PersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")",
"panic(\"PersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")", "panic(\"PersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")",
"lockPersonStoreMockGet.Lock()", "mock.lockGet.Lock()",
"mock.calls.Get = append(mock.calls.Get, callInfo)", "mock.calls.Get = append(mock.calls.Get, callInfo)",
"lockPersonStoreMockGet.Unlock()", "mock.lockGet.Unlock()",
"// ID is the id argument value", "// ID is the id argument value",
} }
for _, str := range strs { for _, str := range strs {
@ -105,9 +105,9 @@ func TestMoqWithAlias(t *testing.T) {
"func (mock *AnotherPersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "func (mock *AnotherPersonStoreMock) Get(ctx context.Context, id string) (*Person, error)",
"panic(\"AnotherPersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")", "panic(\"AnotherPersonStoreMock.CreateFunc: method is nil but PersonStore.Create was just called\")",
"panic(\"AnotherPersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")", "panic(\"AnotherPersonStoreMock.GetFunc: method is nil but PersonStore.Get was just called\")",
"lockAnotherPersonStoreMockGet.Lock()", "mock.lockGet.Lock()",
"mock.calls.Get = append(mock.calls.Get, callInfo)", "mock.calls.Get = append(mock.calls.Get, callInfo)",
"lockAnotherPersonStoreMockGet.Unlock()", "mock.lockGet.Unlock()",
"// ID is the id argument value", "// ID is the id argument value",
} }
for _, str := range strs { for _, str := range strs {

View File

@ -4,6 +4,7 @@ package moq
var moqImports = []string{} var moqImports = []string{}
// moqTemplate is the template for mocked code. // moqTemplate is the template for mocked code.
// language=GoTemplate
var moqTemplate = `// Code generated by moq; DO NOT EDIT. var moqTemplate = `// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq // github.com/matryer/moq
@ -17,11 +18,6 @@ import (
) )
{{ range $i, $obj := .Objects -}} {{ range $i, $obj := .Objects -}}
var (
{{- range .Methods }}
lock{{$obj.MockName}}{{.Name}} sync.RWMutex
{{- end }}
)
// Ensure, that {{.MockName}} does implement {{$sourcePackagePrefix}}{{.InterfaceName}}. // Ensure, that {{.MockName}} does implement {{$sourcePackagePrefix}}{{.InterfaceName}}.
// If this is not the case, regenerate this file with moq. // If this is not the case, regenerate this file with moq.
@ -59,6 +55,9 @@ type {{.MockName}} struct {
} }
{{- end }} {{- end }}
} }
{{- range .Methods }}
lock{{.Name}} sync.RWMutex
{{- end }}
} }
{{ range .Methods }} {{ range .Methods }}
// {{.Name}} calls {{.Name}}Func. // {{.Name}} calls {{.Name}}Func.
@ -75,9 +74,9 @@ func (mock *{{$obj.MockName}}) {{.Name}}({{.Arglist}}) {{.ReturnArglist}} {
{{ .Name | Exported }}: {{ .Name }}, {{ .Name | Exported }}: {{ .Name }},
{{- end }} {{- end }}
} }
lock{{$obj.MockName}}{{.Name}}.Lock() mock.lock{{.Name}}.Lock()
mock.calls.{{.Name}} = append(mock.calls.{{.Name}}, callInfo) mock.calls.{{.Name}} = append(mock.calls.{{.Name}}, callInfo)
lock{{$obj.MockName}}{{.Name}}.Unlock() mock.lock{{.Name}}.Unlock()
{{- if .ReturnArglist }} {{- if .ReturnArglist }}
return mock.{{.Name}}Func({{.ArgCallList}}) return mock.{{.Name}}Func({{.ArgCallList}})
{{- else }} {{- else }}
@ -98,9 +97,9 @@ func (mock *{{$obj.MockName}}) {{.Name}}Calls() []struct {
{{ .Name | Exported }} {{ .Type }} {{ .Name | Exported }} {{ .Type }}
{{- end }} {{- end }}
} }
lock{{$obj.MockName}}{{.Name}}.RLock() mock.lock{{.Name}}.RLock()
calls = mock.calls.{{.Name}} calls = mock.calls.{{.Name}}
lock{{$obj.MockName}}{{.Name}}.RUnlock() mock.lock{{.Name}}.RUnlock()
return calls return calls
} }
{{ end -}} {{ end -}}

View File

@ -8,11 +8,6 @@ import (
"sync" "sync"
) )
var (
lockDoSomethingMockAnother sync.RWMutex
lockDoSomethingMockDo sync.RWMutex
)
// Ensure, that DoSomethingMock does implement DoSomething. // Ensure, that DoSomethingMock does implement DoSomething.
// If this is not the case, regenerate this file with moq. // If this is not the case, regenerate this file with moq.
var _ DoSomething = &DoSomethingMock{} var _ DoSomething = &DoSomethingMock{}
@ -55,6 +50,8 @@ type DoSomethingMock struct {
Thing one.Thing Thing one.Thing
} }
} }
lockAnother sync.RWMutex
lockDo sync.RWMutex
} }
// Another calls AnotherFunc. // Another calls AnotherFunc.
@ -67,9 +64,9 @@ func (mock *DoSomethingMock) Another(thing one.Thing) error {
}{ }{
Thing: thing, Thing: thing,
} }
lockDoSomethingMockAnother.Lock() mock.lockAnother.Lock()
mock.calls.Another = append(mock.calls.Another, callInfo) mock.calls.Another = append(mock.calls.Another, callInfo)
lockDoSomethingMockAnother.Unlock() mock.lockAnother.Unlock()
return mock.AnotherFunc(thing) return mock.AnotherFunc(thing)
} }
@ -82,9 +79,9 @@ func (mock *DoSomethingMock) AnotherCalls() []struct {
var calls []struct { var calls []struct {
Thing one.Thing Thing one.Thing
} }
lockDoSomethingMockAnother.RLock() mock.lockAnother.RLock()
calls = mock.calls.Another calls = mock.calls.Another
lockDoSomethingMockAnother.RUnlock() mock.lockAnother.RUnlock()
return calls return calls
} }
@ -98,9 +95,9 @@ func (mock *DoSomethingMock) Do(thing one.Thing) error {
}{ }{
Thing: thing, Thing: thing,
} }
lockDoSomethingMockDo.Lock() mock.lockDo.Lock()
mock.calls.Do = append(mock.calls.Do, callInfo) mock.calls.Do = append(mock.calls.Do, callInfo)
lockDoSomethingMockDo.Unlock() mock.lockDo.Unlock()
return mock.DoFunc(thing) return mock.DoFunc(thing)
} }
@ -113,8 +110,8 @@ func (mock *DoSomethingMock) DoCalls() []struct {
var calls []struct { var calls []struct {
Thing one.Thing Thing one.Thing
} }
lockDoSomethingMockDo.RLock() mock.lockDo.RLock()
calls = mock.calls.Do calls = mock.calls.Do
lockDoSomethingMockDo.RUnlock() mock.lockDo.RUnlock()
return calls return calls
} }

View File

@ -9,11 +9,6 @@ import (
"github.com/matryer/moq/pkg/moq/testpackages/imports/one" "github.com/matryer/moq/pkg/moq/testpackages/imports/one"
) )
var (
lockDoSomethingMockAnother sync.RWMutex
lockDoSomethingMockDo sync.RWMutex
)
// Ensure, that DoSomethingMock does implement DoSomething. // Ensure, that DoSomethingMock does implement DoSomething.
// If this is not the case, regenerate this file with moq. // If this is not the case, regenerate this file with moq.
var _ DoSomething = &DoSomethingMock{} var _ DoSomething = &DoSomethingMock{}
@ -56,6 +51,8 @@ type DoSomethingMock struct {
Thing one.Thing Thing one.Thing
} }
} }
lockAnother sync.RWMutex
lockDo sync.RWMutex
} }
// Another calls AnotherFunc. // Another calls AnotherFunc.
@ -68,9 +65,9 @@ func (mock *DoSomethingMock) Another(thing one.Thing) error {
}{ }{
Thing: thing, Thing: thing,
} }
lockDoSomethingMockAnother.Lock() mock.lockAnother.Lock()
mock.calls.Another = append(mock.calls.Another, callInfo) mock.calls.Another = append(mock.calls.Another, callInfo)
lockDoSomethingMockAnother.Unlock() mock.lockAnother.Unlock()
return mock.AnotherFunc(thing) return mock.AnotherFunc(thing)
} }
@ -83,9 +80,9 @@ func (mock *DoSomethingMock) AnotherCalls() []struct {
var calls []struct { var calls []struct {
Thing one.Thing Thing one.Thing
} }
lockDoSomethingMockAnother.RLock() mock.lockAnother.RLock()
calls = mock.calls.Another calls = mock.calls.Another
lockDoSomethingMockAnother.RUnlock() mock.lockAnother.RUnlock()
return calls return calls
} }
@ -99,9 +96,9 @@ func (mock *DoSomethingMock) Do(thing one.Thing) error {
}{ }{
Thing: thing, Thing: thing,
} }
lockDoSomethingMockDo.Lock() mock.lockDo.Lock()
mock.calls.Do = append(mock.calls.Do, callInfo) mock.calls.Do = append(mock.calls.Do, callInfo)
lockDoSomethingMockDo.Unlock() mock.lockDo.Unlock()
return mock.DoFunc(thing) return mock.DoFunc(thing)
} }
@ -114,8 +111,8 @@ func (mock *DoSomethingMock) DoCalls() []struct {
var calls []struct { var calls []struct {
Thing one.Thing Thing one.Thing
} }
lockDoSomethingMockDo.RLock() mock.lockDo.RLock()
calls = mock.calls.Do calls = mock.calls.Do
lockDoSomethingMockDo.RUnlock() mock.lockDo.RUnlock()
return calls return calls
} }

View File

@ -7,10 +7,6 @@ import (
"sync" "sync"
) )
var (
lockEchoerMockEcho sync.RWMutex
)
// Ensure, that EchoerMock does implement Echoer. // Ensure, that EchoerMock does implement Echoer.
// If this is not the case, regenerate this file with moq. // If this is not the case, regenerate this file with moq.
var _ Echoer = &EchoerMock{} var _ Echoer = &EchoerMock{}
@ -42,6 +38,7 @@ type EchoerMock struct {
Ss []string Ss []string
} }
} }
lockEcho sync.RWMutex
} }
// Echo calls EchoFunc. // Echo calls EchoFunc.
@ -54,9 +51,9 @@ func (mock *EchoerMock) Echo(ss ...string) []string {
}{ }{
Ss: ss, Ss: ss,
} }
lockEchoerMockEcho.Lock() mock.lockEcho.Lock()
mock.calls.Echo = append(mock.calls.Echo, callInfo) mock.calls.Echo = append(mock.calls.Echo, callInfo)
lockEchoerMockEcho.Unlock() mock.lockEcho.Unlock()
return mock.EchoFunc(ss...) return mock.EchoFunc(ss...)
} }
@ -69,8 +66,8 @@ func (mock *EchoerMock) EchoCalls() []struct {
var calls []struct { var calls []struct {
Ss []string Ss []string
} }
lockEchoerMockEcho.RLock() mock.lockEcho.RLock()
calls = mock.calls.Echo calls = mock.calls.Echo
lockEchoerMockEcho.RUnlock() mock.lockEcho.RUnlock()
return calls return calls
} }