...
1
16
17 package internal_test
18
19 import (
20 "testing"
21 "time"
22
23 "k8s.io/apimachinery/pkg/util/managedfields/internal"
24 )
25
26 func TestAtMostEvery(t *testing.T) {
27 duration := time.Second
28 delay := 179 * time.Millisecond
29 atMostEvery := internal.NewAtMostEvery(delay)
30 count := 0
31 exit := time.NewTicker(duration)
32 tick := time.NewTicker(2 * time.Millisecond)
33 defer exit.Stop()
34 defer tick.Stop()
35
36 done := false
37 for !done {
38 select {
39 case <-exit.C:
40 done = true
41 case <-tick.C:
42 atMostEvery.Do(func() {
43 count++
44 })
45 }
46 }
47
48 if expected := int(duration/delay) + 1; count > expected {
49 t.Fatalf("Function called %d times, should have been called less than or equal to %d times", count, expected)
50 }
51 }
52
View as plain text