Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

implement Clock.NewTimer #108

Merged
merged 1 commit into from
Sep 26, 2016
Merged

Conversation

rogpeppe
Copy link
Contributor

@rogpeppe rogpeppe commented Aug 4, 2016

Also fix some issues with the current implementation - it's allowable
to call Reset after a timer has expired to make the timer fire again,
which wouldn't work with the previous implementation.

Note that this will require a dependency update to use juju/utils#242.

(Review request: http://reviews.vapour.ws/r/5372/)

Copy link
Contributor

@mhilton mhilton left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Member

@alesstimec alesstimec left a comment

Choose a reason for hiding this comment

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

LGTM with a question..

}

// AfterFunc is part of the clock.Clock interface.
func (clock *Clock) AfterFunc(d time.Duration, f func()) clock.Timer {
return clock.addAlarm(d, nil, func() {
go f()
Copy link
Member

@alesstimec alesstimec Sep 26, 2016

Choose a reason for hiding this comment

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

could/should we use tomb to manage the lifetime of this goroutine? and the stop method should try to cleanup running routines as well.. could we have a test case for this scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, although I'm open to persuasion. Remember that this implementation is solely for testing purposes, and I it's generally better the less that the tests know about the details of the code (whether the code uses AfterFunc vs After), so the test shouldn't need to change if the code uses AfterFunc (we know when it completes) vs After (it triggers something that happens elsewhere).

And we can't make Stop clean up goroutines because that's not something that the stdlib Timer.Stop method provides - it's something that individual clients can do if they need to.

FWIW AfterFunc is very rarely used in the Juju source code - almost always to just update a local variable or field.

jujubot added a commit to juju/utils that referenced this pull request Sep 26, 2016
clock: add NewTimer

This allows mock clocks to use timers in the usual way.
This is associated with a companion branch in juju
testing (juju/testing#108) which needs
this as a dependency update.
Also fix some issues with the current implementation - it's allowable
to call Reset after a timer has expired to make the timer fire again,
which wouldn't work with the previous implementation.
@rogpeppe
Copy link
Contributor Author

$$merge$$

@jujubot
Copy link
Contributor

jujubot commented Sep 26, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju-testing

@jujubot jujubot merged commit 7177264 into juju:master Sep 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants