implement Clock.NewTimer #108

Merged
merged 1 commit into from Sep 26, 2016

Conversation

Projects
None yet
4 participants
Owner

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/)

@rogpeppe rogpeppe referenced this pull request in juju/utils Sep 26, 2016

Merged

clock: add NewTimer #242

👍

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()
@alesstimec

alesstimec Sep 26, 2016

Member

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?

@rogpeppe

rogpeppe Sep 26, 2016

Owner

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

Merge pull request #242 from rogpeppe/021-clock-newtimer
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.
implement Clock.NewTimer
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.
Owner

rogpeppe commented Sep 26, 2016

$$merge$$

Contributor

jujubot commented Sep 26, 2016

@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