[core] Return when Every is sent done on its channel

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-05-13 17:30:46 -07:00
parent aab1461e0d
commit 0276721523
3 changed files with 32 additions and 1 deletions

30
examples/every_done.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
)
func main() {
gbot := gobot.NewGobot()
robot := gobot.NewRobot(
"hello",
func() {
done := gobot.Every(500*time.Millisecond, func() {
fmt.Println("Greetings human")
})
gobot.After(5*time.Second, func() {
done <- true
fmt.Println("We're done here")
})
},
)
gbot.AddRobot(robot)
gbot.Start()
}

View File

@ -35,7 +35,7 @@ func Every(t time.Duration, f func()) chan bool {
for {
select {
case <-done:
break
return
default:
<-c
go f()

View File

@ -31,6 +31,7 @@ func TestEveryWhenDone(t *testing.T) {
})
<-time.After(20 * time.Millisecond)
done <- true
<-time.After(50 * time.Millisecond)
if i > 1 {
t.Error("Test should have stopped after 20ms")
}