mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
[core] Return when Every is sent done on its channel
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
5bdead3d8b
commit
491b8aa35a
30
examples/every_done.go
Normal file
30
examples/every_done.go
Normal 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()
|
||||
}
|
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…
x
Reference in New Issue
Block a user