Skip to content

Commit

Permalink
lock when resetting stubs via Returns / ReturnsOnCall
Browse files Browse the repository at this point in the history
Does not help you if you mutate the stub directly. We will need to add a stub setter / clearer to assist people who are directly setting the Stub.

- #44
- #84
  • Loading branch information
joefitzgerald committed Oct 16, 2018
1 parent 2d74a21 commit 0c980ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions generator/function_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (fake *{{.Function.FakeName}}) ArgsForCall(i int) {{.Function.Params.AsRetu
{{if .Function.Returns.HasLength -}}
func (fake *{{.Function.FakeName}}) Returns({{.Function.Returns.AsNamedArgsWithTypes}}) {
fake.mutex.Lock()
defer fake.mutex.Unlock()
fake.Stub = nil
fake.returns = struct {
{{- range .Function.Returns}}
Expand All @@ -97,6 +99,8 @@ func (fake *{{.Function.FakeName}}) Returns({{.Function.Returns.AsNamedArgsWithT
}
func (fake *{{.Function.FakeName}}) ReturnsOnCall(i int, {{.Function.Returns.AsNamedArgsWithTypes}}) {
fake.mutex.Lock()
defer fake.mutex.Unlock()
fake.Stub = nil
if fake.returnsOnCall == nil {
fake.returnsOnCall = make(map[int]struct {
Expand Down
4 changes: 4 additions & 0 deletions generator/interface_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (fake *{{.FakeName}}) {{.Name}}ArgsForCall(i int) {{.Params.AsReturnSignatu
{{if .Returns.HasLength -}}
func (fake *{{.FakeName}}) {{.Name}}Returns({{.Returns.AsNamedArgsWithTypes}}) {
fake.{{UnExport .Name}}Mutex.Lock()
defer fake.{{UnExport .Name}}Mutex.Unlock()
fake.{{.Name}}Stub = nil
fake.{{UnExport .Name}}Returns = struct {
{{- range .Returns}}
Expand All @@ -106,6 +108,8 @@ func (fake *{{.FakeName}}) {{.Name}}Returns({{.Returns.AsNamedArgsWithTypes}}) {
}
func (fake *{{.FakeName}}) {{.Name}}ReturnsOnCall(i int, {{.Returns.AsNamedArgsWithTypes}}) {
fake.{{UnExport .Name}}Mutex.Lock()
defer fake.{{UnExport .Name}}Mutex.Unlock()
fake.{{.Name}}Stub = nil
if fake.{{UnExport .Name}}ReturnsOnCall == nil {
fake.{{UnExport .Name}}ReturnsOnCall = make(map[int]struct {
Expand Down
12 changes: 12 additions & 0 deletions integration/testdata/expected_fake_multiab.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ func (fake *FakeMultiAB) MineCallCount() int {
}

func (fake *FakeMultiAB) MineReturns(result1 foo.S) {
fake.mineMutex.Lock()
defer fake.mineMutex.Unlock()
fake.MineStub = nil
fake.mineReturns = struct {
result1 foo.S
}{result1}
}

func (fake *FakeMultiAB) MineReturnsOnCall(i int, result1 foo.S) {
fake.mineMutex.Lock()
defer fake.mineMutex.Unlock()
fake.MineStub = nil
if fake.mineReturnsOnCall == nil {
fake.mineReturnsOnCall = make(map[int]struct {
Expand Down Expand Up @@ -103,13 +107,17 @@ func (fake *FakeMultiAB) FromACallCount() int {
}

func (fake *FakeMultiAB) FromAReturns(result1 fooa.S) {
fake.fromAMutex.Lock()
defer fake.fromAMutex.Unlock()
fake.FromAStub = nil
fake.fromAReturns = struct {
result1 fooa.S
}{result1}
}

func (fake *FakeMultiAB) FromAReturnsOnCall(i int, result1 fooa.S) {
fake.fromAMutex.Lock()
defer fake.fromAMutex.Unlock()
fake.FromAStub = nil
if fake.fromAReturnsOnCall == nil {
fake.fromAReturnsOnCall = make(map[int]struct {
Expand Down Expand Up @@ -143,13 +151,17 @@ func (fake *FakeMultiAB) FromBCallCount() int {
}

func (fake *FakeMultiAB) FromBReturns(result1 foob.S) {
fake.fromBMutex.Lock()
defer fake.fromBMutex.Unlock()
fake.FromBStub = nil
fake.fromBReturns = struct {
result1 foob.S
}{result1}
}

func (fake *FakeMultiAB) FromBReturnsOnCall(i int, result1 foob.S) {
fake.fromBMutex.Lock()
defer fake.fromBMutex.Unlock()
fake.FromBStub = nil
if fake.fromBReturnsOnCall == nil {
fake.fromBReturnsOnCall = make(map[int]struct {
Expand Down
4 changes: 4 additions & 0 deletions integration/testdata/expected_fake_somethingfactory.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ func (fake *FakeSomethingFactory) ArgsForCall(i int) (string, map[string]interfa
}

func (fake *FakeSomethingFactory) Returns(result1 string) {
fake.mutex.Lock()
defer fake.mutex.Unlock()
fake.Stub = nil
fake.returns = struct {
result1 string
}{result1}
}

func (fake *FakeSomethingFactory) ReturnsOnCall(i int, result1 string) {
fake.mutex.Lock()
defer fake.mutex.Unlock()
fake.Stub = nil
if fake.returnsOnCall == nil {
fake.returnsOnCall = make(map[int]struct {
Expand Down
8 changes: 8 additions & 0 deletions integration/testdata/expected_fake_writecloser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ func (fake *FakeWriteCloser) CloseCallCount() int {
}

func (fake *FakeWriteCloser) CloseReturns(result1 error) {
fake.closeMutex.Lock()
defer fake.closeMutex.Unlock()
fake.CloseStub = nil
fake.closeReturns = struct {
result1 error
}{result1}
}

func (fake *FakeWriteCloser) CloseReturnsOnCall(i int, result1 error) {
fake.closeMutex.Lock()
defer fake.closeMutex.Unlock()
fake.CloseStub = nil
if fake.closeReturnsOnCall == nil {
fake.closeReturnsOnCall = make(map[int]struct {
Expand Down Expand Up @@ -113,6 +117,8 @@ func (fake *FakeWriteCloser) WriteArgsForCall(i int) []byte {
}

func (fake *FakeWriteCloser) WriteReturns(result1 int, result2 error) {
fake.writeMutex.Lock()
defer fake.writeMutex.Unlock()
fake.WriteStub = nil
fake.writeReturns = struct {
result1 int
Expand All @@ -121,6 +127,8 @@ func (fake *FakeWriteCloser) WriteReturns(result1 int, result2 error) {
}

func (fake *FakeWriteCloser) WriteReturnsOnCall(i int, result1 int, result2 error) {
fake.writeMutex.Lock()
defer fake.writeMutex.Unlock()
fake.WriteStub = nil
if fake.writeReturnsOnCall == nil {
fake.writeReturnsOnCall = make(map[int]struct {
Expand Down

0 comments on commit 0c980ea

Please sign in to comment.