prysm-pulse/shared/runutil/every_test.go

40 lines
715 B
Go
Raw Normal View History

2019-12-13 23:14:56 +00:00
package runutil_test
import (
"context"
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/runutil"
)
func TestEveryRuns(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
i := 0
runutil.RunEvery(ctx, 100*time.Millisecond, func() {
2019-12-13 23:14:56 +00:00
i++
})
// Sleep for a bit and ensure the value has increased.
time.Sleep(200 * time.Millisecond)
2019-12-13 23:14:56 +00:00
if i == 0 {
t.Error("Counter failed to increment with ticker")
}
cancel()
// Sleep for a bit to let the cancel take place.
time.Sleep(100 * time.Millisecond)
2019-12-13 23:14:56 +00:00
last := i
// Sleep for a bit and ensure the value has not increased.
time.Sleep(200 * time.Millisecond)
2019-12-13 23:14:56 +00:00
if i != last {
t.Error("Counter incremented after stop")
}
}