github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/backoff_test.go (about)

     1  package f_test
     2  
     3  import (
     4  	"context"
     5  	"github.com/angenalZZZ/gofunc/f"
     6  	"testing"
     7  )
     8  
     9  func TestNewRetryOperation(t *testing.T) {
    10  	var no int
    11  	ctx, cancel := context.WithCancel(context.Background())
    12  
    13  	// Call Periods: 1s,2s,4s,8s,16s,32s,1m,1m,1m... 24h=1day
    14  	ro := f.NewRetryOperationWithPeriod(func() error {
    15  		if no <= 100 {
    16  			no++
    17  			println(f.Now().LocalTimeString())
    18  			if no == 10 {
    19  				cancel()
    20  			}
    21  			return f.ErrRetryOperationFailure
    22  		}
    23  		return nil
    24  	}, f.RetryPeriodOf1s1m1d, ctx)
    25  
    26  	// Call Retry Start
    27  	if err := ro.Retry(); err != nil && err != f.ErrRetryOperationFailure {
    28  		t.Fatal(err)
    29  	}
    30  }