Skip to content

Commit

Permalink
Fix .Timeout() with mutexes (broken with maraino#10)
Browse files Browse the repository at this point in the history
in maraino#10 we added a mutex around .Called(), but this causes an issue when
the mock has .Timeout() set. Currently all callers to the mock are
locked out for this timeout.

As a simple fix, wait to do the sleep until
after we have finished the method and unlocked the mutex.
  • Loading branch information
wadey committed Jul 21, 2017
1 parent ef7faf3 commit 1c59267
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,13 @@ func (m *Mock) When(name string, arguments ...interface{}) *MockFunction {
// return r.Int(0), r.String(1), r.Error(2)
// }
func (m *Mock) Called(arguments ...interface{}) *MockResult {
defer m.mutex.Unlock()
var timeout time.Duration
defer func() {
m.mutex.Unlock()
if timeout > 0 {
time.Sleep(timeout)
}
}()
m.mutex.Lock()

pc, _, _, ok := runtime.Caller(1)
Expand Down Expand Up @@ -312,9 +318,7 @@ func (m *Mock) Called(arguments ...interface{}) *MockResult {
}
}

if f.timeout > 0 {
time.Sleep(f.timeout)
}
timeout = f.timeout

if f.PanicValue != nil {
panic(f.PanicValue)
Expand Down

0 comments on commit 1c59267

Please sign in to comment.