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

Conversation

devdinu
Copy link
Contributor

@devdinu devdinu commented Dec 29, 2017

After was using call.WaitUntil(time.After(duration)),
ch := time.After(duration) returns a channel with the timer immediately started.

.gitignore Outdated
@@ -22,3 +22,4 @@ _testmain.go
*.exe

.DS_Store
*.swp
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this from the .gitignore?

It is a detail from your editor's configuration. You set a global .gitignore on your machine so Git always ignores .swp files.

mock/mock.go Outdated
@@ -52,6 +52,8 @@ type Call struct {
// receives a message or is closed. nil means it returns immediately.
WaitFor <-chan time.Time

WaitTime time.Duration
Copy link
Member

Choose a reason for hiding this comment

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

Unexport this, so it has to be set with After.

Copy link
Contributor Author

@devdinu devdinu Dec 30, 2017

Choose a reason for hiding this comment

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

Followed thinking its a convention, because other members are exported. will change it.

mock/mock.go Outdated
@@ -335,6 +332,7 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
if call.WaitFor != nil {
<-call.WaitFor
}
time.Sleep(call.WaitTime)
Copy link
Member

Choose a reason for hiding this comment

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

Rather than doing time.Sleep, do call.WaitFor = time.After(call.waitTime) before the if.

Otherwise it might be possible to set two different waiting times for the same call.

Copy link
Contributor Author

@devdinu devdinu Dec 30, 2017

Choose a reason for hiding this comment

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

Then in that case we would be losing the channel which is set via time.WaitUntil (which i can control when to send data not just with timer. Maybe if the channel is nil i could do that, but we might lose the duration in waitTime, Is it a valid case to keep both wait on channel and duration? if not will update it.

Copy link
Member

Choose a reason for hiding this comment

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

That is not allowed right now anyway, since After currently calls WaitUntil and overrides the property.

The change I mention would be aligned with how things work right now.

By the way, remember to lock/unlock the mutex in After.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ernesto-jimenez i've added time.Sleep(waitTime) if waitFor is nil, adding it before would make the duration override, in this case channel is used. Anyway It depends on the client usage WaitUntil(ch).After(d) or .After(d).WaitUntil(ch) and not defined in here. so i hope this is fine.

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?

}

func TestAfterTotalWaitTimeWhileExecution(t *testing.T) {
waitDuration := 10
Copy link
Member

Choose a reason for hiding this comment

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

would 1ms be enough here?

The faster we can make it, the better :)

@ernesto-jimenez
Copy link
Member

@Dineshkumar-cse awesome! do you mind squashing into a single commit?

Thanks

commit 5b0291d47dc3a70cc6c6be3bba3c2f934a8e933e
Merge: 1f324ec 8ccf48a
Author: Dinesh Kumar <dineshkumar-cse@users.noreply.github.com>
Date:   Sat Dec 30 19:55:13 2017 +0530

    Merge branch 'master' into master

commit 1f324ec
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Sat Dec 30 19:07:16 2017 +0530

    Fixing comments: reduced test time, locking in after, unexported waitTime

    - WaitUntil/After overrides each other value
    - currently if channel is set that takes the priority, if not the
      waitTime is used to Sleep

commit a7101ec
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Fri Dec 29 13:02:14 2017 +0530

    Using if else instead of switch and pulling out the commone one

commit 936f63d
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Fri Dec 29 12:41:40 2017 +0530

    After - making it wait during method call

    After was using call.WaitUntil(time.After(duration)),
    <-time.After(duration) returns a channel with the timer immediately
    started
@devdinu
Copy link
Contributor Author

devdinu commented Dec 30, 2017

Done. @ernesto-jimenez Thanks for reviewing it. We can close #464

@ernesto-jimenez
Copy link
Member

ernesto-jimenez commented Dec 30, 2017

Cool, will merge once CI passes.

Thanks!

@ernesto-jimenez ernesto-jimenez merged commit b3596e9 into stretchr:master Dec 30, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants