Commit a029bd18 authored by Aaron Raddon's avatar Aaron Raddon

Fix waitfor

parent 40604364
......@@ -22,17 +22,16 @@ var (
// timeout (in seconds) is the last arg
func WaitFor(check func() bool, timeoutSecs int) {
timer := time.NewTicker(100 * time.Millisecond)
tryct := 0
for range timer.C {
if check() {
timer.Stop()
break
for {
select {
case <-timer.C:
if check() {
timer.Stop()
return
}
case <-time.After(time.Duration(timeoutSecs) * time.Second):
return
}
if tryct >= timeoutSecs*10 {
timer.Stop()
break
}
tryct++
}
}
......
package gou
import (
"testing"
"time"
)
func TestWaitFor(t *testing.T) {
isDone := false
foundDone := false
go func() {
time.Sleep(time.Second * 1)
isDone = true
}()
WaitFor(func() bool {
if isDone == true {
foundDone = true
}
return isDone == true
}, 2)
if !foundDone {
t.Fail()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment