prysm-pulse/async/every_test.go

41 lines
800 B
Go
Raw Normal View History

package async_test
2019-12-13 23:14:56 +00:00
import (
"context"
"sync/atomic"
2019-12-13 23:14:56 +00:00
"testing"
"time"
"github.com/prysmaticlabs/prysm/async"
2019-12-13 23:14:56 +00:00
)
func TestEveryRuns(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
i := int32(0)
async.RunEvery(ctx, 100*time.Millisecond, func() {
atomic.AddInt32(&i, 1)
2019-12-13 23:14:56 +00:00
})
// Sleep for a bit and ensure the value has increased.
time.Sleep(200 * time.Millisecond)
2019-12-13 23:14:56 +00:00
if atomic.LoadInt32(&i) == 0 {
2019-12-13 23:14:56 +00:00
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 := atomic.LoadInt32(&i)
2019-12-13 23:14:56 +00:00
// 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 atomic.LoadInt32(&i) != last {
2019-12-13 23:14:56 +00:00
t.Error("Counter incremented after stop")
}
}