1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
package retry
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"k8s.io/utils/clock"
)
type fakeBackoff struct{}
func (b *fakeBackoff) Backoff() clock.Timer {
return clock.RealClock{}.NewTimer(0)
}
func TestRetry_PollWithBackoff_Poke(t *testing.T) {
// GIVEN
cfg := NewPollConfigFactory(1*time.Hour, func() BackoffManager { return &fakeBackoff{} })()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
isInitCall := true
// WHEN
// immediately poke (the poke can happen before the actual polling has been started
go func() {
cfg.Poke()
}()
err := PollWithBackoff(ctx, cfg, func(ctx context.Context) (error, AttemptResult) {
if isInitCall {
// PollWithBackoff does an initial call, so to test if a poke interrupts
// the interval correctly, we have to adhere for that initial call.
isInitCall = false
return ctx.Err(), Continue
}
return ctx.Err(), Done
})
// THEN
require.NoError(t, err)
}
|