[core] gobot.Every function now returns a channel so it can be halted

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-05-13 17:21:21 -07:00
parent bf43df59b5
commit aab1461e0d
2 changed files with 27 additions and 5 deletions

View File

@ -23,17 +23,27 @@ var eventError = func(e *Event) (err error) {
return
}
// Every triggers f every t time until the end of days. It does not wait for the
// previous execution of f to finish before it fires the next f.
func Every(t time.Duration, f func()) {
// Every triggers f every t time until the end of days, or when a
// bool value is sent to the channel returned by the Every function.
// It does not wait for the previous execution of f to finish before
// it fires the next f.
func Every(t time.Duration, f func()) chan bool {
done := make(chan bool)
c := time.Tick(t)
go func() {
for {
select {
case <-done:
break
default:
<-c
go f()
}
}
}()
return done
}
// After triggers f after t duration.

View File

@ -24,6 +24,18 @@ func TestEvery(t *testing.T) {
}
}
func TestEveryWhenDone(t *testing.T) {
i := 0
done := Every(20*time.Millisecond, func() {
i++
})
<-time.After(20 * time.Millisecond)
done <- true
if i > 1 {
t.Error("Test should have stopped after 20ms")
}
}
func TestAfter(t *testing.T) {
i := 0
After(1*time.Millisecond, func() {