2023-07-14 17:30:58 +02:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-07-18 15:57:54 +02:00
|
|
|
R "github.com/IBM/fp-go/retry"
|
2023-07-14 17:30:58 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var expLogBackoff = R.ExponentialBackoff(10)
|
|
|
|
|
|
|
|
// our retry policy with a 1s cap
|
|
|
|
var testLogPolicy = R.CapDelay(
|
|
|
|
2*time.Second,
|
|
|
|
R.Monoid.Concat(expLogBackoff, R.LimitRetries(20)),
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
action := func(status R.RetryStatus) IO[string] {
|
|
|
|
return Of(fmt.Sprintf("Retrying %d", status.IterNumber))
|
|
|
|
}
|
|
|
|
check := func(value string) bool {
|
|
|
|
return !strings.Contains(value, "5")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := Retrying(testLogPolicy, action, check)
|
|
|
|
|
|
|
|
assert.Equal(t, "Retrying 5", r())
|
|
|
|
}
|