github.com/prysmaticlabs/prysm@v1.4.4/shared/runutil/every_test.go (about) 1 package runutil_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/prysmaticlabs/prysm/shared/runutil" 9 ) 10 11 func TestEveryRuns(t *testing.T) { 12 ctx, cancel := context.WithCancel(context.Background()) 13 14 i := 0 15 runutil.RunEvery(ctx, 100*time.Millisecond, func() { 16 i++ 17 }) 18 19 // Sleep for a bit and ensure the value has increased. 20 time.Sleep(200 * time.Millisecond) 21 22 if i == 0 { 23 t.Error("Counter failed to increment with ticker") 24 } 25 26 cancel() 27 28 // Sleep for a bit to let the cancel take place. 29 time.Sleep(100 * time.Millisecond) 30 31 last := i 32 33 // Sleep for a bit and ensure the value has not increased. 34 time.Sleep(200 * time.Millisecond) 35 36 if i != last { 37 t.Error("Counter incremented after stop") 38 } 39 }