[core] Return when Every is sent done on its channel
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
aab1461e0d
commit
0276721523
|
@ -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()
|
||||
}
|
2
utils.go
2
utils.go
|
@ -35,7 +35,7 @@ func Every(t time.Duration, f func()) chan bool {
|
|||
for {
|
||||
select {
|
||||
case <-done:
|
||||
break
|
||||
return
|
||||
default:
|
||||
<-c
|
||||
go f()
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue