Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixing After(duration) to wait during method call #529

Merged
merged 2 commits into from
Dec 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Call struct {
// receives a message or is closed. nil means it returns immediately.
WaitFor <-chan time.Time

waitTime time.Duration

// Holds a handler used to manipulate arguments content that are passed by
// reference. It's useful when mocking methods such as unmarshalers or
// decoders.
Expand Down Expand Up @@ -134,7 +136,10 @@ func (c *Call) WaitUntil(w <-chan time.Time) *Call {
//
// Mock.On("MyMethod", arg1, arg2).After(time.Second)
func (c *Call) After(d time.Duration) *Call {
return c.WaitUntil(time.After(d))
c.lock()
defer c.unlock()
c.waitTime = d
return c
}

// Run sets a handler to be called before returning. It can be used when
Expand Down Expand Up @@ -327,18 +332,12 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
}
}

switch {
case call.Repeatability == 1:
if call.Repeatability == 1 {
call.Repeatability = -1
call.totalCalls++

case call.Repeatability > 1:
} else if call.Repeatability > 1 {
call.Repeatability--
call.totalCalls++

case call.Repeatability == 0:
call.totalCalls++
}
call.totalCalls++

// add the call
m.Calls = append(m.Calls, *newCall(m, methodName, arguments...))
Expand All @@ -347,6 +346,8 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
// block if specified
if call.WaitFor != nil {
<-call.WaitFor
} else {
time.Sleep(call.waitTime)
}

m.mutex.Lock()
Expand Down
31 changes: 31 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"errors"
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1316,6 +1317,36 @@ func Test_MockReturnAndCalledConcurrent(t *testing.T) {
wg.Wait()
}

type timer struct{ Mock }

func (s *timer) GetTime(i int) string {
return s.Called(i).Get(0).(string)
}

func TestAfterTotalWaitTimeWhileExecution(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests slow down the test run by ˜3 seconds.

Can you reduce wait times a lot more to keep tests quick?

waitDuration := 1
total, waitMs := 5, time.Millisecond*time.Duration(waitDuration)
aTimer := new(timer)
for i := 0; i < total; i++ {
aTimer.On("GetTime", i).After(waitMs).Return(fmt.Sprintf("Time%d", i)).Once()
}
time.Sleep(waitMs)
start := time.Now()
var results []string

for i := 0; i < total; i++ {
results = append(results, aTimer.GetTime(i))
}

end := time.Now()
elapsedTime := end.Sub(start)
assert.True(t, elapsedTime > waitMs, fmt.Sprintf("Total elapsed time:%v should be atleast greater than %v", elapsedTime, waitMs))
assert.Equal(t, total, len(results))
for i, _ := range results {
assert.Equal(t, fmt.Sprintf("Time%d", i), results[i], "Return value of method should be same")
}
}

func ConcurrencyTestMethod(m *Mock) {
m.Called()
}